對象給類里的屬性賦值
類的屬性封裝 就是 開閉空間數組 然后每個空間都 可以放一個對象產生的值
語法: 類名字[ ] 變量名=new 類名字[大小];
也就是實例化 加個數組形式
列: 類的屬性[] stus=new 類的屬性[2];
輸出屬性數組的方法:
上面已經實例化了所以不用實例化
應為對象把值都封裝一起了放到 屬性數組中了
輸出也一樣需要調用屬性數組 然后用把變量輸出方法寫到 一個類的方法里 一起調出
列:
public class 類的屬性 {//所有屬性
int stuint;
String stuname;
int age;
char sex;
}
public void show() {//輸出屬性數組里的值得方法
System.out.print("姓名"+stuname+"\t");
System.out.print("學號"+stuint+"\t");
System.out.print("年齡"+age+"\t");
System.out.print("性別"+sex+"\t"+"\n");
}
stus=new 類的屬性[2];
for (int i = 0; i < stus.length; i++) {
if (stus[i]!=null) {
stus[i].show(); 調用輸出方法
}
}