1. Inner classes:
(1) Methods with the same name of the inner class
The inner class can call methods of the outer class. If the inner class has a method with the same name, it must be called using the "OuterClass.this.MethodName()" format (where OuterClass Replace MethodName with the actual external class name and its method; this is the keyword, indicating a reference to the external class); if the internal class does not have a method with the same name, you can directly call the method of the external class.
But peripheral classes cannot directly call private methods of inner classes, and external classes cannot directly call private methods of other classes. Note: The permission of the inner class to directly use the outer class has nothing to do with whether the method is static. It depends on whether the inner class has a method with the same name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
The output result is:
1 2 |
|
(2) Instantiation of inner classes
Instantization of inner classes is different from ordinary classes. Ordinary classes can be instantiated whenever needed, while inner classes must be instantiated after the outer class is instantiated Only then can it be instantiated and establish a relationship with the external class
So in the non-static method in the external class, the inner class object can be instantiated
1 2 3 4 |
|
But in the static method, you need to pay attention! ! ! ! You cannot directly new the inner class in the static method, otherwise an error will occur:
No enclosing instance of type OuterClass is accessible. Must qualify the allocation with an enclosing instance of type OuterClass (e.g. x.new A() where x is an instance of OuterClass).
This is because the static method can be used before the class is instantiated. It is called through the class name. At this time, the dynamic inner class has not been instantiated yet. How to use it? You cannot call something that does not exist. .
If you want to create a new inner class in the Static method, you can declare the inner class as Static
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Of course, the static method is generally not used, but this method is recommended: x.new A(), where x is the external class An instance of OuterClass, A is the inner class Innerclass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
x.new A(), where x is an instance of the outer class OuterClass, A is the class class Innerclass, of course it can be split as follows, so it is obviously clear:
(3) When to use inner classes? The typical situation is that the inner class inherits from a certain class or implements a certain interface, and the code of the inner class operates the object of the outer class that created it. So you can think of inner classes as providing some kind of window into their outer classes.The most attractive reason for using inner classes is that each inner class can independently inherit from an implementation (of the interface), so no matter whether the outer class has inherited an implementation of an (interface), for the inner class All have no effect. Without the ability provided by inner classes to inherit from multiple concrete or abstract classes, some design and programming problems would be difficult to solve. From this perspective, inner classes make the multiple inheritance solution complete. Interfaces solve part of the problem, while inner classes effectively implement "multiple inheritance."
(4) Example of instantiating an inner class in a static method: (The inner class is placed in a static method)
1 2 3 4 5 |
|
There are two more rules about anonymous inner classes:
1) Anonymous inner classes cannot be abstract classes, because when the system creates an anonymous inner class, it will immediately create an object of the inner class. Therefore, anonymous inner classes are not allowed to be defined as abstract classes.2) Anonymous inner classes do not define constructors (constructors). Because anonymous inner classes have no class name, they cannot define a constructor, but anonymous inner classes can define instance initialization blocks.
How to determine the existence of an anonymous class? ? I can't see the name, it feels like it's just an object created by new from the parent class, and there is no name for the anonymous class.
Let’s look at the pseudocode first
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
1 2 3 4 5 6 |
|
运行结果:eat something
可以看到,我们直接将抽象类Person中的方法在大括号中实现了,这样便可以省略一个类的书写。并且,匿名内部类还能用于接口上
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
这里per.speak()是可以正常调用的,但per.say()不能调用,为什么呢?注意Person per = new Person()创建的是Person的对象,而非匿名内部类的对象。其实匿名内部类连名字都没有,你咋实例对象去调用它的方法呢?但继承父类的方法和实现的方法是可以正常调用的,本例子中,匿名内部类实现了接口Person的speak方法,因此可以借助Person的对象去调用。
若你确实想调用匿名内部类的自定义的方法say(),当然也有方法:
(1)类似于speak方法的使用,先在Person接口中声明say()方法,再在匿名内部类中覆写此方法。
(2)其实匿名内部类中隐含一个匿名对象,通过该方法可以直接调用say()和speak()方法;代码修改如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
更多内部类和匿名内部类的用法相关文章请关注PHP中文网!