A face questions about anonymous inner class

Topics are as follows:

按照要求,补齐代码
	interface Inter { 
		void show(); 
	}
	class Outer { 
		//补齐代码 
	}
	class OuterDemo {
	    public static void main(String[] args) {
		      Outer.method().show();
		  }
	}
要求在控制台输出”HelloWorld”。

This is a small road surface above questions, first main analysis method call statements, Outer.method (), Outer directly behind added a method () method call point described method () method of class Outer in a static method, so we can write down the method method on the code.


Then we continue to look behind the interface Inter calls show () method, the foregoing description is Outer.method () type is the type of Inter, which is the interface type, or else can not call the show () method, so we may be determined method () method should return a value of type Inter.


 

interface Inter{
	void show();
}
 
class Outer{
	public static Inter method(){
		return new Inter(){
			public void show(){
				System.out.println("Hello World");
			};
		};
	}
}
 
class OuterDemo{
	public static void main(String[] args){
		Outer.method().show();
	}
}

 

Published 61 original articles · won praise 9 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_33204444/article/details/98358589