public class BeautifulGirl { String name; public BeautifulGirl(String name){ this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } } |
public interface GiveGift { /** * 送花 */ void giveFlowers(); /** * 送巧克力 */ void giveChocolate(); /** * 送书 */ void giveBook(); } |
public class You implements GiveGift { BeautifulGirl mm ; //美女 public You(BeautifulGirl mm){ this.mm = mm; } public void giveBook() { System.out.println(mm.getName() +",送你一本书...."); } public void giveChocolate() { System.out.println(mm.getName() + ",送你一盒巧克力...."); } public void giveFlowers() { System.out.println(mm.getName() + ",送你一束花...."); } } |
public class HerChum implements GiveGift{ private You you; public HerChum(BeautifulGirl mm){ you = new You(mm); } public void giveBook() { you.giveBook(); } public void giveChocolate() { you.giveChocolate(); } public void giveFlowers() { you.giveFlowers(); } |
public class Client { public static void main(String[] args) { BeautifulGirl mm = new BeautifulGirl("小屁孩..."); HerChum chum = new HerChum(mm); chum.giveBook(); chum.giveChocolate(); chum.giveFlowers(); } } |
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |