静态static关键字概述和修饰成员变量

静态static关键字概述

 

 静态static关键字修饰成员变量

案例:

Student7类:

private int id;
private String name;
private int age;
static String room;
private static int idCounter=0;

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

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

public int getId() { return id; }

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

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;
}

DemoStaticTest7类:

public static void main(String[] args) {
Student7 one = new Student7("吴磊",18);
one.room="101教室";
System.out.println("姓名:"+one.getName()+",年龄:"+one.getAge()
+",教室:"+one.room+",学号:"+one.getId());

Student7 two = new Student7("赵露思", 18);
System.out.println("姓名:"+two.getName()+",年龄:"+two.getAge()
+",教室:"+two.room+",学号:"+two.getId());
}
posted @ 2022-08-25 17:45  想见玺1面  阅读(27)  评论(0编辑  收藏  举报