创建对象时使用继承

问题描述 投票:0回答:1

我需要一点帮助,我正在写一个程序,根本上是一个小店。我这里有不同的项目,在类下 Content,它看起来像这样。

public abstract class Content {

    // Content Attributes
    private String contentID;
    private String contentName;
    private double applicationPrice;
    private int downloadCount;

    //private Comments[] comments = new Comments[100];
    //private int commentCount= 0;


    // Constructor
    public Content(String ID, String Name, double price) {
        this.contentID = ID;
        this.contentName = Name;
        this.applicationPrice= price;

    }

    public Content(String ID, String Name) {
        this.contentID = ID;
        this.contentName = Name;
        this.applicationPrice= 0;

    }


    // Accessor Methods
    public String getID() {
        return contentID;
    }
    public double getApplicationPrice() {
        return applicationPrice;
    }


    public String getContentName() {
        return contentName;
    }

    public void download() {
        //code to make a download happen
        //++ download

        return;
    }

    public void addComment() {
        //scanner input comment

        //this.comments[commentCount]= my new comment;
    }




}

在内容下面,我有其他的类来扩展它,比如: ApplicationPublication (Publication 进一步扩展为 BookMagazine). 举个例子 Application 看起来是这样的。

import java.util.Scanner;

public class Application extends Content {
    // attributes

    private String osType;

    //constructor

    public Application(String ID, String name, double price, String os) {

        super(ID, name, price);

        this.osType = os;
    }

    public Application(String ID, String name, String os) {
        super(ID, name);
        this.osType = os;

    }


    //accessor methods


    public String getOsType() {
        return this.osType;
    }




    }





我的问题是,我认为我对继承的基本理解有问题。我想了解如何最好地让我的用户创建这些对象。到目前为止,我有一个 MyShop 类,在那里他们可以创建一个应用程序。

import java.util.Scanner;

public class MyShop {
    // instance variables that you need (marked as private)

    // declare a private array to store content here
    private Content[] contentList = new Content[100];
    private int contentCount = 0;

    // declare a private array to store users here

    public MyShop() {

    }



    public void addApplication(){

        Scanner console = new Scanner(System.in);
        String appID;
        String appName;
        double appPrice;
        String appOS;
        int control = 0;

        while(control== 0) {
            try {
                System.out.println("What is the ID of the Application?");
                appID = console.next();

                System.out.println("What is the name of the Application?");
                appName = console.next();

                System.out.println("What is the price of the Application?");
                appPrice = console.nextDouble();

                System.out.println("What is the minimum OS of the Application?");
                appOS = console.next();

                Application newApplication= new Application(appID, appName, appPrice, appOS);
                System.out.println(newApplication.getApplicationPrice());
                contentList[contentCount] = newApplication;
                contentCount++;
                console.close();
                control = 1;
            }catch(Exception e) {
                System.out.println("invalid input try again!"); 
            }

        }


    }




    public void showContent() {
        System.out.println(contentList);
    }


//  public void addUser(....) {
//      // code here
//  }


    // space for other possible methods here
}

有没有一种方法可以让我利用继承来避免有一个... addBook()addMagazine() 除了 addApplication() 而不是用一些通用的东西,如 addContent()?

如果我的问题不清楚,或者需要更多的信息,请告诉我!

java inheritance subclass
1个回答
0
投票

我想你可以用方法做接口 addContent(int mode) 内含3个名字的整数 APPLICATION, BOOK, MAGAZINE 然后将其落实到你的 myShop 类。根据 int mode 你使方法addContent的实现。


0
投票

工厂模式 正是解决了这个问题。你可以创建一个方法。

<T extends Content> void addContent(Class<T> contentClass) {
  if (Application.class.equals(contentClass)) {
  ... 
  } else if ...

  } else {
    throw new RuntimeException("Unknown Content type");
  }
}

或者

void addContent(ContentType contentType) {
  switch (contentType) {
    case APPLICATION:
       ...
    case ...
    default:
      throw new RuntimeException("Unknown Content type")
  }
}
public enum ContentType {
  APPLICATION, BOOK, MAGAZINE;
}

0
投票

重新思考您的模型,它应该与现实世界中的工作方式相匹配。您的店铺 销售 物品,它不是一个制造者。所以商店类永远不会 创造 一个应用程序。它将 卖东西的池子。产品。一个股票。而有些产品是应用,有些是书,有些是杂志,汽车,洗发水什么的。

重命名 ContentStockItem. contentId 将是 itemId, contentName itemName 或者只是 name. 如果一个模型与现实世界相吻合,那么它就更容易(容易得多!)理解。

然后,添加一个 Stock 类,其中有所有的项目(a List<StockItem> items 对于 简单的第一种方法)。) 股票会有一些方法,比如

public StockItem findById(String stockItemId);
public List<StockItem> findAllByName(String name);
public void addItem(StockItem newItem);
public void itemsSold(String stockItemID, int quantity);
//...

现在,如果你想增加一本图书到股票,只需调用

addItem(new Book("Clean Code", "Robert Martin"));

而这本书则是一个库存品。

public class Publication extends StockItem { ... }
public class Book extends Publication { ... }

后来,你会了解到这个简化的模型并不能用于生产,对于真实的商店,有更好的解决方案,但信息将是:从一个好的模型开始。看看真实的世界,描述你所看到的东西,然后完全用它来构建你的类,并添加方法。

© www.soinside.com 2019 - 2024. All rights reserved.