public class Student {
private int id;
private String name;
private int age;
private static String city;
private static int counter;
static { //静态代码块
System.out.println(" 学生资料表-----静态代码块");
}
public Student(){
this.id=++counter;
}
public Student(String name, int age) {
this.name = name;
this.age = age;
this.id=++counter;
}
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 static String getCity() {
return city;
}
public static void setCity(String city) {
Student.city = city;
}
}
==================================================
public class StaticDemo {
public static void main(String[] args) {
Student st1 = new Student("张三",19);
Student st2 = new Student("李四",20);
Student.setCity("沈阳");
System.out.println("学生编号"+st1.getId()+" 学生姓名:"+st1.getName()+" 学生年龄:"+st1.getAge()
+" 学生籍贯:"+Student.getCity());
System.out.println("学生编号"+st2.getId()+" 学生姓名:"+st2.getName()+" 学生年龄:"+st2.getAge()
+" 学生籍贯:"+Student.getCity());
}
}
JAVA静态类static及静态代码块练习
最新推荐文章于 2022-10-15 15:20:00 发布