Javaの学習 - 静的静的キーワードの変更メンバーを

/ *
静的なキーワードを使用してメンバ変数なので、オブジェクト自体に属し、もはや変数が、クラスが配置されているに属している場合。複数のオブジェクトは、同じデータを共有しています。
* /

public class Demo01StaticField {
    public static void main(String[] args) {
        Student one = new Student("郭靖", 19);
        one.room = "101教室";
        System.out.println("姓名:" + one.getName()
                + ",年龄:" + one.getAge() + ",学号:" + one.getId()
                + ",教室:" + one.room);

        Student two = new Student("黄蓉", 16);
        System.out.println("姓名:" + two.getName()
                + ",年龄:" + two.getAge() + ",学号:" + two.getId()
                + ",教室:" + two.room);
    }
}

学生カテゴリ

public class Student {
    private String name;//姓名
    private int age;//年龄
    private int id;//学号
    static String room;// 所在教室
    private static int idCounter = 0;// 学号计数器,每当new了一个新对象的时候,计数器++

    public Student() {
        this.id = ++idCounter;
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
        this.id = ++idCounter;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

``

公開された23元の記事 ウォンの賞賛0 ビュー151

おすすめ

転載: blog.csdn.net/qq_44813352/article/details/104319193