public class Phone {
private String brand;
private int price;
static private int size;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public static int getSize() {
return size;
}
public static void setSize(int size) {
Phone.size = size;
}
public Phone(String brand, int price) {
super();
this.brand = brand;
this.price = price;
}
public Phone() {
super();
// TODO Auto-generated constructor stub
}
public void game() {
System.out.println("玩游戏");
}
/*@Override
public String toString() {
return "Phone [brand=" + brand + ", price=" + price + "]";
}*/
public class 阿萨德 {
public static void main(String[] args) {
ArrayList<Phone> s =new ArrayList<>();
s.add(new Phone("asd",1500));
s.add(new Phone("as",1500));
System.out.println(s);
}
}
}
从控制台我们得到[dd.Phone@11dba45, dd.Phone@b03be0];两个元素的地址值
public class 阿萨德 {
public static void main(String[] args) {
ArrayList<Phone> s =new ArrayList<>();
s.add(new Phone("asd",1500));
s.add(new Phone("as",1500));
for (int i = 0; i < s.size(); i++) {
System.out.println(s.get(i).getBrand()+" "+s.get(i).getPrice());
}
}
从控制台我们得到asd 1500 as 1500 两个对象的两个元素;
public class Phone {
private String brand;
private int price;
static private int size;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public static int getSize() {
return size;
}
public static void setSize(int size) {
Phone.size = size;
}
public Phone(String brand, int price) {
super();
this.brand = brand;
this.price = price;
}
public Phone() {
super();
// TODO Auto-generated constructor stub
}
public void game() {
System.out.println("玩游戏");
}
@Override //重写toString
public String toString() {
return "Phone [brand=" + brand + ", price=" + price + "]";
}
}
public class 阿萨德 {
public static void main(String[] args) {
ArrayList<Phone> s =new ArrayList<>();
s.add(new Phone("asd",1500));
s.add(new Phone("as",1500));
System.out.println(s);