我可以使用类列表生成类的新实例吗?

时间:2019-06-02 23:04:39

标签: java arrays class

我想从类列表中生成类的数组列表。我教科书上的代码是这样的:

产品类别:

abstract class Product {
protected float price;

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

class ComputerPart extends Product {
public ComputerPart(){
}

public ComputerPart(float p) {
price = p;
}

@Override
public float price() { return price; }
}

class Motherboard extends ComputerPart {

protected String manufacturer;

public Motherboard(){     
}
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(){
}
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(){
}

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(){ 
}
public Peripheral(float p) {
price = p;
}
@Override
public float price() { return price; }
}

class Printer extends Peripheral {
protected String model;

public Printer(){    
}
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(){
}

public Service(float p) {
price = p;
}
@Override
public float price() { return price; }
}

class AssemblyService extends Service {
String provider;
public AssemblyService(){

}

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

class DeliveryService extends Service {
String courier;
public DeliveryService(){

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


class Cheese extends Product {
public Cheese(){

}
public Cheese(float p) {
price = p;
}

@Override
public float price() { return price; }
}

class Cheddar extends Cheese {
public Cheddar(){

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

}
public Mozzarella(float p) {
super(p);
}
}

class Fruit extends Product {
public Fruit(){

}
public Fruit(float p) {
price = p;
}

@Override
public float price() { return price; }
}
class Apple extends Fruit {
public Apple(){

}
public Apple(float p) {
super(p);
}
}
class Orange extends Fruit {
public Orange(){

}
public Orange(float p) {
super(p);
}
}

computerorder类为:

public class ComputerOrder<T extends Product> extends GenericOrder<T> {
float price;
String ProductsSpecification;
String ProductsType;
private List<T> products;//the list of items
Random rand = new Random();
int number;


private Class[] types = {ComputerPart.class, Motherboard.class, RAM.class,
    Drive.class, Peripheral.class, Printer.class, Monitor.class, 
Service.class,
    AssemblyService.class, DeliveryService.class,};

private String[] simpleName = {"ComputerPart", "Motherboard", "RAM",
    "Drive", "Peripheral", "Printer", "Monitor", "Service",
    "AssemblyService", "DeliveryService",};

public ComputerOrder() {
    products = new ArrayList<>();
    number = rand.nextInt(99999)+1;
}

public void addProduct(T t) {
        if (t instanceof ComputerPart) {
            products.add(t);    
    }
        if (t instanceof Service) {
            products.add(t);   
   }
        if (t instanceof Peripheral) {
            products.add(t);   
        }
  }


@Override
public void print() {
    System.out.println("Order Number: " + this.number);
    for (int i = 0; i < products.size(); i++) {        
        System.out.println("Item " + (i+1) +" : " + 
(products.get(i).getClass().getSimpleName()) +
                " Price: " +products.get(i).price);
    }
}
public int getNumberOfProducts() {
    return products.size();
}

public List<T> getProducts() {
    return products;
}

public void setProducts(List<T> products) {
    this.products = products;
}

public float getPrice() {
    return price;
}

public void setPrice(float price) {
    this.price = price;
}

public String getProductsSpecification() {
    return ProductsSpecification;
}

public void setProductsSpecification(String ProductsSpecification) {
    this.ProductsSpecification = ProductsSpecification;
}

public String getProductsType() {
    return ProductsType;
}

public void setProductsType(String ProductsType) {
    this.ProductsType = ProductsType;
}

然后我创建了一个DataGenerator类,该类应生成随机的Product对象:

final private Class[] comptypes = {ComputerPart.class, Motherboard.class, RAM.class,
    Drive.class, Peripheral.class, Printer.class, Monitor.class, Service.class,
    AssemblyService.class, DeliveryService.class,};


Product computernext() {
    try {
        return (ComputerPart)
                comptypes [rand.nextInt(comptypes.length)].newInstance();
    }
    catch (IllegalAccessException | InstantiationException e){
        throw new RuntimeException(e);
    }
}

Product computerDataGenerator() {
    Product t = new genericstest.DataGenerator().computernext();
    if (t instanceof RAM) {
        ((RAM) t).manufacturer = generateRAM();
        ((RAM) t).size = intDataGenerator();
        ((RAM) t).price = rand.nextFloat()*100;
    }
    if (t instanceof Motherboard) {
        ((Motherboard) t).manufacturer = generateMotherboard();
        ((Motherboard) t).price = rand.nextFloat()*100;
    }
    if (t instanceof Drive) {
        ((Drive) t).type = generateMotherboard();
        ((Drive) t).price = rand.nextFloat()*100;
    }
    return t;
}

public static void main(String [] args){

     DataGenerator gen = new DataGenerator();

    for (int i = 0; i < 5; i++) {
        System.out.println(gen.computernext());
    }
}

但是当我实现它时,我得到了很多实例化异常。我查看了我的版本和教科书代码,没有看到任何差异,但是有人看到我错过的错误了吗?

1 个答案:

答案 0 :(得分:0)

您没有为我们提供足够的代码来绝对确定问题,但是仅通过阅读您提供的内容,就会由于以下两个原因之一而引发实例化错误:

  • ComputerPart类不是Product的子类。
  • comptypes中包含的一个类不是ComputerPart的子类。

为确保comptypes中包含的每个类都是一个 product ,您应像这样对数组放置类型约束:

final private Class<Product>[] comptypes

这将确保该数组中的每个类都实现或扩展Product

如果您可以为我们提供更多信息,那么我可以为您提供更好的答案。