java创建自定义类的对象数组

 
 1 public class Student{
 2      static int number = 0;     // 静态变量的访问可以不用创建类的实例就可就可使用< 类名.属性 >的方法访问
 3      String name; // 学生姓名
 4 
 5      Student( ){     // 无参构造函数
 6          System.out.println("创建学生成功。。");
 7          number++;      // 学生数加1
 8      }
 9 
10      public static void main(String [] args){
11          //  主方法开始
12          System.out.println("学生数:"+Student.number);     // 0
13 
14          Student [] s;    // 声明要创建的对象数组
15          s = new Student[2];   // 创建对象数组,为对象数组开辟空间
16          s[0] = new Student();  // 创建数组对象,为数组对象开辟空间
17 
18          s[0].name = "凌小墨";       // 先声明,再创建,之后才能使用
19 
20          System.out.println("学生数:" + Student.number);
21          System.out.println("姓名:"+s[0].name);
22 
23      }
24 }

猜你喜欢

转载自www.cnblogs.com/liuhuijie/p/9051896.html