Application of anonymous inner class in development

/**
 * Application of anonymous inner class in development
 * @author Administrator
 *
 */
interface Person2{
	void study();
}
class PersonDemo2{
	//Define the interface as a parameter, what is needed here is not the interface, but the implementation class object
	public void method(Person2 p){
		p.study();
	}
}
//implementation class
class XueSheng implements Person2{

	@Override
	public void study() {
		// TODO Auto-generated method stub
		System.out.println("Study hard and make progress every day");
	}
	
}
public class InnerClassTest2 {
	public static void main(String[] args){
		PersonDemo2 pd = new PersonDemo2();
		Person2 p = new XueSheng();
		pd.method(p);
		System.out.println("---------");
		//Anonymous inner class implementation
		new PersonDemo2().method(new Person2() {
			
			@Override
			public void study() {
				// TODO Auto-generated method stub
				System.out.println("Study hard and make progress every day");
			}
		});;
	}
}

Guess you like

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