Loading

Java类与对象的创建

以类的方式组织代码,以对象的方式组织(封装)数据

组织代码(类)

public class Demo04 {
    String name;//默认值null
    int age;//默认值0

    public void study() {
        System.out.println(this.name+"在学习");
        //同一地址
        System.out.println(this.hashCode());
    }
}

封装数据

public class Demo05 {
    public static void main(String[] args) {
        //类:抽象的,实例化
        //类实例化后会返回一个自己的对象
        //student对象就是一个Demo4类的具体实例
        Demo04 student=new Demo04();
        student.name="小明";
        student.age=18;
        System.out.println(student.name);
        System.out.println(student.age);
        //同一地址
        System.out.println(student.hashCode());
        student.study();
    }
}

结果

小明
18
460141958
小明在学习
460141958
posted @ 2022-01-17 15:54  Cn_FallTime  阅读(37)  评论(0编辑  收藏  举报