Interface as return value type

/**
 * interface as return value type
 * @author Administrator
 *
 */
interface Loves{
	public abstract void loves();
}
class LoveDemos{
	public Loves getLoves(){
		return new TS();//Polymorphism, the parent class reference points to the child class object
	}
}

class TS implements Loves{

	@Override
	public void loves() {
		// TODO Auto-generated method stub
		System.out.println("Learning");
	}
	
}

public class InterfaceReturn {
	public static void main(String[] args){
		LoveDemos ld = new LoveDemos ();
		Loves l = ld.getLoves();//new TS();Love l = new TS();多态
		l.loves();
	}
}

Guess you like

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