1、编写一个Java应用程序,除了主类(Testone)外,该程序中还有3个 类:People、ChinaPeople和AmericanPeople。该程序具体要求如下: 1)People类有publicvoidspeakHe11o0、publicvoid averagelleight0和publicvoidaverageWeight0方法。 2)ChinaPeople类是People的子类,新增了publicvoidchinaGongfu( 方法。要求ChinaPeople重写父类的publicvoidspeaklle11o0、publicvoid averagelleight(和publicvoidaveragelleight(方法。 3)AmericanPeople类是People的子类,新增publicvoid americanBoxing0方法。要求AmericanPeople重写父类的publicvoid speakHello public void averageHleight0和publicvoid averageveight0方法。 4)在Testone类的主方法中分别创建各子类的对象并调用各自的方法打印 输出信息。 2
时间: 2024-01-22 13:18:28 浏览: 85
下面是Java应用程序的代码实现:
People.java
```java
public class People {
public void speakHello() {
System.out.println("Hello!");
}
public void averageHeight() {
System.out.println("The average height is 170cm.");
}
public void averageWeight() {
System.out.println("The average weight is 60kg.");
}
}
```
ChinaPeople.java
```java
public class ChinaPeople extends People {
public void chinaGongfu() {
System.out.println("I can do China Gongfu!");
}
@Override
public void speakHello() {
System.out.println("你好!");
}
@Override
public void averageHeight() {
System.out.println("中国人的平均身高为 165cm。");
}
@Override
public void averageWeight() {
System.out.println("中国人的平均体重为 55kg。");
}
}
```
AmericanPeople.java
```java
public class AmericanPeople extends People {
public void americanBoxing() {
System.out.println("I can do American Boxing!");
}
@Override
public void speakHello() {
System.out.println("Hi!");
}
@Override
public void averageHeight() {
System.out.println("The average height of American people is 175cm.");
}
@Override
public void averageWeight() {
System.out.println("The average weight of American people is 75kg.");
}
}
```
Testone.java
```java
public class Testone {
public static void main(String[] args) {
People p1 = new People();
p1.speakHello();
p1.averageHeight();
p1.averageWeight();
ChinaPeople p2 = new ChinaPeople();
p2.speakHello();
p2.averageHeight();
p2.averageWeight();
p2.chinaGongfu();
AmericanPeople p3 = new AmericanPeople();
p3.speakHello();
p3.averageHeight();
p3.averageWeight();
p3.americanBoxing();
}
}
```
在Testone类的main方法中,我们分别创建了一个People对象p1,一个ChinaPeople对象p2和一个AmericanPeople对象p3,并分别调用它们的方法打印输出信息。其中,p2对象重写了父类People的三个方法,并新增了一个方法chinaGongfu(),而p3对象也重写了父类People的三个方法,并新增了一个方法americanBoxing()。
阅读全文