使用对象类型作为方法的返回值

使用对象类型作为方法的返回值

当使用一个对象类型作为方法返回值时:返回值其实就是对象的地址.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

public class Main2 {
public static void main(String[] args) {
Phone two = getPhone();
System.out.println(two.brand);//苹果
System.out.println(two.price);//8388.0
System.out.println(two.color);//玫瑰金
}
public static Phone getPhone(){
Phone one = new Phone();

one.brand = "苹果";
one.price = 8388.0;
one.color = "玫瑰金";
return one;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

public class Phone {
String brand;//产品
double price;//价格
String color;//颜色

public void printAll()
{
System.out.println(brand);
System.out.println(price);
System.out.println(color);
}

}