Village.java
class Village{
static int treeAmount;
int peopleNumber;
String name;
Village(String s){
name = s;
}
void treePlanting(int n){
treeAmount = treeAmount+n;
System.out.println(name+"植树"+n+"棵");
}
void fellTree(int n){
if(treeAmount-n>=0){
treeAmount = treeAmount-n;
System.out.println(name+"伐树"+n+"棵");
}
else{
System.out.println("无树木可伐");
}
}
static int lookTreeAmount(){
return treeAmount;
}
void addPeopleNumber(int n){
peopleNumber = peopleNumber+n;
System.out.println(name+"增加了"+n+"人");
}
}
MainClass.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author 高晶
*/
public class MainClass {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Village zhaoZhuang,maJiaHeZhi;
zhaoZhuang=new Village("赵庄");
maJiaHeZhi=new Village("马家河子");
zhaoZhuang.peopleNumber=100;
maJiaHeZhi.peopleNumber=150;
Village.treeAmount=200;
int leftTree = Village.treeAmount;
System.out.println("森林中有"+leftTree+"棵树");
zhaoZhuang.treePlanting(50);
leftTree=Village.lookTreeAmount();g
System.out.println("森林中有"+leftTree+"棵树");
maJiaHeZhi.fellTree(70);
leftTree=Village.lookTreeAmount();
System.out.println("森林中有"+leftTree+"棵树");
System.out.println("赵庄的人口"+zhaoZhuang.peopleNumber);
zhaoZhuang.addPeopleNumber(12);
System.out.println("赵庄的人口"+zhaoZhuang.peopleNumber);
System.out.println("马家河子的人口"+maJiaHeZhi.peopleNumber);
maJiaHeZhi.addPeopleNumber(10);
System.out.println("马家河子的人口"+maJiaHeZhi.peopleNumber);
// TODO code application logic here
}
}