FIX: The original article was a bit of a mistake, mainly about the subclass of the member's inner class.
Because of my cognitive limitations, it is assumed that the subclass of the inner class should also be an inner class, but in fact, the knowledge about the inheritance of the inner class will be explained in note .
The main areas of change are:
1). Added "5.innerclasschild2 and internal class different packages", tested the visibility of the member methods of the inner class in the subclasses of the different packages of the members ' inner classes.
2). The last picture is modified, and the visibility of the member method of the protected permission of the inner class of public is: class of the same package and subclass of the inner class of the different package
In the previous article-note 20 added the first four points, and here goes the remaining two points.
Key points are as follows:
5. The access permission modifier can decorate the member inner class
1). Access rights modifiers restrict access to internal classes in accordance with the same rules as the general members of the external class, please refer to note 10 for details;
2). The subclass referred to here is the subclass of the outer class ;
6. Access permission modifiers can decorate member variables and member methods of members ' inner classes
1). The member inner class can be decorated with 4 access modifiers, and the member methods or variables of each inner class can also be decorated with 4 access modifiers, so it is discussed in 16 cases;
2). The subclass that is involved in modifying the class is the subclass of the outer class , and the subclass that is involved in modifying the member method or variable of the inner class is the subclass of the member's inner class;
3). The default modifier of the construction method is consistent with the class;
4). When you create an object in a protected-decorated inner class in a different bun class of an external class, you are prompted that the construction method is not visible, and you want to add the public- modified construction method to the inner class;
5). The visibility of the member methods of the inner class is determined by the modifiers of the inner class and the modifiers of the member method ;
Cicada Cicada
Please respect the author's labor results, if necessary reprint please indicate "reprint" in the title, and indicate the original link:
Http://www.cnblogs.com/chanchan/p/8337173.html
PS: Why can't I turn back to the essay after turning into a diary? Had to be re-sent once.
The following details expand the instructions.
The class person defines 4 member inner classes, respectively, by public, protected, default, private, and adds 4 member methods to each of the 4 member inner classes, respectively, by public, protected, default, and private adornments. The code is as follows:
1 Public classINNERCLASSPU {2 voidprintincpudef () {3SYSTEM.OUT.PRINTLN ("Public inner Class:default");4 }5 6 Public voidPrintincpupu () {7SYSTEM.OUT.PRINTLN ("Public inner Class:public");8 }9 Ten protected voidPrintincpupro () { OneSYSTEM.OUT.PRINTLN ("Public inner class:protected"); A } - - Private voidPrintincpupri () { theSYSTEM.OUT.PRINTLN ("Public inner Class:private"); - } - } - + protected classInnerclasspro { - voidprintincprodef () { +SYSTEM.OUT.PRINTLN ("Protected inner Class:default"); A } at - Public voidPrintincpropu () { -SYSTEM.OUT.PRINTLN ("Protected inner class:public"); - } - - protected voidPrintincpropro () { inSYSTEM.OUT.PRINTLN ("Protected inner class:protected"); - } to + Private voidPrintincpropri () { -SYSTEM.OUT.PRINTLN ("Protected inner class:private"); the } * } $ Panax Notoginseng classInnerclass { - //methods for modifying the inner class with the access permission modifier the voidprintincdefdef () { +SYSTEM.OUT.PRINTLN ("Default inner Class:default"); A } the + Public voidPrintincdefpu () { -SYSTEM.OUT.PRINTLN ("Default inner Class:public"); $ } $ - protected voidPrintincdefpro () { -SYSTEM.OUT.PRINTLN ("Default inner class:protected"); the } - Wuyi Private voidPrintincdefpri () { theSYSTEM.OUT.PRINTLN ("Default inner Class:private"); - } Wu } - About Private classInnerclasspri { $ voidprintincpridef () { -SYSTEM.OUT.PRINTLN ("Private inner Class:default"); - } - A Public voidPrintincpripu () { +SYSTEM.OUT.PRINTLN ("Private inner Class:public"); the } - $ protected voidPrintincpripro () { theSYSTEM.OUT.PRINTLN ("Private inner class:protected"); the } the the Private voidPrintincpripri () { -SYSTEM.OUT.PRINTLN ("Private inner Class:private"); in } the}
A. Depending on the nature of the access modifier, to verify the access rights of each member's internal class, you need to
1). External class
2). class with external similarity to a package
3). Subclasses of different packages from external classes
4). Other classes that are different packages from the external class
Add the test code in these 4 categories.
Note:
Here is the member's inner class as a member of the outer class , that is, the member inner class is a whole, for other classes it is a variable of the outer class, so the subclass involved in this is the subclass of the outer class.
B. Also, depending on the nature of the access modifier, to verify the access rights for each member method, you need to
1). External class
2). Class that is similar to a package within a member
3). Subclasses of the inner class of the members of the class that are different from the members inner classes (corrections. 2018.1.23)
4). Other classes that have different packages from the members ' internal classes
1> Other classes are subclasses of an outer class
2> Other classes are not subclasses of the outer class
Add the test code in these 5 categories.
Note:
1]. This is to test the member method of the member's inner class, so for Case 3, it should be a subclass of the member's inner class, not the subclass of the outer class;
And because the member inner class is inside the outer class, it is impossible for its subclasses to be in other packages, so the 3rd case does not exist. (subclasses of members inner classes can be in other packages 2018.1.24)
2]. Because the member inner class is a member of the outer class, its visibility is not the same as the subclass and non-subclass of the outer class of its different packages, so the case 4 is subdivided into two cases.
The combination of a, b,a in the above 1, 2, 3, 4, and B in the 1, 2, 4.1, 4.2, and then append b 3 of the test code, see 5th below. (Append: 2018.1.24)
The following graphs are all the test code:
Where person is an external class, Testmain is the same package class, Gateman is a non-subclass of person of different package, teacher is a subclass of person of different package.
Tests within 1.Person:
By:
All members internal classes are visible to the external class;
All member methods of the inner class of all members are visible to the outer class;
Tests within 2.TestMain:
By:
The member inner class of private adornment is not visible to other classes with external similar package;
All member methods of the private decorated member inner class are not visible to other classes that are internally similar to the package;
Members of public, protected, and default permissions are internally class-visible to other classes that are similar to external packages;
Public, protected, and member methods of the public, protected, and default permissions for members of the default permissions are visible to other classes that are internally similar to the package;
Public, protected, and member methods of the private permission of the member inner class of the default permission are visible to other classes that are internally similar to the package;
Tests within 3.GateMan:
By:
The member inner class of the public adornment is visible to the non-subclasses of the package other than the outer class;
The member method of the public permission of the member inner class of the public adornment is visible to the non-subclasses of the different packages of the inner class;
The member method of the other permissions of the member inner class that is modified by public is not visible to the non-subclasses of the inner class that are different from the package;
Protected, default, and private decorated members inner classes are not visible to non-subclasses of different packages from external classes;
All member methods of the protected, default, and private decorated member inner classes are invisible to---classes that are not visible to the non-subclasses of the inner class, whose member methods are not visible;
Tests within 4.Teacher:
Note 28 lines, and the error of the prompt is different from the other lines:
That is, the constructor of the protected-modified member inner class Innerclasspro is not visible.
But line 42nd, Incpro.printincpropu (), is no problem, that is to say Innerclasspro can be used in teacher.
Why is there such a contradiction?
Analysis:
The class teacher is not in the same package as the class person, but is a subclass of man, so the protected-modified inner class Innerclasspro is visible to teacher;
However, the class Innerclasspro does not explicitly create a construction method, so its default construction method is also protected;
For the inner class Innerclasspro, the class teacher is not in the same package, and is not its subclass, so teacher is not seen the protected decoration of the construction method;
As a result, the above error has occurred.
Countermeasures:
add a constructor method to the class Innerclasspro and declare it public, so that the teacher can see its construction method and create the Innerclasspro object correctly.
The code is as follows:
Protected class Innerclasspro { //constructor method is public public Innerclasspro () { } ... }
After the modification, the above problem will not appear, see:
The complete test code in the teacher class is no longer analyzed as follows:
5.innerclasschild2 different packages from internal classes
InnerClassChild2 to inherit a member inner class, as shown, only member inner classes that are decorated with public are visible in other packages , that is, only member inner classes that have public permissions can have subclasses in other packages.
InnerClassChild2 inherits the inner class PERSON.INNERCLASSPU, as shown in:
Create subclass Object Incch, member methods Printincpupu and Printincpupro are both visible , because InnerClassChild2 are subclasses and different packages, So member methods that are decorated by public and protected in the parent class are visible, and the default and private decorated member methods are not visible;
Create the parent class object INCPU, only the member method Printincpupu is visible;
Note 10 also appears in this phenomenon, the following excerpt from note 10:
If the teacher class object is defined in the subclass teacher of a different package from the person, the object can access and invoke member variables and member methods of public and protected adornments in person, default, Neither the member variable nor the member method of the private adornment can be accessed or invoked;
If the person class object is defined, only the member variables and member methods that are decorated with the public in person can be accessed and invoked through the object, and neither protected, default, private-decorated member variables, or member methods can be accessed or invoked.
Question: Do not understand why there is such a difference.
Summarize:
Through the above 5-step test, you can see:
1). When a member of a member's inner class and a member's inner class is decorated with the access modifier, the same rules are followed when the members of the generic class are decorated (see note 10).
2). The visibility of the member methods of the inner class is determined by the modifiers of the inner class and the modifiers of the member method.
Report:
The visibility of 16 cases is listed in sequence, the more special is:
1). The member method of the protected of the inner class innerclasspu of the public adornment;
2). Protected the member method of the public of the inner class Innerclasspro that is modified;
The reasons above have been explained in detail, not to repeat.
(Note: The figure is a bit large, can be opened to view)
Java Learning Note---Supplementary description of internal classes to Members (ii) (Amendment)