简体   繁体   English

Java:我可以在外部 class 中创建可变类型的内部 class 吗?

[英]Java: Can I create a variable type of inner class in an outer class?

So We were covering the topic of LinkedList and noticed a variable of type Node (inner class name) was declared on the outer class SingleLinkedList.所以我们正在讨论 LinkedList 的主题,并注意到在外部 class SingleLinkedList 上声明了一个 Node 类型的变量(内部 class 名称)。 Why is this behavior allowed?为什么允许这种行为? I knew you could access members of your inner class in your outer class but not declare variables of the inner class我知道您可以在外部 class 中访问内部 class 的成员,但不能声明内部 class 的变量

private class SingleLinkedList {

        private Node head; //This here is my doubt 
         //.
         //.
         //.
                
      private class Node {

        // reference to the next node in the chain, or null if there isn't one.
        Node next;
 
        // data carried by this node. could be of any type you need.
        Object data;
 
        // Node constructor
        public Node(Object dataValue) {
            next = null;
            data = dataValue;
        }
}

Thanks in advance.提前致谢。

nothing wrong with it, you define an inner class, then an instance of it.没有错,你定义了一个内部 class,然后是它的一个实例。 you can also declare instances of inner classes later defined in the outer class - the compiler is smart enough to make several passes through the code and find the references.您还可以声明稍后在外部 class 中定义的内部类的实例 - 编译器足够聪明,可以多次通过代码并找到引用。

About the second question, you can do:关于第二个问题,你可以这样做:

class Person {
    class Friend {
       Person p1,p2;
       Friend(Person a, Person b) { p1=a;p2=a}
    }
    List<Friend> friends;
}

But this will fail:但这会失败:

class Person {
    Person(String name) {}

    class Friend {
       

       Person p1=new Person("john"), p2=new Person("mike");

       Friend(Person a, Person b) { p1=a;p2=a}

    }
    List<Friend> friends;
}

Think about it: if not in the "SingleLinkedList" class scope, where else can "Node" be used?想一想:如果不在“SingleLinkedList”class scope 中,“节点”还能在哪里使用?

Java allows you to define a class within another class, ie a nested class or an inner class. Java allows you to define a class within another class, ie a nested class or an inner class. In Java, an inner/nested class is a member of its enclosing class.在 Java 中,内部/嵌套 class 是其封闭 class 的成员。

class OuterClass {
   ...
   class InnerClass {
     ...
   }
}

Q1: Why is this behavior allowed? Q1: Why is this behavior allowed?

A1: Here are three compelling reasons: A1:以下是三个令人信服的理由:

  • More encapsulation: If InnerClass above was declared private, it can only be accessed by members of OuterClass .更多封装:如果上面的 InnerClass 被声明为私有,那么它只能被InnerClass的成员OuterClass It would be invisible to the outside world.它对外界来说是不可见的。

  • A class used only one place: If InnerClass is only used by OuterClass and no other class and is a component of OuterClass , it would make it more appropriate to define it inside OuterClass . A class 只使用了一个地方:如果InnerClass仅由OuterClass使用,而没有其他 class 并且是OuterClass的一个component ,则将其定义在OuterClass内部会更合适。

  • More readability: Nesting classes in the location where they are relevant and used, makes the code more readable.更具可读性:将类嵌套在相关和使用的位置,使代码更具可读性。

Q2: I knew you could access members of your inner class in your outer class but not declare variables of the inner class Q2: I knew you could access members of your inner class in your outer class but not declare variables of the inner class

A2: Yes, as mentioned before, inner classes are members of the enclosing class. A2:是的,如前所述,内部类是封闭 class 的成员。

  • So you can declare a variable of the inner class within the enclosing class ( OuterClass ).因此,您可以在封闭的 class ( OuterClass ) 中声明内部 class 的变量。

  • Also, an instance of InnerClass only exists within an instance of the OuterClass .此外, OuterClass的实例仅存在于InnerClass的实例中。 The inner instance has direct access to the methods and fields of its enclosing instance (even if they are declared private).内部实例可以直接访问其封闭实例的方法和字段(即使它们被声明为私有)。

To take this one step further, to create an object of inner class from a scope outside of both classes, you must first instantiate the outer class.为了更进一步,要从两个类之外的 scope 创建内部 class 的 object,您必须首先实例化外部 ZA2F2ED4F8EBC0A58Z。 Then, create the inner object within the outer object with this syntax:然后,使用以下语法在外部 object 中创建内部 object:

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM