当前位置: 代码迷 >> J2SE >> 请教这段代码要如何设计
  详细解决方案

请教这段代码要如何设计

热度:69   发布时间:2016-04-24 01:44:46.0
请问这段代码要怎么设计?
Java code
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();    }}



现在的问题就在于在main方法里面,顾客预览了商品后无法决定买不买,要怎么解决这个问题?

------解决方案--------------------
book就是book,不要用其中的某一接口名声明,这样会把book限定死,你的核心问题在这里
  相关解决方案