抽象類&&介面做形參(其實同理)
阿新 • • 發佈:2018-12-16
- 抽象類:傳入該抽象類的子類物件
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!!"); } }