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

public class Test {
    public static void main(String[] args) {
        Phone phone = new Phone();
        phone.brand = "菠萝手机";
        phone.price = 500;
        phone.color = "黑色";


        method(phone);//传递进去的对象其实就是地址值
    }

    public static void method(Phone phone){
        System.out.println(phone.price);//菠萝手机
        System.out.println(phone.brand);//500
        System.out.println(phone.color);//黑色
    }
}




class Phone{
    String brand;
    double price;
    String color;
    public void call(String who){
        System.out.println("给"+who+"打电话");
    }
    public void sendMessage(){
        System.out.println("群发短信");
    }
}

image

对象作为方法的返回值

public class Test {
    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.color = "黑色";
        phone.price = 123.456;

        return phone;
    }
}
posted @ 2022-06-29 10:32  我滴妈老弟  阅读(31)  评论(0)    收藏  举报