Inner class in JAVA

/*
 * Access rules for inner classes:
 * 1. The inner class can directly access the members in the outer class, including private
 * The reason why you can directly access the outer class members is because the inner class holds a reference to the outer class, format: outer class name.this
 * 2. To access the inner class, the outer class must create an inner class object
 *
 *
 * Inner classes can be modified privately or statically
 * */

/*class Outer {
	private int x = 3;

	class Inner { // inner class
		int x=4;
		void function() {
			int x=6;
			System.out.println("Inner:" + this.x);
			System.out.println("Outer:" + Outer.this.x);
		}
	}

	void method() {
		Inner in=new Inner();
		in.function();
		System.out.println(x);
	}
}

public class InnerClassDemo {
	public static void main(String args[]) {
		//Outer out = new Outer();
		//out.method();
		
		//Direct access to inner class members
		Outer.Inner in =new Outer().new Inner();
		in.function();		
	}
}*/
class Outer {
	private static int x = 3;

	static class Inner { // inner class
		void function() {
			System.out.println("Inner:" + x);
		}
	}
}

public class InnerClassDemo {
	public static void main(String args[]) {
		new Outer.Inner().function();
	}
}

Below is the anonymous inner class

/*
 * Anonymous inner class
 * 1. Anonymous inner class is the short form of inner class
 * 2. The premise of defining an anonymous inner class, the inner class must inherit a class or implement an interface
 * 3. Format of anonymous inner class: new parent class or interface () { define subclass content }
 * 4. In fact, an anonymous inner class is an anonymous subclass object, which can be understood as an object with content
 *
 * */
abstract class AbsDemo {
	abstract void show();
}

class Outer2 {
	int x = 3;

	/*
	 * class Inner extends AbsDemo {
	 *
	 * void show() { System.out.println("show:"+x); } }
	 */

	public void function() {
		// new Inner().show();
		new AbsDemo() {
			void show() {
				System.out.println("show:" + x);
			}

			void abc() {
				System.out.println("abc");
			}
		}.show();// This is the anonymous inner class, which is the simplified green comment section
	}
}

public class InnerClassDemo3 {
	public static void main(String args[]) {
		new Outer2().function();
	}
}

If you want to call the abc() and show() methods, then give the anonymous inner class a name, but note that the method used must be a method in the parent class (that is, polymorphism)

As shown below, note that the AbsDemo class is different from the above

abstract class AbsDemo {
	abstract void show();

	abstract void abc();
}

class Outer2 {
	int x = 3;

	public void function() {
		AbsDemo a1 = new AbsDemo() {
			void show() {
				System.out.println("show:" + x);
			}

			void abc() {
				System.out.println("abc");
			}
		};
		a1.show();
		a1.abc();
	}
}

If there is no abc method in the AbsDemo class, even if the name is given, it cannot be called, and a compilation error will occur.

Then if I want to use the abc method, I can directly write it as     

new AbsDemo(){ void show(){}      void abc(){} }.abc();

Of course, this time has little to do with inheritance. You can write an inner class or function without inheritance, just to give an example.

The following is an exercise, you can try it yourself

interface Inter2{
	void method();
}

class Test2{
	//Complement code, pass anonymous inner class
	
}

public class InnerClassTest {
	public static void main(String args[]){
		Test2.function().method();		
	}
}

The answer is as follows

interface Inter2{
	void method();
}

class Test2{
	//Complement code, pass anonymous inner class
	static Inter2 function(){
		return new Inter2(){
			public void method(){
				System.out.println("abc");
			}
		};
	}
}

public class InnerClassTest {
	public static void main(String args[]){
		Test2.function().method();
		//test.function There is a static method function in the test class
		//.method indicates that the result of the function method operation is an object, and it is an object of type Inter
		//Because only objects of type Inter can call the method method
		
	}
}



                                                                                    ----------------------By chick


Guess you like

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