Detailed analysis of four internal classes in Java

The main role of the inner class:

  • The inner class provides better encapsulation, the inner class can be hidden in the outer class, and other classes in the same package are not allowed to access the class;
  • The method of the inner class can directly access all data of the outer class, including private data;
  • The functions implemented by the inner class can also be realized by using the outer class, but sometimes it is more convenient to use the inner class.

1. Local inner class

The local inner class is defined in the method of the outer class. When accessing, it can directly access all members of the outer class, but cannot access local variables casually, unless the local variable is modified by final.

//局部内部类
public class LocalInnerClass {
	
	int a = 10;
	public static void main(String[] args) {
		int c = 0;
		class Noo{
			public int add(int a, int b) {
				//局部内部类访问的外部类变量必须是final
				//return a + b + c; 编译错误
				return a + b;
			}
		}
		Noo noo = new Noo();
		noo.add(1, 1);
	}
		

}

Second, the member inner class

The member inner class is the most common inner class, which can unconditionally access all member properties and member methods of the outer class (including private members and static members).

//成员内部类
public class InnerClass {
	
	public static void main(String[] args) {
		Aoo aoo = new Aoo("蔡徐坤");
		Boo boo1 = aoo.new Boo();
		boo1.f();
	}

}

class Aoo{
	String name;
	public Aoo(String name) {
		this.name = name;
	}
	/**
	 * 成员内部类,类似实例变量,依赖外部类
	 * 需要先创建外部类对象Aoo,才能创建Boo对象
	 *
	 */
	class Boo{
		public void f() {
			System.out.println(name);
		}
	}
}

3. Static inner class

The static inner class is decorated with static, and it can only access member variables or methods modified by static in the outer class in terms of access restrictions.

//静态内部类
public class StaticInnerClass {
	
	public static void main(String[] args) {
		//类名直接访问
		Foo.Goo goo = new Foo.Goo();
		goo.f();
		System.out.println(goo.age);
	}

}

class Foo{
	static String name;
	int age;
	/**
	 * 静态内部类-相当于静态变量
	 * 1.类加载时初始化
	 * 2.访问的外部变量必须是static
	 */
	static class Goo{
		int age = 18;
		public void f() {
			//System.out.pritnln(age); 编译错误-->2.
			System.out.println(name);
		}
	}
}

4. Anonymous inner class

Anonymous inner classes are actually shorthand for local inner classes and can only be used once.

  • When using anonymous inner classes, you must inherit from a class or implement an interface, but not both.
  • Constructors cannot be defined in anonymous inner classes.
  • There cannot be any static member variables and static methods in an anonymous inner class.
  • An anonymous inner class is a local inner class, so all restrictions of a local inner class also take effect on an anonymous inner class.
  • An anonymous inner class cannot be abstract, it must implement all the abstract methods of the inherited class or implemented interface.
//匿名内部类
public class AnnoInnerClass {
	
	public static void main(String[] args) {
		/**
		 * 匿名内部类:相当于当前类或接口的子类
		 */
		new Xoo(){  //创建的是Xoo的子类对象
			@Override
			public int add(int a, int b) {
				return a + b;
			}
		};

}


interface Xoo{
	public int add(int a, int b);
}

}

Guess you like

Origin blog.csdn.net/weixin_49851451/article/details/127390330