An array is a reference type, and a class is also a reference type, so if an array of objects represents a reference type, the other reference types are nested inside.
In the previous two articles are data of the basic data type, but the reference data type of the index can also define an array, such an array is called an array of objects.
Define an array of objects as an example of a class:
- Dynamic initialization://The contents of an object array after opening are default values, i.e. null values
- Class name Object array name [] = new class name [length];
- Step through:
- Declaring an Object array: Class name object array name [] = null;
- Open Object array: Object array name = new class name [length];
- Static initialization:
- Class name Object array name [] =new class name []{instantiate object, instantiate object, instantiate object ...}
Example:
To dynamically generate an array of objects:
1 classbook_demo_1{2 PrivateString name;3 Private DoubleMoney ;4 5 6 Publicbook_demo_1 () {7 }8 PublicBook_demo_1 (String N,Doublem) {9Name =N;TenMoney =m; One } A - PublicString GetName () { - returnname; the } - Public DoubleGetmoney () { - returnMoney ; - } + - PublicString GetInfo () { + return"Title:" + name + "\ T" + "price" +Money ; A } at - Public voidSetName (String N) { -Name =N; - } - Public voidSetmoney (intm) { -Money =m; in } - } to + Public classObject Array { - Public Static voidMain (string[] args) { theBook_demo_1 Book [] =NewBOOK_DEMO_1[3];//open an array of 3-length objects *Book[0] =NewBook_demo_1 ("Java development", 66.6); $BOOK[1] =NewBook_demo_1 ("Python", 99.9);Panax NotoginsengBOOK[2] =NewBook_demo_1 ("PHP", 11.1); - for(inti = 0; i < book.length; i++) { the System.out.println (Book[i].getinfo ()); + } A } the}
Static initialization:
1 classbook_demo_1{2 PrivateString name;3 Private DoubleMoney ;4 5 6 Publicbook_demo_1 () {7 }8 PublicBook_demo_1 (String N,Doublem) {9Name =N;TenMoney =m; One } A - PublicString GetName () { - returnname; the } - Public DoubleGetmoney () { - returnMoney ; - } + - PublicString GetInfo () { + return"Title:" + name + "\ T" + "price" +Money ; A } at - Public voidSetName (String N) { -Name =N; - } - Public voidSetmoney (intm) { -Money =m; in } - } to + Public classObject Array { - Public Static voidMain (string[] args) { theBook_demo_1 Book [] =Newbook_demo_1[]{ * NewBook_demo_1 ("Java development", 66.6), $ NewBook_demo_1 ("Python", 99.9),Panax Notoginseng NewBook_demo_1 ("PHP", 11.1), - }; the for(inti = 0; i < book.length; i++) { + System.out.println (Book[i].getinfo ()); A } the } +}
- An array of objects is actually just handing over multiple objects to the array for unified management.
- Object array definition syntax, object array is actually equal to multiple objects
Java Object array