What happens when accessing a modifier in the modifier with protected?

xiaoxiao2021-03-06  20

// in SuperClass.javapackage com.duwei.SuperClass; class SuperClass {protected class InnerClass {// source of the error}} // in SubClass.javapackage com.duwei.SubClassimport com.duwei.SuperClass *;. Class SubClass extends SuperClass {public Void func () {innerclass ic = new innerclass (); // causes an error}}

It's not a very deep friend who is not a very profound friend who is not a very profound friend who will feel that this is very normal. In fact, the program behavior will be wrong when the program is compiled, the error message is innerclass () HAS protected access in COM .duwei.superclass.innerclass. This is why? Inner class innerclass in my superclass class has been defined as protected, and Java guarantees that the things modified by Protected can be used in their subclasses !!! But Please take a closer look at the contents of the error message. It is not what is innerclass class, but the innerclass () method in the InnerClass class has a problem. InnerClass () This is not a constructor of the InnerClass class !!! The problem found half, we know when learning Java, if a class does not define any form of constructor, the Java compiler will automatically add one, but the problem is here, many people will not pay attention to this automatic Why is the access level of the constructor to be added! But here mentioned that everyone else thinks, in fact, the access level of the automatically coupled constructor is the same as the class! We are going back to the question just now At this time, we will find that the original InnerClass class does not write constructor when defined. The interface is the same as the class, which is the same, which is protected. At this time, I think everyone should understand why the program will have problems. !!! The problem when the InnerClass class was instantiated in the func () method in the Subclass class. Of course, from the access level of InnerClass to protected by protected, but instantiation has to call the constructor, that is When calling the constructor of the InnerClass class, since this compiler is automatically added with the constructor along the access level of the InnerClass class is protected, this constructor can only be called in the subclass of InnerClass or in the same package. At this time, it is simply simple to instantiate the InnerClass class, and there is no subclass and in another package, and then Java only allows the internal class in the same class to inherit another internal class, and this operation Allowed scope only, so the problem is finally going to fall, because the constructor of the InnerClass class only allows other subclass calls without allowing other forms of call, so there is no call to the InnerClass class. Summary object. Find this problem. Solve the problem is also very simple, just define a PUBLIC access level in the InnerClass class. This can be seen that some of the characteristics of sometimes the program It is a deep hidden, if you don't want to find it, it is hard to find it.

Since the person is very poor and the professional level is limited, the individual mistakes are inevitable. If there is, please point out. Thank you !!!

转载请注明原文地址:https://www.9cbs.com/read-40562.html

New Post(0)