What is an internal class? The inner class is the class, and there are classes inside the class, which is called the inner class. An inner class can declare a class inside a class or declare an interface. 8.10 is shown below:
650) this.width=650; "src=" Http://img.mp.itc.cn/upload/20161021/3fa508f846a749118fb3733c17639ccd.jpg "style=" border:0px;margin:0px;padding:0px;font-size:0px; "/>
Figure 8.10
Why use internal classes? Inner class from the point of view of the code, the inner class just simplifies the operation of the code, but he does not have much of a feature, if the need for a class, do not let others use, then this class can be what is inside. Inner classes are not very useful in object-oriented thinking just for the sake of quickness, which is the use of inner classes.
Internal classes A lot of people are simply declaring the inner class, the Java rule is that a file only put a class on the line, but have to write multiple classes in the file so do not violate his rules his rule is that a file only allows a class with public, And with public, the file name must be the same as the class.
Inner class He and the class can also have new objects that can invoke methods, but there is a rule inner class divided into 2 types: static inner classes and internal classes of instances.
If static is added, this class is a static inner class. If there is no static, then it is the inner class of the instance. The inner class can be seen as a property, the property has static and instance member if it is static then the static method inside of him can be accessed by static class. If it is an instance then it can only be accessed with the object. 8.101 is shown below:
650) this.width=650; "src=" Http://img.mp.itc.cn/upload/20161021/e8246234148844f48071007ee7e20f46_th.jpg "style=" border:0px;margin:0px;padding:0px;font-size:0px; "/>
Figure 8.101
If there is a static inner class, he has a private method inside, but the outside class can access the private method inside of him, because the inner class is the class of the abdomen, and any member of the class in the abdomen can access each other.
Just like pregnant women with a child, all of his nutrition is provided by the mother. So he's completely accessible to private members of the inner class. 8.102 is shown below:
650) this.width=650; "src=" Http://img.mp.itc.cn/upload/20161021/c046a55c58e34c8ab48969fdfc4c680e.jpg "style=" border:0px;margin:0px;padding:0px;font-size:0px; "/>
Figure 8.102
This is not allowed if the external static method accesses the inner class of the instance, because he is an instance, and the instance must be an instance before it can be called. The instance inner class does not allow for static methods and static properties, while static inner classes can request the properties of the instance's methods and instances.
Access Methods for internal classes:
A static method of an outer class cannot generate any access to an instance's inner class, only the new object. An instance method of an outer class can generate access to static and instance internal classes. An outer class can access private members of an inner class.
The static inner class is built out of the way: new external class. Inner class ();
The inner class of the instance is constructed outside: New external Class (). New inner Class ();
Nameless Inner class:
Internal classes also have nameless inner classes, and books or programmers in Android are mostly nameless inner classes. So what is called an anonymous inner class? is an inner class without a name, which is called an unnamed inner class.
So what is the use of nameless inner classes? For example, a subclass inherits the parent class's method, but there is only one method in it, so writing a subclass to inherit the class to rewrite his method feels too troublesome, no need, write the inner class to inherit he also feel a bit of trouble, then we can write a nameless inner class to rewrite this method.
Technology sharing: www.kaige123.com
This article is from the "11247808" blog, please be sure to keep this source http://11257808.blog.51cto.com/11247808/1870618
Java Internal classes