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());
   }
}
复制代码
posted @ 2023-02-23 14:03  阿风小子  阅读(74)  评论(0编辑  收藏  举报