使用对象类型作为方法的参数

使用对象类型作为方法的参数

当一个对象作为参数,传递到方法当中时,实际上传进去的是对象地址值

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

public class Main {
public static void main(String[] args) {
Phone one = new Phone();
one.brand = "苹果";
one.price = 8388.0;
one.color = "黑色";

method(one);

}
public static void method(Phone param){
System.out.println(param.brand);//苹果
System.out.println(param.price);//8388.0
System.out.println(param.color);//黑色
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//类的定义

public class Phone {
String brand;
double price;
String color;

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

}