面向接回编程。要求设计一个广告牌,可以晨示不同公司的广告词。定义一个Advertisement广告接口,包括两个抽家方法showAdvertisement和getCorpName(),定义两个类ACOI(A公司)和BCOrp(B公司 ) 实现Adertisement接口分别实现以上两个抽多万法,最示公司的广告河和區示公司的名称。设计_个 AdvertrserentBord类(广告牌),该关有一个show( Advertisement adver ) 方法,显示某公司的广告 词
时间: 2024-02-26 12:58:12 浏览: 24
好的,根据你的需求,我为你设计了以下代码:
```
// Advertisement 接口
interface Advertisement {
void showAdvertisement();
String getCorpName();
}
// ACorp 类实现 Advertisement 接口
class ACorp implements Advertisement {
public void showAdvertisement() {
System.out.println("Welcome to ACorp. Our products are the best!");
}
public String getCorpName() {
return "ACorp";
}
}
// BCorp 类实现 Advertisement 接口
class BCorp implements Advertisement {
public void showAdvertisement() {
System.out.println("BCorp: Your satisfaction is our top priority!");
}
public String getCorpName() {
return "BCorp";
}
}
// AdvertisementBoard 类
class AdvertisementBoard {
public void show(Advertisement adver) {
System.out.println("Advertisement from " + adver.getCorpName() + ":");
adver.showAdvertisement();
}
}
// 测试代码
public class Main {
public static void main(String[] args) {
AdvertisementBoard board = new AdvertisementBoard();
ACorp aCorp = new ACorp();
BCorp bCorp = new BCorp();
board.show(aCorp);
board.show(bCorp);
}
}
```
以上代码中,我们定义了 Advertisement 接口,其中包含 showAdvertisement() 和 getCorpName() 两个抽象方法。然后我们定义了两个类 ACorp 和 BCorp,分别实现了 Advertisement 接口,实现了 showAdvertisement() 和 getCorpName() 方法。最后,我们定义了 AdvertisementBoard 类,其中有一个 show() 方法,可以显示某个公司的广告词。
在主函数中,我们创建了 AdvertisementBoard 对象和 ACorp、BCorp 对象,并分别调用 show() 方法来显示他们的广告词。