我正在做功课,并试图将一些值吐出到IntelliJ的表中。我正在关注一个网络研讨会系列,但无法找出我做错了什么。我试图向系统中添加一个部件,但我得到了一个错误,即“无法从静态上下文引用非静态方法addPart(classes.Part)”。沿着,在我的www.example.com文件中,还有三个关于同一区域的其他错误MainApplication.java。
package project.wfc482project;
import classes.InHouse;
import classes.Outsourced;
import classes.Part;
import classes.Inventory;
import classes.Product;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class MainApplication extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(MainApplication.class.getResource("MainMenu.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 320, 240);
stage.setTitle("C482 Inventory Management System");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
// Sample values, Parts, and Products
InHouse inhouse1 = new InHouse(1, "Tire", 10.99, 10, 0, 20, 125);
InHouse inhouse2 = new InHouse(2, "Strut", 12.99, 27, 0, 30, 124);
Outsourced outsourced1 = new Outsourced(3, "Tubing", 7.50, 5, 0, 15, "Super Parts");
Outsourced outsourced2 = new Outsourced(5, "Lens", 3.50, 22, 0, 25, "Auto Glass");
Inventory.addPart(inhouse1);
Inventory.addPart(inhouse2);
Inventory.addPart(outsourced1);
Inventory.addPart(outsourced2);
launch();
// Call static members
}
}
这是Inventory.java页面,我正试图从它拉出来,它从一个www.example.com页面拉出来Parts.java,我马上就在这里吐出来...
package classes;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
public class Inventory {
private static int partId = 0;
private static int productId = 0;
private static ObservableList<Part> allParts = FXCollections.observableArrayList();
private static ObservableList<Product> allProducts = FXCollections.observableArrayList();
public static ObservableList<Part> getAllParts() {
return allParts;
}
public ObservableList<Product> getAllProducts() {
return allProducts;
}
public void addPart(Part newPart) {
allParts.add(newPart);
}
public void addProduct(Product newProduct) {
allProducts.add(newProduct);
}
public static int getNewPartId() {
return ++partId;
}
public static int getNewProductId() {
return ++productId;
}
public Part lookupPart(int partId) {
Part partFound = null;
for (Part part : allParts) {
if (part.getId() == partId) {
partFound = part;
}
}
return partFound;
}
public ObservableList<Part> lookupPart(String partName) {
ObservableList<Part> partsFound = FXCollections.observableArrayList();
for (Part part : allParts) {
if (part.getName().equals(partName)) {
partsFound.add(part);
}
}
return partsFound;
}
public Product lookupProduct(int productId) {
Product productFound = null;
for (Product product : allProducts) {
if (product.getId() == productId) {
productFound = product;
}
}
return productFound;
}
public ObservableList<Product> lookupProduct(String productName) {
ObservableList<Product> productsFound = FXCollections.observableArrayList();
for (Product product : allProducts) {
if (product.getName().equals(productName)) {
productsFound.add(product);
}
}
return productsFound;
}
public void updatePart(int index, Part selectedPart) {
allParts.set(index, selectedPart);
}
public void updateProduct(int index, Product selectedProduct) {
allProducts.set(index, selectedProduct);
}
public boolean deletePart(Part selectedPart) {
if (allParts.contains(selectedPart)) {
allParts.remove(selectedPart);
return true;
}
else {
return false;
}
}
public boolean deleteProduct(Product selectedProduct) {
if (allProducts.contains(selectedProduct)) {
allProducts.remove(selectedProduct);
return true;
}
else {
return false;
}
}
}
部件分类:
package classes;
public abstract class Part {
// Declare fields
private int id;
private String name;
private double price;
private int stock;
private int min;
private int max;
// Declare Constructor
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 Part() {
}
// Setters and Getters
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the price
*/
public double getPrice() {
return price;
}
/**
* @param price the price to set
*/
public void setPrice(double price) {
this.price = price;
}
/**
* @return the stock
*/
public int getStock() {
return stock;
}
/**
* @param stock the stock to set
*/
public void setStock(int stock) {
this.stock = stock;
}
/**
* @return the min
*/
public int getMin() {
return min;
}
/**
* @param min the min to set
*/
public void setMin(int min) {
this.min = min;
}
/**
* @return the max
*/
public int getMax() {
return max;
}
/**
* @param max the max to set
*/
public void setMax(int max) {
this.max = max;
}
}
内部课程:
package classes;
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 int getMachineId() {
return machineId;
}
public void setMachineId(int machineId) {
this.machineId = machineId;
}
}
我试着在这里和Reddit上寻找答案,但我很难理解这一切。
我知道我不能为抽象类创建一个部分,尽管这是网络研讨会告诉我要做的(更有可能的是,这是我的解释,但我转过身来,不理解它)。
1条答案
按热度按时间smdnsysy1#
将方法
Inventory.addPart()
更改为静态方法。对此