java定义计算机类并模拟其操作
/*** 定义计算机类并模拟其操作*/
public class Computer {//成员变量private String cpu = "Intel"; // cpuprivate String memory; //内存private String mainBoard; //主板private String keyBoard; //键盘//构造方法public Computer(){System.out.println("-----Computer-----");cpu = "AMD";}public Computer(String cpu,String memory,String mainBoard,String keyBoard){this.cpu = cpu;this.memory = memory;this.mainBoard = mainBoard;this.keyBoard = keyBoard;}//成员方法public void start(){System.out.println("-----starting-----");}public void close(){System.out.println("-----closing-----");}public void show(){System.out.println("cpu"+cpu+",memory"+memory+",mainBoard"+mainBoard+",keyBoard"+keyBoard);}public static void main(String[] args) {Computer computer = new Computer();Computer computer2 = new Computer("酷睿","三星","华硕","罗技");computer.start();computer.show();computer.close();System.out.println("=============================");computer2.start();computer2.show();computer2.close();}}
执行代码如下: