抽象类&&接口做形参(其实同理)
- 抽象类:传入该抽象类的子类对象
eg:
package javaBasic;
public class TestAbstract {
public static void main(String[] args) {
FreshLad fl = new FreshLad();//想调ta的方法,要先new出来ta先呀~
Lad l = new Fool();//间接实现抽象类
fl.method(l);
}
}
abstract class Lad{
public abstract void need();
}
class FreshLad{
public void method(Lad l) {
l.need();
}
}
class Fool extends Lad{
public void need() {
System.out.println("memorize more words\ncet-6 come on!");
}
}
- 接口:传入该接口的实现类对象
eg如下:package javaBasic; public class TestAbstract { public static void main(String[] args) { /*FreshLad fl = new FreshLad(); Lad l = new Fool();//间接实现抽象类 fl.method(l);*/ FreshYouth fy = new FreshYouth(); Youth y = new Idiot(); fy.method(y); } } interface Youth{ abstract void struggle(); } class FreshYouth{ public void method(Youth y) { y.struggle(); } } class Idiot implements Youth{ public void struggle() { System.out.println("study grammer well\ncet-6 fighting!!"); } }