静态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) {标签:name,int,age,id,关键字,static,修饰,public From: https://www.cnblogs.com/shenziyi/p/16624864.html
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());
}