简体   繁体   English

使用任意数量的类

[英]Using arbitrary number of classes

I would appreciate any help in solving the following question.对于解决以下问题,我将不胜感激。

Design and implement a subclass of GenericOrder called ComputerPartyOrder that takes an arbitrary number of different classes of ComputerPart objects, Peripheral objects, Cheese objects, Fruit objects and Service objects.设计并实现一个名为 ComputerPartyOrder 的 GenericOrder 子类,它采用任意数量的不同类的 ComputerPart 对象、Peripheral 对象、Cheese 对象、Fruit 对象和 Service 对象。

here is the code for Product class and GerericOrder class.这是产品 class 和 GerericOrder class 的代码。

abstract class Product {
    protected float price;

    // return the price of a particular product
    abstract float price();

    //public getType() {
    //
    //}
}

//------------------------------------------------------------

class ComputerPart extends Product {
    public ComputerPart(float p) {
    price = p;
    }

    public float price() { return price; }
}

class Motherboard extends ComputerPart {
    protected String manufacturer;
    public Motherboard(String mfg, float p) {
    super(p);
    manufacturer = mfg;
    }
    public String getManufacturer() { return manufacturer; }
}

class RAM extends ComputerPart {
    protected int size;
    protected String manufacturer;
    public RAM(String mfg, int size, float p) {
    super(p);
    this.manufacturer = mfg;
    this.size = size;
    }
    public String getManufacturer() { return manufacturer; }
}

class Drive extends ComputerPart {
    protected String type;
    protected int speed;
    public Drive(String type, int speed, float p) {
    super(p);
    this.type = type;
    this.speed = speed;
    }
    public String getType() { return type; }
    public int getSpeed() { return speed; }
}


class Peripheral extends Product {
    public Peripheral(float p) {
    price = p;
    }
    public float price() { return price; }
}

class Printer extends Peripheral {
    protected String model;
    public Printer(String model, float p) {
    super(p);
    this.model = model;
    }
    public String getModel() { return model; }
}

class Monitor extends Peripheral {
    protected String model;
    public Monitor(String model, float p) {
    super(p);
    this.model = model;
    }
    public String getModel() { return model; }
}

class Service extends Product {
    public Service(float p) {
    price = p;
    }
    public float price() { return price; }
}

class AssemblyService extends Service {
    String provider;
    public AssemblyService(String pv, float p) {
    super(p);
    provider = pv;
    }
    public String getProvider() { return provider; }
}

class DeliveryService extends Service {
    String courier;
    public DeliveryService(String c, float p) {
    super(p);
    courier = c;
    }
    public String getCourier() { return courier; }
}

//-------------------------------------------------------
class Cheese extends Product {
    public Cheese(float p) {
    price = p;
    }
    public float price() { return price; }
}

class Cheddar extends Cheese {
    public Cheddar(float p) {
    super(p);
    }
}
class Mozzarella extends Cheese {
    public Mozzarella(float p) {
    super(p);
    }
}

class Fruit extends Product {
    public Fruit(float p) {
    price = p;
    }
    public float price() { return price; }
}
class Apple extends Fruit {
    public Apple(float p) {
    super(p);
    }
}
class Orange extends Fruit {
    public Orange(float p) {
    super(p);
    }
}

GenericOrder:通用顺序:

import java.util.ArrayList;
import java.util.List;

public abstract class GenericOrder<T> extends Product {
    private static long counter = 1;
    private final long id = counter++;
    private List<T> Item;

    public GenericOrder() {
        Item = new ArrayList<T>();
    }

    public long getid() {
        return id;
    }

    public void addItem(T newItem) {
        Item.add(newItem);
    }

    public List<T> getItem() {
        return Item;
    }

    public void setItem(List<T> Item) {
        this.Item = Item;
    }
}

EDIT: Code so far编辑:到目前为止的代码

public abstract class ComputerPartyOrder extends GenericOrder {
    GenericOrder GOrder = new GenericOrder() {
        @Override
        float price() {
            return 0;
        }
    };

    public void input(Product newitem) {
        GOrder.addItem(newitem);
    }

    public void output() {
        System.out.println(GOrder.getItem());
    }
}

You have the right idea, but GenericOrder does not need a type parameter T .您有正确的想法,但GenericOrder不需要类型参数T Instead, you can set the type of Item to Product (the superclass of all the different types of products).相反,您可以将Item的类型设置为Product (所有不同类型产品的超类)。

public abstract class GenericOrder extends Product {
    private static long counter = 1;
    private final long id = counter++;
    private List<Product> Item;

    public GenericOrder() {
        Item = new ArrayList<Product>();
    }

    public long getid() {
        return id;
    }

    public void addItem(Product newItem) {
        Item.add(newItem);
    }

    public List<Product> getItem() {
        return Item;
    }

    public void setItem(List<Product> Item) {
        this.Item = Item;
    }
}

You will still be able to call addItem with any instance of a subclass of Product .您仍然可以使用Product子类的任何实例调用addItem

I would also suggest renaming Item to item , uppercase names are usually used for types, not variables.我还建议将Item重命名为item ,大写名称通常用于类型,而不是变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM