1.Java中for-each循环为何能放入对象Computer com,不都是放 数据类型 变量(int x) 的吗?
2.打印结果不应该是(一句我打印了,一句cpu吗)呈现四对,8行。为何是我打印了,4行,然后才是CPU4行?求解
package com.newknowledge.object;
public class Computer {
//成员变量
private String cpu = "Intel";//处理器
private String memory;//内存
private String keyBoard;//键盘
private String mainBoard;//主板
//构造方法
public Computer(){
System.out.println("------Computer()-----");
cpu = "AMD";
}
public Computer(String cpu,String memory,String keyBoard,String mainBoard){
System.out.println("我打印了");
this.cpu = cpu;
this.mainBoard = mainBoard;
this.keyBoard = keyBoard;
this.memory = memory;
}
public void show(){
System.out.println("cpu="+cpu+",memory="+memory+
",mainBoard="+mainBoard+",keyBoard"+keyBoard);
}
public static void main(String[] args) {
}
}
package com.newknowledge.object;
import java.util.Arrays;
public class TestArray {
public static void main(String[] args){
Computer[] arr = new Computer[4];
arr[0] = new Computer("AM","2G","达尔优","七彩虹");
arr[1] = new Computer("AM","2G","达尔优","七彩虹");
arr[2] = new Computer("AM","2G","达尔优","七彩虹");
arr[3] = new Computer("AM","2G","达尔优","七彩虹");
for(Computer com:arr){
com.show();
}
}
}