1、People类
public class People {
private int id = 0;//序号
private int age;//单位岁
private int height;//单位cm
private double weight;//单位斤
private static int num = 0;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public static int getNum() {
return num;
}
public static void setNum(int num) {
People.num = num;
}
//构造函数
People(int a,int h,double w){
this.age = a;
this.height = h;
this.weight = w;
num = num+1;
this.id = num;
}
//进食函数
public void eatting(){
this.weight = this.weight + 1;
}
//运动函数
public void sporting(){
this.height = this.height + 1;
}
//睡眠函数
public void sleeping(){
this.age = this.age + 1;
this.height = this.height + 1;
this.weight = this.weight + 1;
}
//显示函数
public void show(){
System.out.println("序号:"+this.id);
System.out.println("年龄:"+this.age);
System.out.println("身高:"+this.height);
System.out.println("体重:"+this.weight);
}
//显示人数函数
public static void shownum(){
System.out.println("人数:"+People.num);
}
}
2、测试类
public class TestPeople {
public static void main(String[] args) {
People[] peoples = new People[8];
System.out.println("---------------现有人数(构造对象之前)--------------------");
People.shownum();
System.out.println("------------------------------------------");
for(int i=0;i<peoples.length;i++){
peoples[i] = new People(12,178,120);
}
System.out.println("---------------现有人数(构造对象之后)--------------------");
People.shownum();
System.out.println("------------------------------------------");
//调用各成员函数
peoples[0].show();
peoples[1].eatting();
peoples[2].sporting();
peoples[3].sleeping();
System.out.println("--------------现在各对象属性值------------------");
for (People people : peoples) {
people.show();
}
}
}