创建一个Computer类,其中包括品牌,价格,内存大小,硬盘大小,J厂商五个属性。 采用setter注入,和构造器注入两种方法,分别创建对象,并输出该对象的toString0(即全部信息)。
时间: 2024-10-10 12:07:37 浏览: 33
首先,我们来创建一个`Computer`类,它包含所需的属性:
```java
public class Computer {
private String brand;
private double price;
private int memorySize;
private int hardDiskSize;
private String manufacturer;
// 构造器注入(通过构造函数)
public Computer(String brand, double price, int memorySize, int hardDiskSize, String manufacturer) {
this.brand = brand;
this.price = price;
this.memorySize = memorySize;
this.hardDiskSize = hardDiskSize;
this.manufacturer = manufacturer;
}
// Setter方法注入(通过getter和setter)
public void setBrand(String brand) { this.brand = brand; }
public void setPrice(double price) { this.price = price; }
public void setMemorySize(int memorySize) { this.memorySize = memorySize; }
public void setHardDiskSize(int hardDiskSize) { this.hardDiskSize = hardDiskSize; }
public void setManufacturer(String manufacturer
阅读全文