请用JAVA代码实现以下功能:(1)定义零部件类,(2)增加目录类,属性包括:零部件编号、零部件名称及零部件价格;
时间: 2023-07-14 08:12:03 浏览: 34
以下是用Java代码实现的功能:
零部件类:
```java
public class Component {
private int id;
private String name;
private double price;
public Component(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}
```
目录类:
```java
import java.util.ArrayList;
import java.util.List;
public class Catalog {
private List<Component> components;
public Catalog() {
components = new ArrayList<>();
}
public void addComponent(Component component) {
components.add(component);
}
public List<Component> getComponents() {
return components;
}
}
```
在这个例子中,我们定义了一个零部件类,其属性包括零部件编号、零部件名称和零部件价格。然后,我们定义了一个目录类来存储零部件对象,并且可以添加零部件到目录中。