对象类型作为参数以及返回值
使用对象类型作为方法的参数
public class Phone { String brand; //品牌 double price; //价格 String color; //颜色 //打电话 public void call(String who){ System.out.println("给"+who+"打电话"); } //发短信 public void sendMessage(){ System.out.println("发短信"); } }
public class PhoneTest { public static void main(String[] args) { Phone phone = new Phone(); phone.brand = "苹果"; phone.price = 10000; phone.color = "黑色"; method(phone); } public static void method(Phone phone){ System.out.println(phone.brand); System.out.println(phone.price); System.out.println(phone.color); } }

当一个对象作为参数,传递到方法当中时,实际上传递进去的是对象的地址值
使用对象类型作为方法的返回值
public class Phone { String brand; //品牌 double price; //价格 String color; //颜色 //打电话 public void call(String who){ System.out.println("给"+who+"打电话"); } //发短信 public void sendMessage(){ System.out.println("发短信"); } }
public class PhoneTest { public static void main(String[] args) { Phone phone = getPhone(); System.out.println(phone.brand); System.out.println(phone.price); System.out.println(phone.color); } public static Phone getPhone(){ Phone phone = new Phone(); phone.brand = "苹果"; phone.price = 10000; phone.color = "黑色"; return phone; } }


浙公网安备 33010602011771号