P188spa
須要的、返回的都是該類的對象
public class Animal{ public void eat(){ System.out.println("eee"); } } public class AnimalOperator{ public void hei(Animal a){ a.eat(); } } public class Test{ Public static void main(String args[]){ //要想用eat 就須要先有個AnimalOperator的對象,再有個Animal對象才行 AnimalOperator ao = new AnimalOperator(); Animal a = new Animal(); ao.hei(a); } }
說的很輕巧。就是抽象類的對象。因爲是抽象類的,所以須要新建一個類用來實例化抽象類,並重寫方法。再多態造對象。
public **abstract** class Animal{ **public abstract void eat();** } public class AnimalOperator{ public void hei(Animal a){ a.eat(); } } //要想用eat 就須要先有個類來實例化Animal 多態後纔來調用 //因而 public class Animaler extend Animal{ @overwrite public void eat(){ System.out.println("eee"); } } public class Test{ Public static void main(String args[]){ Animal a = new Animaler; AnimalOperator ao = new AnimalOperator(); ao.hei(a); } }