java 编写一个People类的子类ChinaPeople,该子类重写People类的speak方法。在主类中用ChinaPeople类创建对象zhang,并让zhang调用speak方法输出“我是中国人”。
时间: 2023-05-21 15:07:19 浏览: 237
以下是代码:
```
class People {
public void speak() {
System.out.println("I am a person.");
}
}
class ChinaPeople extends People {
@Override
public void speak() {
System.out.println("我是中国人。");
}
}
public class Main {
public static void main(String[] args) {
ChinaPeople zhang = new ChinaPeople();
zhang.speak();
}
}
```
输出:
```
我是中国人。
```
相关问题
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
下面是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()。
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类的主方法中分别创建各子类的对象并调用各自的方法打印输出信息。
下面是代码实现:
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!
```
阅读全文