java 类与对象的创建

1,新建Application类,作为程序的入口

package oop.demo02;

public class Application {
    public static void main(String[] args) {
//类,抽象的,需要实例化
        Student xiaoming = new Student();
        System.out.println("xiaoming.name:\t" + xiaoming.name);//xiaoming.name:	null
        System.out.println("xiaoming.age:\t" + xiaoming.age);//xiaoming.age:	0

        Student xiaoxiao = new Student();
        xiaoxiao.name="小不";
        xiaoxiao.age=18;
        System.out.println("xiaoxiao.name:\t"+xiaoxiao.name);//xiaoxiao.name:	小不
        System.out.println("xiaoxiao.age:\t"+xiaoxiao.age);//xiaoxiao.age:	18

    }
}

2,新建Student类

package oop.demo02;
//学生类;
public class Student {
//属性
    String name; //默认为null
    int age;//0

//方法
public  void  study(){
    System.out.println(this.name+"在学习!!!");
}
}

3,运行结果

posted @ 2021-07-31 16:18  阿向向  阅读(138)  评论(0编辑  收藏  举报