java 面向接口编程
Advertisement.java
public interface Advertisement { //接口 public void showAdvertisement(); public String getCorpName(); }
AdvertisementBoard.java
public class AdvertisementBoard { //负责创建广告牌 public void show(Advertisement adver) { System.out.println(adver.getCorpName()+"的广告词如下:"); adver.showAdvertisement(); //接口回调 } }
WhiteCloudCorp.java
public class WhiteCloudCorp implements Advertisement { //PhilipsCorp实现Avertisement接口 public void showAdvertisement(){ System.out.println("@@@@@@@@@@@@@@@@@@@@@@"); System.out.printf("飞机中的战斗机,哎yes!\n"); System.out.println("@@@@@@@@@@@@@@@@@@@@@@"); } public String getCorpName() { return "白云有限公司" ; } }
BlackLandCorp.java
public class BlackLandCorp implements Advertisement { public void showAdvertisement(){ System.out.println("**************"); System.out.printf("劳动是爹\n土地是妈\n"); System.out.println("**************"); } public String getCorpName() { return "黑土集团" ; } }
Example6_6.java
public class Example6_6 { public static void main(String args[]) { AdvertisementBoard board = new AdvertisementBoard(); board.show(new BlackLandCorp()); board.show(new WhiteCloudCorp()); } }