创建对象数组,给数组赋值(两种理解思路)


创建对象数组,给数组赋值(两种理解思路)

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) }
 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM