package exercise;
class Village{
private String name;
private int number;
static int count=6500;//相当于全局变量
public void setName(String name) {
if(name=="") {
System.out.println("请输入名字");
}else {
this.setName(name);
}
}
public String getName() {
return name;
}
public void setNumber(int number) {
if(number<=0) {
System.out.println("请输入正确的人数");
}else {
this.number=number;
}
}
public int getNumber() {
return number;
}
public static void setCount(int count) {
if(count<=0) {
System.out.println("请正确的树木数量");
}else {
Village.count = count;
}
}
public static int getCount() {
return count;
}
public Village() {
System.out.println("村庄已经成功建造");
}
public Village(String name) {
this.name=name;
}
public Village(int number) {
this.number=number;
}
public Village(String name,int number) {
this.name=name;
this.number=number;
}
public void info() {
System.out.println("这个村庄名字叫"+this.name+"一共有"+this.number+"人");
}
public void peoplenumber(int n){
if(n<=0) {
this.number=this.number+n;
System.out.println(this.name+"逝去了"+(-n)+"人"+",现在一共有"+this.number+"人");
}else {
this.number=this.number+n;
System.out.println(this.name+"出生了"+n+"人"+",现在一共有"+this.number+"人");
}
}
public void treecount(String name,int n) {
if(n<=0) {
Village.count=Village.count+n;
System.out.println(this.name+"砍了"+(-n)+"棵树"+",现在所有村庄共有"+Village.count+"棵树");
}else {
Village.count=Village.count+n;
System.out.println(this.name+"种了"+n+"棵树"+",现在所有村庄共有"+Village.count+"棵树");
}
}
}
public class demo_1 {
public static void main(String[] args) {
Village f1=new Village("李家庄",600);
Village f2=new Village("王家庄",800);
f1.info();
f2.info();
f1.peoplenumber(60);
f2.peoplenumber(80);
f1.peoplenumber(-60);
f2.peoplenumber(-8);
f1.treecount("李家庄", 600);
f2.treecount("王家庄", 800);
f1.treecount("李家庄", -600);
f2.treecount("王家庄", -800);
}
}
5.0静态属性
最新推荐文章于 2021-04-05 22:25:41 发布