👉 "About the author"
As we all know, life is a long process, constantly overcoming difficulties and constantly reflecting on the process of progress. In this process, there will be a lot of questions and thoughts about life, so I decided to share all my thoughts, experiences and stories in order to find resonance!!!
Focus on Android/Unity and various game development skills, as well as various resource sharing (website, tools, materials, source code, games, etc.)
If there is any need to welcome private self, exchange group, so that learning is no longer lonely.
👉 "Premise"
👉 "Practice process"
In object-oriented programming, nested classes can define another class inside one class
There are four forms:
- Static inner class
- Member inner class
- Local inner class
- Anonymous inner class (key)
Advantages of internal classes:
The internal class object can access the implementation of the object that created it, including private data;
The inner class is not seen by other classes in the same package and has good encapsulation;
Using internal classes can easily write event drivers; (for example, the graphical interface programming of android adopts event driven mechanism)
Anonymous inner classes can easily define runtime callbacks;
Internal classes can be easily defined
Commonalities of internal classes:
The internal class is still an independent class. After compilation, the internal class will be compiled into an independent. Class file, the class name and $symbol of the external class
Inner classes cannot be accessed in the normal way. The inner class is a member of the outer class, so the inner class can freely access the member variables of the outer class, regardless of the permission modifier
If the internal class is declared static, you can't access the members of the external class casually. The external class can access the static members of the internal class through the class name
😜 Static inner class
The simplest form of an internal class is to add the static keyword when defining a class. It cannot have the same name as an external class
You can only access static members and static methods of external classes, including private static members and methods
It is simply understood that like other static members, you can access it when there is no external class object
The method of generating static internal class objects is as follows:
OutClass.InClass in=new OutClass.InClass();
Example code
class OuterClass{ static class InClass{ public void test(){ //Static inner classes can access static members of external classes //And can only access static System.out.println("method"); } } } class Test { public static void main(String[] args){ OutClass.InClass in=new OutClass.InClass(); in.test(); } }
😜 Member inner class
Member inner classes and static inner classes are similar to non static member variables and static member variables
It can access all variables and methods of its external class
Create an instance of the member's internal class in the external class:
this.new Inclass();
Create an instance of the inner class outside the outer class:
(new Outclass()).new Inclass();
To access members of an external class in an internal class:
Outclass.this.member
Example code
class Inner1{ private int b=1; private int a=2; // Define a member inner class public class Inner2{ private int a=3; public void doSomething() { // Direct access to external class objects System.out.println(b); System.out.println(a);// If you directly access a, you will access a in the inner class // How do I access a in an external class? System.out.println( Inner1.this.a); } } } public class InnerClassTest { public static void main(String[] args) { // Create an object within a member class // You need to create an instance of the external class first Inner1.Inner2 in = new Inner1().new Inner2(); inner.doSomething(); } }
😜 Local inner class (also known as method inner class)
Class is defined in the method, which is smaller than the scope of the method. It can only be used in the method, and the internal class is used at least
Like local variables, they cannot be modified by public, protected, private and static
Only local variables of type funal in a method can be accessed
😜 Anonymous Inner Class
When a class is used only once, an anonymous inner class is used immediately after definition
The keywords class, extensions, and implements are not used, and there is no constructor.
Anonymous inner classes cannot be public, protected, private or static, and cannot define any static members or methods
Anonymous inner classes implicitly inherit a parent class or implement an interface.
Anonymous inner classes are often used, usually as a method parameter.
Inherited anonymous inner class
public class Car { public void drive(){ System.out.println("a car!"); } public static void main(String[] args) { Car car = new Car(){ public void drive() { System.out.println("b car!"); } }; car.drive(); } }
The result is output: B Car! The Car reference variable does not refer to the Car object, but the object of the Car anonymous subclass
Interface style anonymous inner class.
interface Vehicle { public void drive(); } class Test{ public static void main(String[] args) { Vehicle v = new Vehicle(){ public void drive(){ System.out.println(" a car!"); } }; v.drive(); } }
The above code is very strange. It seems to be instantiating an interface. This is not the case. An interface type anonymous inner class is an anonymous class that implements an interface. And only one interface can be implemented.
Parameterized anonymous inner class.
class Bar{ void doStuff(Foo f){ f.foo(); } } interface Foo{ void foo(); } class Test{ static void go(){ Bar b = new Bar(); b.doStuff(new Foo(){ public void foo(){ System.out.println("foofy"); } }); } }
👉 "Others"
📢 Author: Xiaokong and Xiaokong in Xiaozhi
📢 Reprint instructions - be sure to indicate the source: https://zhima.blog.csdn.net/
📢 Welcome to praise 👍 Collection 🌟 Leaving a message. 📝