創建對象數組,給數組賦值(兩種理解思路)
class Student{ String name; int age; } public class StudentTest{ Student []stu=new Student[5];//創建學生對象數組(其中的元素是類的一個對象) Student demo=new Student(); //創建一個學生類的對象 demo.name="張三"; demo.age=18; //給對象的屬性賦值 stu[0]=demo; //將對象demo賦值給對象數組的一個位置的值 System.out.println(stu[0].name) }
class Student{
String name;
int age; } public class StudentTest{ Student []stu=new Student[5]; //創建學生對象數組(其中的元素是類的一個對象)
stu[0]=new student; //實例化 stu[0].name="張三"; //給元素(一個對象)的屬性賦值
stu[0].age=18;
System.out.println(stu[0].name) }