五、创建对象内存分析

 内存分析图

 

 

宠物类

public class Pet {

    String name;

    int age;

    void speak(String words){
        System.out.println(words);
    }

    static void sayHi(){
        System.out.println("Hi");
    }

}

 

测试类

public static void main(String[] args) {
        Pet dog = new Pet();

        dog.name="小狗";
        dog.age=2;
        dog.speak("汪汪汪");


        Pet cat = new Pet();
        cat.name="小狗";
        cat.age=2;
        dog.speak("喵喵喵");

 }

 

posted @ 2022-05-13 01:25  Epiphany8Z  阅读(21)  评论(0编辑  收藏  举报