首页 > 解决方案 > 将 Part 对象添加到 observableArrayListJava 中 Product 类中的 associatedParts

问题描述

我正在做我的课堂作业,我被困住了。

我有两个类:Part,它是抽象的,具有扩展 Part 的 InHouse 和 Outsourced 类。然后我有 Product,它奇怪地有一个 observableArrayList 的部件,称为 associatedParts。

我正在处理我的 AddProductController,试图调用Product类中的方法addAssociatedPart()。我的问题是编译器在 Part 中找不到方法。如果我投射到 InHouse,它在 InHouse 中找不到方法,依此类推。我不能使用静态方法,因为addAssociatedPart()根据 UML 设计,该方法应该是非静态的。所以,我不能明确告诉它在 中找到它Product.addAssociatedPart(),因为它告诉我我不能引用非静态等。

下面是从 Product 类开始的代码片段。

public class Product {
private ObservableList<Part> associatedParts = FXCollections.observableArrayList();
private int id;
private String name;
private double price;
private int stock;
private int min;
private int max;

public void addAssociatedPart(Part part) {

   getAllAssociatedParts().add(part);
}

public ObservableList<Part> getAllAssociatedParts() {
    return this.associatedParts;
}

然后是 AddProductScreenController 类:

public class AddProductScreenController implements Initializable {

@FXML
public void onAddProductAddPart(ActionEvent event) {

    // this is triggered when the Add button is clicked

    Part selectedItem = addProductTableViewAll.getSelectionModel().getSelectedItem();
    selectedItem.addAssociatedPart(); // can't find method
    Product.selectedItem.addAssociatedPart(); // can't find variable selectedItem (obviously bad formatting)
    selectedItem.Product.addAssociatedPart(); // can't find variable Product (again bad formatting)
    addAssociatedPart(selectedItem); // can't find method addAssociatedPart()
    Product.addAssociatedPart(selectedItem); // non-static method, can't be referenced from a static context

    InHouse newPart = new InHouse(1, "test", 1.99, 1, 1, 1, 101);
    addAssociatedPart(newPart); // can't find method
    Product.addAssociatedPart(newPart); // non-static method
    newPart.addAssociatedPart(); // can't find method


    addProductTableViewPartial.setItems(associatedParts);
}
}

要求的零件代码:

public abstract class Part {
private int id;
private String name;
private double price;
private int stock;
private int min;
private int max;


public ObservableList<Part> allParts = FXCollections.observableArrayList();

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

public void setId(int id) {
    this.id = id;
}
public int getId() {
    return this.id;
}

public void setName(String name) {
    this.name = name;
}
public String getName() {
    return this.name;
}

public void setPrice(double price) {
    this.price = price;
}
public double getPrice() {
    return this.price;
}

public void setStock(int stock) {
    this.stock = stock;
}
public int getStock() {
    return this.stock;
}

public void setMin(int min) {
    this.min = min;
}
public int getMin() {
    return this.min;
}

public void setMax(int max) {
    this.max = max;
}
public int getMax() {
    return this.max;
}

}

这是内部

 package model;



public class InHouse extends Part {
    private int machineId;

    public InHouse(int id, String name, double price, int stock, int min, int max, int machineId) {
        super(id, name, price, stock, min, max);
        this.machineId = machineId;
    }



    public void setMachineId(int machineId) {
        this.machineId = machineId;
    }
    public int getMachineId() {
        return this.machineId;
    }

}

然后外包:

    package model;


public class Outsourced extends Part {

    private String companyName;

    public Outsourced(int id, String name, double price, int stock, int min, int max, String companyName) {

        super(id, name, price, stock, min, max);
        this.companyName = companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }
    public String getCompanyName() {
        return this.companyName;
    }
}

如果您觉得 Java 的某个特定部分需要复习才能理解这一点,我对此持开放态度。我想了解问题,而不仅仅是解决问题。我什至没有在寻找答案,只是指出问题所在的方向。

更新

@Le 和 @Jackson 用他们对他提供的回复的评论为我指明了正确的方向。我需要先有一个产品:

Product product = new Product(1, "test", 1.99, 1, 1, 1);
product.addAssociatedPart(selectedItem);

标签: java

解决方案


在此处输入图像描述

我试图在评论中向您解释各种课程的关联,但我认为我会使用视觉帮助。我已将您的场景简化为经典的 OOP 问题。

public class Product {

    public void addAssociatedPart(Part part) {
        // some stuff
    }
}
public abstract class Part {
}
public class InHouse extends Part {
}
public class Outsourced extends Part {
}
public class Assembly {

    public static void main(String[] args) {
        Product car = new Product();

        Part seat = new InHouse();
        Part engine = new Outsourced();
        Part window = new InHouse();

        car.addAssociatedPart(seat);
        car.addAssociatedPart(engine);
        car.addAssociatedPart(window);
    }
}

Part我的或其子类中没有任何方法可以将自己添加到某些Product. 这是你想要达到的吗?


推荐阅读