首页 > 其他分享 >29-类与对象的创建

29-类与对象的创建

时间:2023-02-07 21:57:27浏览次数:42  
标签:xiaoming name 对象 创建 29 System 实例 Student public

属性和方法组成类

package com.oop;

//属性和方法组成类
public class Student {

    //属性
    String name;
    int age;

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

}

类是抽象的,可以用new实例化

package com.oop;

public class Application {
    public static void main(String[] args) {

        //类是抽象的,需要实例化,new
        Student xiaoming = new Student();
        //类实例化后会返回一个自己的对象
        //xiaoming对象 就是一个Student类的具体实例
        xiaoming.name="小明";
        xiaoming.age=5;
        System.out.println(xiaoming.name);
        System.out.println(xiaoming.age);

    }
}

标签:xiaoming,name,对象,创建,29,System,实例,Student,public
From: https://www.cnblogs.com/PedroPascal/p/17099928.html

相关文章