日期:2014-05-20 浏览次数:20687 次
public class Test { public static void main(String[] args) { Person person = new Person(); IRead read = new Book(); //看了这读物后想买下它 person.read(read); //person.buy(read); } } //读物 interface IRead { void content(); } //商品 interface Goods { void buy(); } //图书 class Book implements IRead,Goods { public void content() { System.out.println("沉思录...."); } public void buy() { System.out.println("我买下这本书"); } } //报纸 class NewPaper implements IRead,Goods { public void content() { System.out.println("今天的头条是....."); } public void buy() { System.out.println("我买十份报纸"); } } class Person { //预览读物 public void read(IRead read) { read.content(); } //买下商品(这是要预览读物后才决定买不买的,不能首先就调用buy方法) public void buy(Goods goods) { goods.buy(); } }