5.0静态属性

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);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值