Understand the. this and. new syntaxes of java-8.3 internal classes from the beginning
In this section, we will talk about the. this and. new syntaxes of internal classes.
In fact, the above is a bit of a problem. The correct statement is the external Class Object. this and the external Class Object. new.
Example:
Package com. ray. ch03; public class Test {private int testId = 1; public Test () {System. out. println (testId);} class MyTest {private int myTestId = 0; public MyTest () {this. myTestId = Test. this. testId + 1; System. out. println (myTestId) ;}} public static void main (String [] args) {Test test = new Test (); Test. myTest myTest = test. new MyTest ();}}
Because the internal class already holds the reference of the external class by default, you can use the external class. this to reference the object, so there will be Test. this. testId + 1
Similarly, we will hold the reference of the object test, and then generate the internal class object through the. new syntax, so there is a new Test (). new MyTest () syntax.
To sum up, this chapter mainly discusses the. this and. new syntaxes of internal classes.