Phone作为参数类型和返回值类型的内存图

mac2026-06-20  1

/* 当一个对象作为参数时,传递到方法当中,实际上传递进去的是对象的地址值 */ public class Demo04PhoneParam { public static void main(String[] args) { Phone one =new Phone(); one.brand="苹果"; one.price=838801.0; one.color="土豪金"; method(one); } public static void method(Phone param){ System.out.println(param.color); System.out.println(param.price); System.out.println(param.brand); } }

/* 当使用对象类型作为方法的返回值时,返回值其实就是对象的地址值 */ public class Demo05PhoneReturn { public static void main(String[] args) { Phone two =methond(); System.out.println(two.brand); System.out.println(two.price); System.out.println(two.color); } public static Phone methond(){ Phone one = new Phone(); one.brand="苹果"; one.price=8366.0; one.color="玫瑰金"; return one; }

最新回复(0)