简体   繁体   English

Java内外部类之间的关系?

[英]Relationship between Inner and Outer Class in Java?

This is my Book.java code 这是我的Book.java代码

public class Book {
    private int pageNumber;

    private class BookReader{
        public int getPage(){
            return pageNumber;
        }
    }
}

When I complied it and used javap I got following things for the two classes 当我编译它并使用javap我得到了两个类的内容

For Book$BookReader.class 对于Book$BookReader.class

This is the output code 这是输出代码

Compiled from "Book.jav
class Book$BookReader {
  final Book this$0;
  public int getPage();
}

My question is why final is added while making any reference here and why this reference was made? 我的问题是,为什么在此处进行引用时添加了final ,以及为什么引用了该引用? What is its use in innerclass? 它在内部类中有什么用?

For Book.class 对于Book.class

$ javap Book.class
Compiled from "Book.java"
public class Book {
  public Book();
  static int access$000(Book);
}

Why static is added for variable and why Book has been passed as parameter here? 为什么为变量添加了静态变量,为什么在此处将Book作为参数传递?

Please explain it in simple terms if possible! 如果可能的话,请用简单的方式解释一下!

In BookReader , the final variable this$0 will hold a reference to the BookReader 's containing Book instance. BookReaderfinal变量this$0将保存对BookReader包含的Book实例的引用。 This is final because it is determined for each BookReader instance when that instance is created, by the manner of its creation, and cannot thereafter change. 这是final因为它是BookReader创建实例的方式为每个BookReader实例确定的,并且此后无法更改。

In class Book , the static method access$000 is a synthetic accessor method for the benefit of class Book.BookReader . Book类中,静态方法access$000是类Book.BookReader的综合访问器方法。 As an inner class of Book , each BookReader has access to the member variables of its containing instance, but the Java runtime does not actually know this, and the class file format has no special representation for it. 作为Book的内部类,每个BookReader都可以访问其包含的实例的成员变量,但是Java运行时实际上并不知道这一点,并且类文件格式对此没有特殊的表示形式。

For BookReader to be able to access private member Book.pageNumber , therefore, the compiler generates a synthetic, default-access method for that purpose in class Book , and in BookReader writes accesses to the outer class's variable in terms of that method. 为了使BookReader能够访问private成员Book.pageNumber ,编译器为此在Book类中生成一个综合的默认访问方法,并且在BookReader中使用该方法对外部类的变量进行访问。

A non static inner class has a reference to its parent instance. 非静态内部类对其父实例具有引用。 That is the 那就是

final Book this$0;

The reference to the parent Book instance cannot be changed at run time, which is why it is final. 对父Book实例的引用不能在运行时更改,这就是为什么它是最终的。 Which is to say your BookReader has a reference to exactly on Book that is assigned on construction and cannot be changed later. 也就是说,您的BookReader完全引用了Book,该参考是在构造时分配的,以后无法更改。

The line: 该行:

static int access$000(Book);

Is a package-level static accessor method. 是包级静态访问器方法。 It is used to allow the inner class access the private members of the outer. 它用于允许内部类访问外部的私有成员。

You define BookReader by: 您可以通过以下方式定义BookReader

class Book {
    private class BookReader {  }
}

This class relies on an instance of Book being created therefore the compiler creates the reference and makes it final (this is an optimisation as each Book instance can create BookReader ) 该类依赖于正在创建的Book实例,因此编译器创建引用并将其设为最终引用(这是一个优化,因为每个Book实例都可以创建BookReader

If you defined BookReader by: 如果您通过以下方式定义BookReader

class Book {
    private static class BookReader {  }
}

Then the reference would not exist as a Book reader can be created without an instance of Book. 然后,该引用将不存在,因为可以在没有Book实例的情况下创建Book Reader。

See here . 这里

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

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