package work1;
/*
定义类的时候,首字母要大写,并且在同一个包内类名称不能重复
/
class People{
/*- 定义属性,即成员变量
- /
String name;
static String country="北京";
/* - 定义有参的构造方法
- /
public People(String name) {
this.name=name;
}
public static String getCountry() {
return country;
}
public static void setCountry(String country) {
People.country = country;
}
//定义成员方法
public void tell() {System.out.println("姓名:"+name+"出生地:"+getCountry());
}
}
public class A3 {public static void main(String[] args) {
Person.setCountry("上海");//为什么我在这里明明都设置了它为上海,而最后运行的结果三个都是北京呢? People p1=new People("张三");
// p1.country="上海";
p1.tell(); People p2=new People("李四"); p2.tell(); People p3=new People("王五"); p3.tell();
}
}