本实验的目的是让学生掌握类变量与实例变量,以及类方法与实例方法的区别
编写程序模拟两个村庄共同拥有一片森林。编写一个Village类,该类有一个静态的int型成员变量treeAmount用于模拟森林中树木的数量。在主类MainClass的main方法中创建两个村庄,一个村庄改变了treeAmount的值,另一个村庄查看treeAmount的值。
Village.java
public 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
public class MainClass { public static void main(String[] args) { // TODO Auto-generated method stub Village zhaoZhuang,majiaHeZi; zhaoZhuang = new Village("赵庄"); majiaHeZi = new Village("马家河子"); zhaoZhuang.peopleNumber = 100; majiaHeZi.peopleNumber = 150; //用类名Village访问treeAmount,并赋值200 Village.treeAmount = 200; int leftTree = Village.treeAmount; System.out.println("森林中有"+leftTree+"棵树"); //zhaoZhuang调用treePlanting(int n),并传值50 zhaoZhuang.treePlanting(50); //majiaHezi调用lookTreeAmount()方法得到树木的数量 leftTree = Village.lookTreeAmount(); System.out.println("森林中有"+leftTree+"棵树"); //majiaHeZi调用fellTree(int n)并向参数传值70 majiaHeZi.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("马家河子的人口:"+majiaHeZi.peopleNumber); majiaHeZi.addPeopleNumber(10); System.out.println("马家河子的人口:"+majiaHeZi.peopleNumber); } }
也可以合并到一起
public class MainClass { public static void main(String[] args) { // TODO Auto-generated method stub Village zhaoZhuang,majiaHeZi; zhaoZhuang = new Village("赵庄"); majiaHeZi = new Village("马家河子"); zhaoZhuang.peopleNumber = 100; majiaHeZi.peopleNumber = 150; //用类名Village访问treeAmount,并赋值200 Village.treeAmount = 200; int leftTree = Village.treeAmount; System.out.println("森林中有"+leftTree+"棵树"); //zhaoZhuang调用treePlanting(int n),并传值50 zhaoZhuang.treePlanting(50); //majiaHezi调用lookTreeAmount()方法得到树木的数量 leftTree = Village.lookTreeAmount(); System.out.println("森林中有"+leftTree+"棵树"); //majiaHeZi调用fellTree(int n)并向参数传值70 majiaHeZi.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("马家河子的人口:"+majiaHeZi.peopleNumber); majiaHeZi.addPeopleNumber(10); System.out.println("马家河子的人口:"+majiaHeZi.peopleNumber); } } 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+"人"); } }
运行结果