编写一个Java应用程序,除主类外,还有3个类:People、ChinaPeople和BeijingPeople。1、People类具有描述身高和体重的属性,以及sperkHello()、averageHeight()、averageWeight()方法。 2、ChinaPeople类是People类的子类,重写父类的sperkHello()、averageHeight()、averageWeight()方法,新增chinaGongfu()(输出中国武术口号)方法。 3、BeijingPeople类是Ch
时间: 2024-02-05 20:02:29 浏览: 111
inaPeople类的子类,重写父类的sperkHello()、chinaGongfu()方法,新增beijingOpera()(输出京剧唱段)方法。
以下是对应的Java代码:
```
public class People {
private double height;
private double weight;
public People(double height, double weight) {
this.height = height;
this.weight = weight;
}
public void sperkHello() {
System.out.println("Hello, I'm a people.");
}
public double averageHeight() {
return height;
}
public double averageWeight() {
return weight;
}
}
public class ChinaPeople extends People {
public ChinaPeople(double height, double weight) {
super(height, weight);
}
@Override
public void sperkHello() {
System.out.println("你好,我是中国人。");
}
@Override
public double averageHeight() {
return super.averageHeight() + 0.05;
}
@Override
public double averageWeight() {
return super.averageWeight() + 2.0;
}
public void chinaGongfu() {
System.out.println("中华武术,千锤百炼才能成。");
}
}
public class BeijingPeople extends ChinaPeople {
public BeijingPeople(double height, double weight) {
super(height, weight);
}
@Override
public void sperkHello() {
System.out.println("您好,我是北京人。");
}
@Override
public void chinaGongfu() {
System.out.println("北京折子戏,一出好戏连台戏。");
}
public void beijingOpera() {
System.out.println("梨园弟子,万众瞩目。");
}
}
```
在这个程序中,People类是一个基础类,具有身高和体重的属性以及一些基础方法,ChinaPeople类继承了People类,并重写了一些方法,新增了一个chinaGongfu()方法,表示中国人的武术口号,BeijingPeople类继承了ChinaPeople类,并重写了一些方法,新增了一个beijingOpera()方法,表示北京人的京剧唱段。这样,我们就可以通过继承来构建一个具有继承关系的类结构,实现更加丰富的功能。
阅读全文