有没有一种创建对象的方法,其中对象类由另一个变量定义?

时间:2020-01-18 03:07:17

标签: java object if-statement

我有两个类具有相同的方法,下面的方法应根据打勾的复选框访问两个类之一的方法,但是我在将IF语句中创建的对象传递到主体时遇到了问题该方法。有没有办法在不将方法主体复制到两个区域的情况下实现这一目标?这是代码:

public void populateSupplyChainTextFields() {

    if (jDeliveryCheckBox.isSelected() == true) {
        DeliveryCompany supplyChainMember = new DeliveryCompany();
        supplyChainMember = theDeliveryCompanyList.Find(jSupplyChainSearchBox.getSelectedItem().toString());
    } else {
        Supplier supplyChainMember = new Supplier();
        supplyChainMember = theSupplierList.Find(jSupplyChainSearchBox.getSelectedItem().toString());

    }

    jCategoryTextField.setText(supplyChainMember.getCategory());
    jPriceTextField.setText(String.valueOf(supplyChainMember.getPrice()));
    jUnitCostTextField.setText(String.valueOf(supplyChainMember.getUnitCost()));

编辑**

根据Salim的回复,这是我收到的错误消息: enter image description here

为确认起见,方法getBusinessName()既在供应商类中又在交付公司类中。

如果这是一个新手问题,我深表歉意,我对Java还是很陌生,对此表示感谢。

2 个答案:

答案 0 :(得分:0)

最好从通用接口或抽象类继承DeliveryCompany和Supplier。如果这不起作用,则可以使用Object类型作为最后的选择。

    public Object populateSupplyChainTextFields() {
    Object result = null;

        if (jDeliveryCheckBox.isSelected() == true) {
            DeliveryCompany supplyChainMember = new DeliveryCompany();
            supplyChainMember = theDeliveryCompanyList.Find(jSupplyChainSearchBox.getSelectedItem().toString());
    result = supplyChainMember
        } else {
            Supplier supplyChainMember = new Supplier();
            supplyChainMember = theSupplierList.Find(jSupplyChainSearchBox.getSelectedItem().toString());
    result = supplyChainMember
        }


    return result;

    }
//Call the method
Object r = populateSupplyChainTextFields();

//Check type and take action
if (r instanceof DeliveryCompany){
    DeliveryCompany supplyChainMember = (DeliveryCompany)r;

        jCategoryTextField.setText(supplyChainMember.getCategory());
        jPriceTextField.setText(String.valueOf(supplyChainMember.getPrice()));
        jUnitCostTextField.setText(String.valueOf(supplyChainMember.getUnitCost())
}

答案 1 :(得分:0)

许多人在这里提到,最好的办法是使SupplierDeliveryCompany实现注释接口或扩展公共基类。

如果您确实无法做到这一点,并且确实需要避免重复执行代码块,并且如果实际代码比发布的代码复杂,那么您可以让代码块共享一个变量

使用本地类来保存数据

public void populateSupplyChainTextFields() {

    class Holder {
        String category, price, unitCost;
        Holder(String category, double price, double unitCost){
            this.category = category; 
            this.price = String.valueOf(price); 
            this.unitCost = String.valueOf(unitCost);
        } 
    }

    Holder data;

    if (jDeliveryCheckBox.isSelected()) {
        DeliveryCompany supplyChainMember = new DeliveryCompany().Find(jSupplyChainSearchBox.getSelectedItem().toString());
        data = new Holder(supplyChainMember.getCategory(), supplyChainMember.getPrice(), supplyChainMember.getUnitCost());
    } else {
        Supplier supplyChainMember = new Supplier().Find(jSupplyChainSearchBox.getSelectedItem().toString());
        data = new Holder(supplyChainMember.getCategory(), supplyChainMember.getPrice(), supplyChainMember.getUnitCost());
    }

    jCategoryTextField.setText(data.category);
    jPriceTextField.setText(data.price);
    jUnitCostTextField.setText(data.unitCost);

使用本地类只是我的偏爱,您可以选择使用诸如地图之类的东西(当然,引入记录会很方便):

Map<String, String> data;

if (jDeliveryCheckBox.isSelected()) {
    DeliveryCompany supplyChainMember = ...
    data = Map.of("category, supplyChainMember.getCategory(), 
                  "price", String.valueOf(supplyChainMember.getPrice()), 
                  "unitCost", String.valueOf(supplyChainMember.getUnitCost()));
} else {
    Supplier supplyChainMember = ...
    data = Map.of("category, supplyChainMember.getCategory(), 
                  "price", String.valueOf(supplyChainMember.getPrice()), 
                  "unitCost", String.valueOf(supplyChainMember.getUnitCost()));
}

jCategoryTextField.setText(data.get("category"));
jPriceTextField.setText(data.get("price"));
jUnitCostTextField.setText(data.get("unitCost"));