如何访问不同包中的子类?

时间:2018-05-22 03:04:46

标签: java access-modifiers

在包1中,我有Mainclass和SubClass。如果在包2中如何访问子类?

如果我们想要访问同一个包但不同文件Java的子类,我们可以使用如下:

  package Modifier;

        public class Other {
        public static void main(String[] args) {
            Public ut =  new Public();
     // Access the subclass of a class in the same package different file
            Public.subPublic sub_ut = ut.new subPublic();
        sub_ut();
        }
    }     

但是如何在不同的Package中访问Class的子类?感谢...

2 个答案:

答案 0 :(得分:0)

我认为这没有意义,但如果你想这样做。检查你的访问修饰符,你必须使用public:

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>


 <button id="myBtn" class="btn btn-primary" type="button" data-toggle="collapse" data-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
    Button 
  </button>

<div class="collapse" id="collapseExample">
  <div class="card card-body">
    Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident.
  </div>
</div>

答案 1 :(得分:0)

内部类访问与访问字段或方法没有什么不同。您可以为内部类使用public,protected,package或private访问修饰符。