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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM