标签:[1] \n IV info public 两种 学习 ring double
两种定义方式:
2.静态初始化:
类名称 对象数组名[] = new 类名称[] {对象,对象......};
class Book { private String name; private double price; public Book(String n ,int p) { name = n; price = p; } public void setName(String n) { name = n; } public void setPrice(double p) { price = p; } public String getInfo() { return "书名"+name+"\n"+ "单价"+price; } } public class test2 { public static void main(String args[]) { Book arr[] = new Book[3];//开辟数组 空间 arr[0] = new Book("java",10);//对象需要分别实例化 arr[1] = new Book("js",20); arr[2] = new Book("py",30); for(int i=0; i<arr.length; i++) { System.out.println(arr[i]); System.out.println(arr[i].getInfo()); } } }
标签:[1] \n IV info public 两种 学习 ring double
原文地址:https://www.cnblogs.com/xhnxhnu/p/9124534.html