java对象数组
/*** 对象数组*/
public class TestArray {public static void main(String[] args) {//定义一个数组,存储4个分数,并遍历int[] arr;arr = new int[4];arr[0] = 90;arr[1] = 80;arr[2] = 100;arr[3] = 54;for (int score:arr){System.out.println(score);}System.out.println("---------------------------------");Computer[] arr2 = new Computer[4];arr2[0] = new Computer("酷睿","三星","华硕","罗技");arr2[1] = new Computer("Intel","金士顿","技嘉","双飞燕");arr2[2] = new Computer("AMD","三星","华硕","双飞燕");arr2[3] = new Computer("酷睿","金士顿","技嘉","罗技");for (Computer computer:arr2){computer.show();}}}
/*** 定义计算机类并模拟其操作*/
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();}}
执行代码如下: