当前位置: 首页 > 工具软件 > 2people > 使用案例 >

Java 创建三个类People、ChinaPeople、AmericanPeople,其中People是父类,另外两个是其子类

马峻
2023-12-01

题目:

【问题描述】
创建三个类People、ChinaPeople、AmericanPeople,其中People是父类,另外两个是其子类。父类和测试类已经有完整定义了,根据输出结果,完成ChinaPeople和AmericanPeople的编写。

【输入形式】

【输出形式】
相应字符串。

【样例输入】

【样例输出】
hello,I am ChinaPeople.
avarge heigh of ChinaPeople:180.0
average weight of ChinaPeople:130.0
I can gongfu.
hello,I am American!
avarge heigh of American:175.0
average weight of American:150.0
I can boxing.

代码:

class  People  {
    protected  double  height;
    protected  double  weight;

    public  double  getHeight()  {
            return  height;
    }

    public  void  setHeight(double  height)  {
            this.height  =  height;
    }

    public  double  getWeight()  {
            return  weight;
    }

    public  void  setWeight(double  weight)  {
            this.weight  =  weight;
    }

    public  void  speakHello()  {
            System.out.println("hello!");
    }

    public  void  averageHeight()  {
            System.out.println("avarge  heigh:"  +  height);
    }

    public  void  averageWeight()  {
            System.out.println("average  weight:"  +  weight);
    }
}
class  AmericanPeople  extends  People  {
    public void speakHello() {
    	System.out.println("hello,I am American!");
    }
    public  void  averageHeight()  {
        System.out.println("avarge heigh of American:"  +  height);
}
    public  void  averageWeight()  {
        System.out.println("average weight of American:"  +  weight);
}
	public void amercianBoxing() {
		System.out.println("I can boxing.");
	} 
	
}
class  ChinaPeople  extends  People  {
	 public void speakHello() {
	    	System.out.println("hello,I am ChinaPeople.");
	    }
	 public  void  averageHeight()  {
         System.out.println("avarge heigh of ChinaPeople:"  +  height);
 }
	 public  void  averageWeight()  {
         System.out.println("average weight of ChinaPeople:"  +  weight);
 }
	public void chinaGongfu() {
		System.out.println("I can gongfu.");
	} 
	
}
public  class  TestPeople  {
        public  static  void  main(String[]  args)  {
                ChinaPeople  chp  =  new  ChinaPeople();
                chp.height  =  180;
                chp.weight  =  130;
                chp.speakHello();
                chp.averageHeight();
                chp.averageWeight();
                chp.chinaGongfu();
                AmericanPeople  amp  =  new  AmericanPeople();
                amp.setHeight(175);
                amp.setWeight(150);
                amp.speakHello();
                amp.averageHeight();
                amp.averageWeight();
                amp.amercianBoxing();
        }
}
 类似资料: