Article directory
- inner class
- 1. Overview of inner classes
- 2. Local inner class
- 3. Member inner class
-
- Precautions:
-
- 1. Cannot
- 2. How to instantiate an object of a member inner class
- 3. In a member inner class, if a member variable with the same name as the outer class is included, how to access it in the member inner class
- 4. How to inherit inner classes (just understand)
- 5. When a class has an inner class, the bytecode file (just understand)
- 4. Static inner class
- 5. Anonymous inner classes
inner class
1. Overview of inner classes
An inner class refers to a class defined inside another class, and the class is called an inner class. The class name does not need to be the same as the source file name .
Types of inner classes:
In Java, there are two situations in which inner classes are used:
(1). Define a class in a class (member inner class, static inner class);
(2). Define a class in a method (local inner class, anonymous inner class).
2. Local inner class
Local inner class (local inner class): A class defined in a method.
It can only be used in the current method, and cannot be used outside the method.
public class TestDemo {
public void func() {
class In{
private int a;
public String str;
}
//局部内部类 缺点 :只能在当前方法中使用
}
}
3. Member inner class
A member inner class is also called an instance inner class, which can be regarded as a member of an ordinary instance of the outer class.
class OuterClass {
public int data1 = 1;
private int data2 = 2;
public static int data3 = 3;
// 实例内部类
class InnerClass{
public int data4 = 4;
private int data5 = 5;
public static final int data6 = 6;
public InnerClass() {
System.out.println("不带参数的内部类的构造方法");
}
public void test() {
System.out.println("InnerClass::test()");
}
}
public void func(){
System.out.println("OuterClass::func1()");
}
}
Precautions:
1. Cannot
If you have to define it, you can only define one, static constant. (static final)
2. How to instantiate an object of a member inner class
外部类名.内部类名 变量 = 外部类对象的引用.new 内部类();
3. In a member inner class, if a member variable with the same name as the outer class is included, how to access it in the member inner class
4. How to inherit inner classes (just understand)
5. When a class has an inner class, the bytecode file (just understand)
OuterClass$InnerClass
外部类$内部类.class
4. Static inner class
class OuterClass {
public int data1 = 1;
private int data2 = 2;
public static int data3 = 3;
// 静态内部类
static class Innerclass{
public int data4 = 5;
private int data5 = 5;
private static int data6 = 6;
public void test(){
System.out.println("InnerClass::test()");
}
}
}
Precautions:
1. How to instantiate an object of a static inner class
外部类名.内部类名 变量 = new 外部类名.内部类名();
2. In a static inner class, how to access the member variables of the outer class
5. Anonymous inner classes
class Out{
public void test(){
System.out.println("hello!");
}
}
public class Test {
public static void main(String[] args) {
new Out(){
};
}
}
The anonymous inner class calls the test() method.
The anonymous inner class calls the overridden test() method