简单的java小题目,求帮助

简单的java小题目,求帮助编写设计一个people(人)类。该类的数据成员有年龄(age),身高(height), 体重(weight)和人数(num),其中人数为静态数据成员,成员函数有构造函数(people),进食(eatting),运动(sporting),睡眠(sleeping),显示(show)和显示人数(shownum)。 其中构造函数由已参数年龄(a),身高(h),体重(w)构造对象,进食函数(eatting)使体重加1,运动函数运动(sporting)使身高加1,睡眠函数(sleeping)使年龄,身高,体重各加1,显示函数(show)用于显示人的序号,年龄,身高,体重,显示人数函数(shownum)为静态成员函数,用于显示人的个数。假设年龄的单位为岁,身高的单位为厘米,体重的单位为斤。在主函数中定义一个对象数组(8个元素)通过对象数组访问类中的所有成员函数。

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

温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-10-12
class People{
public:
People(){
age = 0;
height = 0;
weight = 0;
}

~People();

void eat(){
weight += 1;
}

void sport(){
height += 1;
}

void sleep(){
age += 1;
height += 1;
weight += 1;
}

private:
int age;
int height;
int weight;
} ;
相似回答