JAVA对象调用

一个对象的内存图

在这里插入图片描述

package cn.hu.day01.demo03;

public class Phone {

    String brand;//品牌
    double price;
    String color;

    public void call(String who){
        System.out.println("给"+who+"打电话");
    }

    public void sendMessage(){
        System.out.println("群发短信");
    }
}

package cn.hu.day01.demo03;

public class Demo01PhoneOne {
    public static void main(String[] args) {
        Phone one=new Phone();
        System.out.println(one.brand);//null
        System.out.println(one.price);//0.0
        System.out.println(one.color);//null
        System.out.println("==========================");

       
       
        one.price =8388.0;
        one.color ="黑色";

        System.out.println(one.brand);
        System.out.println(one.price);
        System.out.println(one.color);
        System.out.println("==========================");


        one.call("乔布斯");
        one.sendMessage();
    }
}

过程:

1.将两个类的定义(成员变量和成员方法)存入方法区
2.程序先从main方法开始,所以main方法先在栈区压栈并开辟空间
3.执行main方法中代码 Phone one 局部变量创建在栈区,并开辟小段空间来存地址
3. 执行main方法中代码new Phone();堆区创建对象开辟空间
4. 对成员变量赋初值成员变量所在空间有其地址0x666
5. 成员方法在堆区只开辟存放地址的小空间,其真正的位置在方法区
6. 执行代码 one.brand =“苹果”,先在栈区中找到
Phone one
空间存放的对象地址
7. 然后找到堆区开辟出来的空间,将brand的值由初始化的null改为"苹果"
8.调用成员方法 one.call(“乔布斯”) 时先在Phone one空间找到0x666然后找到堆区的0x333
9.根据0x333找到方法区的成员方法call(String who)
10.然后该成员方法在栈区压栈调用,该方法执行完后立即弹出栈区,轮到main方法继续执行
11.当main方法执行完后,main方法也弹出栈。程序执行完毕

两个对象使用同一方法的内存图

在这里插入图片描述

package cn.hu.day01.demo03;

public class Phone {

    String brand;//品牌
    double price;
    String color;

    public void call(String who){
        System.out.println("给"+who+"打电话");
    }

    public void sendMessage(){
        System.out.println("群发短信");
    }
}

package cn.hu.day01.demo03;

public class Demo01PhoneTwo {
    public static void main(String[] args) {
        Phone one=new Phone();
        System.out.println(one.brand);//null
        System.out.println(one.price);//0.0
        System.out.println(one.color);//null
        System.out.println("==========================");

        one.brand ="苹果";
        one.price =8388.0;
        one.color ="黑色";

        System.out.println(one.brand);
        System.out.println(one.price);
        System.out.println(one.color);
        System.out.println("==========================");


        one.call("乔布斯");
        one.sendMessage();
        System.out.println("==========================");

        Phone two=new Phone();
        System.out.println(two.brand);//null
        System.out.println(two.price);//0.0
        System.out.println(two.color);//null
        System.out.println("==========================");

        two.brand ="三星";
        two.price =5999.0;

        two.color ="蓝色";

        System.out.println(two.brand);
        System.out.println(two.price);
        System.out.println(two.color);
        System.out.println("==========================");


        two.call("欧巴");
        two.sendMessage();
    }
}

两个引用指向同一个对象

在这里插入图片描述

package cn.hu.day01.demo03;

public class Phone {

    String brand;//品牌
    double price;
    String color;

    public void call(String who){
        System.out.println("给"+who+"打电话");
    }

    public void sendMessage(){
        System.out.println("群发短信");
    }
}

package cn.hu.day01.demo03;

public class Demo01PhoneSame {
    public static void main(String[] args) {
        Phone one=new Phone();
        System.out.println(one.brand);//null
        System.out.println(one.price);//0.0
        System.out.println(one.color);//null
        System.out.println("==========================");

        one.brand ="苹果";
        one.price =8388.0;
        one.color ="黑色";

        System.out.println(one.brand);
        System.out.println(one.price);
        System.out.println(one.color);
        System.out.println("==========================");


        one.call("乔布斯");
        one.sendMessage();
        System.out.println("==========================");

        //将one当中保存的对象地址值赋值给two
        Phone two=one;
        System.out.println(two.brand);//null
        System.out.println(two.price);//0.0
        System.out.println(two.color);//null
        System.out.println("==========================");

        two.brand ="三星";
        two.price =5999.0;

        two.color ="蓝色";

        System.out.println(two.brand);
        System.out.println(two.price);
        System.out.println(two.color);
        System.out.println("==========================");


        two.call("欧巴");
        two.sendMessage();
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值