1、编写一个Java应用程序,除了主类(TestOne)外,该程序中还有3个类:People、ChinaPeople和AmericanPeople。该程序具体要求如下: 1)People 类有 public void speakHello) 、public voidaverageHeight()和public void averageWeight)方法。 2)ChinaPeople类是People的子类,新增了public void chinaGongfu()方法。要求ChinaPeople重写父类的public void speakHello()、public voidaverageleight()和public void averageWeight()方法。 3)AmericanPeople 类是People的子类,新增publicvoidamericanBoxing()方法。要求AmericanPeople重写父类的public void speakHello() 、 public void averagelleight () 和 public voidaverageWeight()方法。 4)在TestOne类的主方法中分别创建各子类的对象并调用各自的方法打印输出信息。
时间: 2024-01-22 09:17:41 浏览: 71
下面是代码实现:
People类:
```java
public class People {
public void speakHello() {
System.out.println("Hello!");
}
public void averageHeight() {
System.out.println("The average height is unknown.");
}
public void averageWeight() {
System.out.println("The average weight is unknown.");
}
}
```
ChinaPeople类:
```java
public class ChinaPeople extends People {
@Override
public void speakHello() {
System.out.println("你好!");
}
@Override
public void averageHeight() {
System.out.println("中国人的平均身高为:175cm");
}
@Override
public void averageWeight() {
System.out.println("中国人的平均体重为:65kg");
}
public void chinaGongfu() {
System.out.println("中国功夫很厉害!");
}
}
```
AmericanPeople类:
```java
public class AmericanPeople extends People {
@Override
public void speakHello() {
System.out.println("Hello!");
}
@Override
public void averageHeight() {
System.out.println("美国人的平均身高为:180cm");
}
@Override
public void averageWeight() {
System.out.println("美国人的平均体重为:80kg");
}
public void americanBoxing() {
System.out.println("美国拳击很厉害!");
}
}
```
TestOne类:
```java
public class TestOne {
public static void main(String[] args) {
People people = new People();
ChinaPeople chinaPeople = new ChinaPeople();
AmericanPeople americanPeople = new AmericanPeople();
people.speakHello();
people.averageHeight();
people.averageWeight();
chinaPeople.speakHello();
chinaPeople.averageHeight();
chinaPeople.averageWeight();
chinaPeople.chinaGongfu();
americanPeople.speakHello();
americanPeople.averageHeight();
americanPeople.averageWeight();
americanPeople.americanBoxing();
}
}
```
输出结果:
```
Hello!
The average height is unknown.
The average weight is unknown.
你好!
中国人的平均身高为:175cm
中国人的平均体重为:65kg
中国功夫很厉害!
Hello!
美国人的平均身高为:180cm
美国人的平均体重为:80kg
美国拳击很厉害!
```
阅读全文