interface as a formal parameter

/**
 * interface as a formal parameter
 * @author Administrator
 *
 */
//Define a hobby interface
interface Love{
	public abstract void Love();
}
class LoveDemo{
	public void method(Love l){//l; l = new Teacher(); Love l = new Teacher();
		l.Love();
	}
}
//Define the concrete class to implement the interface
class Teacher implements Love{

	@Override
	public void Love() {
		// TODO Auto-generated method stub
		System.out.println("Teacher loves students");
	}
	
}
public class InterfactTest {
	public static void main(String[] args){
		//Requirement: I want to test the Love method in the LoveDemo class
		LoveDemo ld = new LoveDemo ();
		Love l = new Teacher();
		ld.method(l);
	}
}

Guess you like

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