Use of inner classes and anonymous inner classes

inner class

1. Inner classes can share properties and methods of outer classes

2. The outer class encapsulates the inner class

3. When the inner class has the same name as the outer class property or method, it must be prefixed when using the outer class property or method in the inner class: outer class.this.

4. The access type of the inner class defined outside the method body can be public, protect, default, private, etc. 4 types, just like the member variables defined in the class have 4 access types, they determine the definition of this inner class Whether it is visible to other classes; at this time, we can also create an instance object of the inner class outside. When creating an instance object of the inner class, we must first create an instance object of the outer class, and then use the instance object of the outer class to create the inner class. instance object of

public class Inner {

	public static void main(String[] args) {
		
		Outer outer = new Outer(5,10);
		outer.test(); //6,11
		System.out.println("Inner.times1 = "+outer.inner.times1);
		
		//Create an inner class object (inner here is a new object, not the member variable inner in the outer class)
		Outer.Inner inner = outer.new Inner();
		inner.timesInc();
		System.out.println("Inner.times1 = "+inner.times1);

	}//main

}

//external class
class Outer {
	private int times;
	private int times1;
	Inner inner; //Inner classes are usually only used inside outer classes
	//Construction method
	public Outer(int times,int times1){
		this.times = times;
		this.times1 = times1;
		inner = new Inner(); //Create inner class object
		inner.timesInc(); //Method call to add 1 to times
	}
	// method to display private properties
	public void test(){
		System.out.println("Outer.times = "+times);
		System.out.println("Outer.times1 = "+times1);
	}
	//inner class
	class Inner{
		int times1 = 1;
		public void timesInc(){
			times++; //Inner classes can share properties and methods of outer classes
			times1++; //times1 in the inner class is added by 1
			Outer.this.times1++; //times1 in the outer class is added by 1
		}
		
	}
}

anonymous inner class

1. Must have a parent class

2. An anonymous inner class creates a subclass object while declaring a subclass

3. The syntax is concise and easy to use
new supertype() {};
Invoke the constructor Subclass class body

4. For concise inheritance of abstract classes, or to implement interfaces

5. When an anonymous inner class shares a local variable in an outer method, the local variable must be final modified

public class NoInner {

	public static void main(String[] args) {
		
		final int a = 10;
		Yoo yoo = new Yoo(){}; //Create an anonymous inner class---the subclass object is obtained, and the subclass has no name
		Yoo yoo1 = new Yoo(); //Create a Yoo object---the parent class object
		Xoo xoo = new Xoo(){}; //Create anonymous inner class---what you get is the implementation class object, the implementation class has no name
		
		Goo goo = new Goo(){
			public void test(){
				System.out.println("test, a="+a);
			}
		};
		goo.test();
	}//main
}
class Yoo{}
Xoo interface {}
interface Goo{
	void test();
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325441770&siteId=291194637