简体   繁体   English

Java ArrayList 向下铸造

[英]Java ArrayList Downcasting

So I've been working in a project in which I need to have a list to be filled with its new child class objects.所以我一直在从事一个项目,我需要一个列表来填充它的新子 class 对象。

I've made it with traditional method which one ArrayList will be filled with a type of child Class.我用传统方法做到了,其中一个 ArrayList 将填充一种子 Class。 But to make the code shorter and more efficient, I'm considering to downcast the ArrayList to have a single ArrayList of parent class that have both of it's child classes object inside it. But to make the code shorter and more efficient, I'm considering to downcast the ArrayList to have a single ArrayList of parent class that have both of it's child classes object inside it. Is it possible?可能吗?

These are the parent class of Things这些是事物的父 class

 package Model;

public class Things {
    protected String id;
    protected String name;
    protected double price;
    protected int stock;
    protected int bought;

public Things() {}

public Things(String id, String name, double price, int stock) {
    this.id = id;
    this.price = price;
    this.name = name;
    this.stock = stock;
}

public String getId() {
    return id;
}

public String getName() {
    return name;
}

public double getPrice() {
    return price;
}

public int getStock() {
    return stock;
}

public void minusStock(int bought) {
    this.stock = stock - bought;
 }
}

and these are its child class of Handphone and Vouchers这些是手机和优惠券的子 class

Handphone child class手机小孩class

package Model;

public class Handphone extends Things {
    private String color;

    public Handphone(String id, String name, double price, int stock, String color) {
        super(id, name, price, stock);
        this.color = color;
    }

    public String getColor() {
        return color;
    }
}

Voucher child class代金券子class

package Model;

public class Voucher extends Things {
    private double tax;

    public Voucher(String id, String name, double price, int stock, double tax) {
        super(id, name, price, stock);
        this.tax = tax;
    }

    public double getTax() {
        return tax;
    }

    public double getsellingPrice() {
        return (price + (price*tax));
    }
}

Thus to mention the main menu interface will be on different package, I put import Model.* on it.因此要提到主菜单界面会在不同的package上,我把import Model.*放在上面。 Will it also be included inside the menu package if I put it that way?如果我这样说,它是否也会包含在菜单 package 中?

You can put a Handphone and a Voucher into List<Things> ( Thing would perhaps be a more appropriate name?), no need for casting:您可以将HandphoneVoucher放入List<Things>Thing可能是更合适的名称?),无需强制转换:

List<Things> things = new ArrayList<>();
things.add(new Handphone("id", "name", 1, 1, "color"));

However, if you access the list and really need to know if it is a Handphone or a Voucher , you would have to downcast the object.但是,如果您访问该列表并且确实需要知道它是Handphone还是Voucher ,您将不得不向下转换 object。

This type of casting could be sign of a design flaw, so carefully think your solution through.这种类型的铸造可能是设计缺陷的标志,因此请仔细考虑您的解决方案。

If you include a field with protected getter containing the class itself into your parent class, then it will be possible to know at runtime, was it a Headphone, a Voucher or a SomethingElse.如果您将一个包含 class 本身的protected吸气剂字段包含到您的父 class 中,则可以在运行时知道它是耳机、凭证还是其他东西。

Make sure, though, as @Maglinex suggests, that you are not looking at a design flaw.不过,正如@Maglinex 建议的那样,请确保您没有看到设计缺陷。

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

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