定义一个计算机类MyComputer,它包括CPU类型(String类型)、RAM内存大小(Integer类型)、HD硬盘大小(Integer类型),设计它的构造函数,并设计一个显示函数,建立一个MyComputer对象并调用该显示函数显示。
时间: 2024-02-23 20:01:57 浏览: 42
好的,根据您的要求,我来为您编写一个Java程序。
```
public class MyComputer {
private String cpuType;
private int ramSize;
private int hdSize;
public MyComputer(String cpuType, int ramSize, int hdSize) {
this.cpuType = cpuType;
this.ramSize = ramSize;
this.hdSize = hdSize;
}
public void display() {
System.out.println("CPU类型:" + cpuType);
System.out.println("RAM内存大小:" + ramSize + "GB");
System.out.println("HD硬盘大小:" + hdSize + "GB");
}
public static void main(String[] args) {
MyComputer myComputer = new MyComputer("Intel i7", 8, 512);
myComputer.display();
}
}
```
这个程序定义了一个MyComputer类,包括cpuType、ramSize和hdSize三个属性,以及一个构造函数和一个显示函数。在构造函数中,通过传入的参数初始化类的属性;在显示函数中,将类的属性输出到控制台。在main方法中,创建一个MyComputer对象,调用其display方法输出属性信息。