package lkp;
class Person{
private String name;
private int age;
static String country = "A city"; // 使用static定义城市属性
public Person(String name,int age){ //通过构造为属性赋值
this.name = name;
this.age = age;
}
public void info(){
System.out.println("姓名: " + this.name + " 年龄: " + this.age + " 城市: " + country);
}
}
public class Test{
public static void main(String args[]){
Person per1 = new Person("张三",20);
Person per2 = new Person("李四",21);
Person per3 = new Person("王五",23);
System.out.println("--------- 修改前-----------");
per1.info();
per2.info();
per3.info();
System.out.println("--------- 修改后-----------");
Person.country = "B city"; //使用类名称修改static属性的内容
per1.info();
per2.info();
per3.info();
}
}
使用static声明属性
最新推荐文章于 2022-02-23 15:18:04 发布