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:02 浏览: 88
下面是Java应用程序的代码:
```java
// People.java
public class People {
public void speakHello() {
System.out.println("Hello!");
}
public void averageHeight() {
System.out.println("The average height is about 170cm.");
}
public void averageWeight() {
System.out.println("The average weight is about 60kg.");
}
}
// ChinaPeople.java
public class ChinaPeople extends People {
@Override
public void speakHello() {
System.out.println("你好!");
}
@Override
public void averageHeight() {
System.out.println("中国人的平均身高约为170cm。");
}
@Override
public void averageWeight() {
System.out.println("中国人的平均体重约为60kg。");
}
public void chinaGongfu() {
System.out.println("中国功夫厉害!");
}
}
// AmericanPeople.java
public class AmericanPeople extends People {
@Override
public void speakHello() {
System.out.println("Hello, how are you?");
}
@Override
public void averageHeight() {
System.out.println("The average height of Americans is about 175cm.");
}
@Override
public void averageWeight() {
System.out.println("The average weight of Americans is about 80kg.");
}
public void americanBoxing() {
System.out.println("American boxing is great!");
}
}
// 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();
System.out.println("-------------------------");
chinaPeople.speakHello();
chinaPeople.averageHeight();
chinaPeople.averageWeight();
chinaPeople.chinaGongfu();
System.out.println("-------------------------");
americanPeople.speakHello();
americanPeople.averageHeight();
americanPeople.averageWeight();
americanPeople.americanBoxing();
}
}
```
在主方法中,分别创建了People、ChinaPeople和AmericanPeople的对象,并分别调用各自的方法打印输出信息。注意,ChinaPeople和AmericanPeople重写了父类People的speakHello()、averageHeight()和averageWeight()方法,因此在实例化它们的对象后,调用这些方法时,将会执行子类中的方法。
阅读全文