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

静态static关键字修饰成员变量
public class Student {
private int id;
private String name;
private int age;
static String room;//锁在教室
private static int idCount = 0;//学号计数器,每当new了一个新对象的时候,计数器++
public Student(String name, int age) {
this.name = name;
this.age = age;
this.id = idCount++;
}
public Student() {
idCount++;
}
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;
}
public class test {
public static void main(String[] args) {
Student two = new Student("黄蓉",19);
two.room="111教室";
System.out.println("姓名:"+two.getName()+"年龄:"+two.getAge()+"教室:"+two.room+"学号:"+two.getId());
Student one = new Student("郭靖",19);
System.out.println("姓名:"+one.getName()+"年龄:"+one.getAge()+"教室:"+one.room+"学号:"+one.getId());
}

浙公网安备 33010602011771号