java 對象數組

來源:互聯網
上載者:User

標籤:嵌套   類型   get   int   索引   style   對象   定義   static   

數組是參考型別,而類同樣是參考型別,所以如果是對象數組的話表示一個參考型別裡面嵌套其他參考型別。

在前面兩篇都是屬於基礎資料型別 (Elementary Data Type)的資料,但是索引的引用資料類型也同樣可以定義數組,這樣的數組稱為對象數組。

以類為例定義對象數組:

  • 動態初始化:   //開闢之後對象數組的內容都是預設值,即null值
    •   類名稱 對象數組名稱 [ ]  = new 類名稱 [長度] ;
    • 分步完成:
      •   聲明對象數組: 類名稱 對象數組名稱 [ ]  = null ; 
      •   開闢對象數組:對象數組名稱 = new 類名稱 [長度] ;
  • 靜態初始化:
    •   類名稱 對象數組名稱 [ ]  =new 類名稱 [ ]{ 執行個體化對象,執行個體化對象,執行個體化對象 ... ...  }

例子:

  動態產生對象數組:

 1 class Book_demo_1{ 2     private String name ; 3     private double money ; 4  5  6     public Book_demo_1(){ 7     } 8     public Book_demo_1(String n ,double m){ 9         name = n;10         money = m;11     }12 13     public String getName() {14         return name;15     }16     public double getMoney() {17         return money;18     }19 20     public String getInfo() {21         return "書名:" + name + "\t" + "價格" + money;22     }23 24     public void setName(String n ) {25         name = n;26     }27     public void setMoney(int m){28         money = m;29     }30 }31 32 public class 對象數組 {33     public static void main(String[] args) {34         Book_demo_1 book [] = new Book_demo_1[3];   //開闢一個3個長度的對象數組35         book[0] = new Book_demo_1("java開發",66.6);36         book[1] = new Book_demo_1("python",99.9);37         book[2] = new Book_demo_1("php",11.1);38         for (int i = 0 ; i < book.length ; i++) {39             System.out.println(book[i].getInfo());40         }41     }42 }

  靜態初始化:

 1 class Book_demo_1{ 2     private String name ; 3     private double money ; 4  5  6     public Book_demo_1(){ 7     } 8     public Book_demo_1(String n ,double m){ 9         name = n;10         money = m;11     }12 13     public String getName() {14         return name;15     }16     public double getMoney() {17         return money;18     }19 20     public String getInfo() {21         return "書名:" + name + "\t" + "價格" + money;22     }23 24     public void setName(String n ) {25         name = n;26     }27     public void setMoney(int m){28         money = m;29     }30 }31 32 public class 對象數組 {33     public static void main(String[] args) {34         Book_demo_1 book [] = new Book_demo_1[]{35                 new Book_demo_1("java開發",66.6),36                 new Book_demo_1("python",99.9),37                 new Book_demo_1("php",11.1),38         };39         for (int i = 0 ; i < book.length ; i++) {40             System.out.println(book[i].getInfo());41         }42     }43 }

 

  • 對象數組實際是就是將多個對象交給數組統一管理。
  • 對象數組的定義文法,對象數組實際上就等於多個對象

java 對象數組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.