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 11:18:20 浏览: 89
用java调用oracle存储过程总结
4星 · 用户满意度95%
下面是代码实现:
People.java
```java
public class People {
public void speakHello() {
System.out.println("Hello!");
}
public void averageHeight() {
System.out.println("The average height is about 1.7 meters.");
}
public void averageWeight() {
System.out.println("The average weight is about 65 kilograms.");
}
}
```
ChinaPeople.java
```java
public class ChinaPeople extends People {
public void speakHello() {
System.out.println("你好!");
}
public void averageHeight() {
System.out.println("中国人的平均身高约为1.68米。");
}
public void averageWeight() {
System.out.println("中国人的平均体重约为60公斤。");
}
public void chinaGongfu() {
System.out.println("中国功夫!");
}
}
```
AmericanPeople.java
```java
public class AmericanPeople extends People {
public void speakHello() {
System.out.println("Hello!");
}
public void averageHeight() {
System.out.println("The average height of Americans is about 1.78 meters.");
}
public void averageWeight() {
System.out.println("The average weight of Americans is about 80 kilograms.");
}
public void americanBoxing() {
System.out.println("American boxing!");
}
}
```
TestOne.java
```java
public class TestOne {
public static void main(String[] args) {
ChinaPeople chinaPeople = new ChinaPeople();
AmericanPeople americanPeople = new AmericanPeople();
chinaPeople.speakHello();
chinaPeople.averageHeight();
chinaPeople.averageWeight();
chinaPeople.chinaGongfu();
americanPeople.speakHello();
americanPeople.averageHeight();
americanPeople.averageWeight();
americanPeople.americanBoxing();
}
}
```
运行结果:
```
你好!
中国人的平均身高约为1.68米。
中国人的平均体重约为60公斤。
中国功夫!
Hello!
The average height of Americans is about 1.78 meters.
The average weight of Americans is about 80 kilograms.
American boxing!
```
阅读全文