Java Study Notes 21 --- supplementary description of internal classes of members (2) (corrected), Study Notes 21 ---

Source: Internet
Author: User

Java Study Notes 21 --- supplementary description of internal classes of members (2) (corrected), Study Notes 21 ---

Fixed: the original article was a bit incorrect, mainly concerning the subclass of the member's internal classes.

Due to my cognitive limitations, I think that the subclass of the internal class should also be an internal class.The points of internal class inheritance will be in note 22.

Major changes include:

1). append"5. InnerClassChild2 and internal class different packages ",The visibility of the member methods of the internal class in the subclass of different packages of the internal class is tested.

2). modified the final image. The visibility of the protected permission member method of the public internal class is:Classes of the same package and subclasses of internal classes of different packages

 

In the previous article -- note 20, the first four points are added. Here we will talk about the remaining two points.

Key points are as follows:

5. The access permission modifier can modify the internal class of a member.

1). the access permission modifier's access restrictions for internal departments and general members of external departments follow the same rules. For details, refer to Note 10;

2). What is involved hereSubclass is a subclass of an external class.;

6. The access permission modifier can modify the member variables and member methods of the member's internal classes.

1 ). the internal class of a member can be modified by four types of access permission modifiers. The member methods or variables of each internal class can also be modified by four access permission modifiers, which are discussed in 16 cases;

2 ).Subclass is a subclass of an external class.When modifying the member methods or variables of an internal classA subclass is a subclass of a Member's internal class;

3). The default modifier of the constructor is the same as that of the class;

4). When you create an object for the protected internal class in different steamed stuffed bun classes of the external class, a prompt is displayed that the constructor is invisible.Add the public modifier constructor;

5) the visibility of the member methods of the internal class is jointly determined by the modifier of the internal class and the modifier of the member methods.;

 

Author: Chan

Please respect the author's Labor achievements. If you need to repost the work results, please enter the words "reprinted" in the title and indicate the original article link:

Http://www.cnblogs.com/chanchan/p/8337173.html

PS: Why can't I go back to the notebook after turning it into a diary? I had to resend it once.

 

The following is a detailed description.

Class Person defines four member Internal classes, which are modified by public, protected, default, and private, and adds four member methods to the four member Internal classes respectively, modified by public, protected, default, and private respectively. The Code is as follows:

1 public class InnerClassPu {2 void printInCPuDef () {3 System. out. println ("public inner class: default"); 4} 5 6 public void printInCPuPu () {7 System. out. println ("public inner class: public"); 8} 9 10 protected void printInCPuPro () {11 System. out. println ("public inner class: protected"); 12} 13 14 private void printInCPuPri () {15 System. out. println ("public inner class: private"); 16} 17} 18 19 protected class InnerClassPro {20 void printInCProDef () {21 System. out. println ("protected inner class: default"); 22} 23 24 public void printInCProPu () {25 System. out. println ("protected inner class: public"); 26} 27 28 protected void printInCProPro () {29 System. out. println ("protected inner class: protected"); 30} 31 32 private void printInCProPri () {33 System. out. println ("protected inner class: private"); 34} 35} 36 37 class InnerClass {38 // Method for modifying the internal class 39 void printInCDefDef () {40 System. out. println ("default inner class: default"); 41} 42 43 public void printInCDefPu () {44 System. out. println ("default inner class: public"); 45} 46 47 protected void printInCDefPro () {48 System. out. println ("default inner class: protected"); 49} 50 51 private void printInCDefPri () {52 System. out. println ("default inner class: private"); 53} 54} 55 56 private class InnerClassPri {57 void printInCPriDef () {58 System. out. println ("private inner class: default"); 59} 60 61 public void printInCPriPu () {62 System. out. println ("private inner class: public"); 63} 64 65 protected void printInCPriPro () {66 System. out. println ("private inner class: protected"); 67} 68 69 private void printInCPriPri () {70 System. out. println ("private inner class: private"); 71} 72}

 

A.According to the characteristics of the access permission modifier, to verify the access permissions of each member's internal classes, you must

1). External class

2). Class of the same package as the external class

3). subclasses of different packages from external classes

4). other classes of packages different from external classes

Add test code to the four types.

Note:

Here isConsider a member's internal class as a member of an external class.That is, the internal class of the member is a whole. For other classes, it is only a variable of the external class. Therefore, the subclass involved here is the subclass of the external class.

 

B.In addition, to verify the access permissions of each member method

1). External class

2). Class of the same package as the member's internal class

3). Subclass of the member internal class that is different from the member internal class (Correction. 2018.1.23)

4). other classes of different packages than the internal classes of members

1>. Other classes are subclasses of external classes.

2>. Other classes are not subclasses of external classes.

Add test code to these five types.

Note:

1]. Here we want to test the member methods of the member internal class, so for case 3, it should be a subclass of the member internal class, rather than a subclass of the external class;

Because the Member internal class is located inside the external class, its subclass cannot be located in other packages.3rd cases do not exist.(This section is incorrect. The subclass of the member's internal class can be located in 2018.1.24 of other packages)

2]. Because the Member internal class is a member of the external class,Its visibility is different for "external class subclass and non-subclass" of "different packages", so case 4 should be subdivided into two situations.

 

The ranges of 1, 2, 3, and 4 in A, B, and A are the same as those of 1, 2, 4.1, and 4.2 in B,Append the test code 3 in B, as shown in the following 5th. (Append: 2018.1.24)

The following figure shows all the test code:

Here, Person is an external class, TestMain is a class of the same package, GateMan is a non-subclass of Person of different packages, and Teacher is a subclass of Person of different packages.

 

1. Test in Person:

 

We can see that:

All member Internal classes are visible to external departments;

All member methods of all member Internal classes are visible to external departments;

 

2. Test in TestMain:

We can see that:

The internal class of the private modifier is invisible to other classes in the same package as the external class;

All member methods of private-modified member Internal classes are invisible to other classes in the same package as internal classes;

Public, protected, and the internal class of the default permission are visible to other classes in the same package as the external class;

The public, protected, and member methods of the default permission are visible to other classes in the same package as the internal class;

The member methods of private permissions of public, protected, and Members with default permissions are invisible to other classes in the same package as internal classes;

 

3. Test in GateMan:

We can see that:

The internal class of the public modifier is visible to non-Child classes of different packages from the external class;

The member method of the public permission of the member's internal class modified by public is visible to non-Child classes of different packages of the internal class;

The member methods for other permissions of the member's internal class modified by public are invisible to non-Child classes of different packages of the internal class;

The internal classes of protected, default, and private modifier members are invisible to non-Child classes of different packages from external classes;

All member methods of the protected, default, and private-modified member Internal classes are invisible to non-subclasses of different packages of the internal classes --- class invisible, and its member methods are invisible;

 

4. Test in Teacher:

 

Note that 28 rows indicate different errors from other rows:

That is, the constructor of the InnerClassPro class of the member modified by protected is invisible.

But there are no problems with the 42nd rows, that is, inCPro. printInCProPu (); that is, InnerClassPro can be used in Teacher.

Why is there such a conflict?

 

Analysis:

The class Teacher and class Person are not in the same package, but are child classes of Person. Therefore, the internal class InnerClassPro modified by protected is visible to Teacher;

However, the class InnerClassPro does not explicitly create a constructor, so its default constructor is also protected;

For the internal class InnerClassPro, the class Teacher is not in the same package and is not its subclass. Therefore, the Teacher cannot see the protected-modified constructor;

Therefore, the above error occurs.

 

Countermeasure:

Add a constructor to the class InnerClassPro and declare it as public,This ensures that the Teacher can see its constructor and correctly create the InnerClassPro object.

 

The Code is as follows:

Protected class InnerClassPro {// The constructor is public InnerClassPro (){}...}

 

After modification, the above problems will not occur. See:

 

 

The complete test code in the Teacher class is as follows and will not be analyzed one by one:

 

 

5. Different packages of InnerClassChild2 and internal classes

InnerClassChild2 must inherit the internal class of a member, as shown in, onlyThe internal class of the public modifier is visible in other packages.That is to say, only the Members with the public permission can have sub-classes in other packages.

 

InnerClassChild2 inherits the internal class Person. InnerClassPu, as shown in:

Create a subclass object inCCh,The member Methods printInCPuPu and printInCPuPro are both visible;Because InnerClassChild2 is a subclass and has different packages, the member Methods Modified by public and protected in the parent class are visible. The default and private member methods are invisible;

Create the parent class Object inCPu. Only the member method printInCPuPu is visible;

Note 10 also showed this phenomenon. The following section is taken from Note 10:

If the Teacher Class Object is defined in the Teacher Class Object of a different package than the Person class, this object can be used to access and call the member variables and member Methods Modified by public and protected in the Person class, the member variables and member Methods Modified by default and private cannot be accessed or called;

If the Person class object is defined, only the member variables and member Methods Modified by public in Person can be accessed and called through this object, member variables and member Methods Modified by protected, default, and private cannot be accessed or called.

Question: I don't know why there is such a difference.

 

 

Summary:

Through the above five tests, we can see that:

1). When the access permission modifier modifies members of the internal class and the internal class of the member, the access permission modifier follows the same rules as when modifying members of the general class (see note 10 ).

2) the visibility of the member methods of the internal class is jointly determined by the modifier of the internal class and the modifier of the member methods.

 

Appendix:

The visibility of the 16 cases is listed in sequence, which is special:

1). The public modifier's internal class InnerClassPu protected member method;

2). The public Member method of the internal class InnerClassPro modified by protected;

The reason has been explained in detail above and will not be repeated.

(Note: The figure is a little large. You can click it to view it)

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.