1.编写程序模拟中国人、美国人都是人,北京人是中国人。除主类外,该程序还有People、Chinese、American和Beijingman 4个类。要求如下:
1)People类有权限是protected的double型成员变量height和weight,以及public void speakHello()输出“yayaya”、public void averageHeight()设置height值为173,并输出height的值和public void averageWeight()方法设置weight值为70,并输出weight的值
2)Chinese类是People类的子类,新增了public void chinaGongfu()方法输出“坐如钟,站如松,睡如弓”,要求Chinese重写父类的public void speakHello()输出“您好”、public void averageHeight()设置height的值为168.78,输出中国人的平均身高为…和public void averageWeight()设置weight的值为65千克,输出中国人的平均体重为….
3) American类是People类的子类,新增了public void americanBoxing()方法输出“直拳、勾拳、组合拳”,要求American重写父类People类的public void speakHello()输出“how do you do”、public void averageHeight()设置height的值为176,并输入美国人的平均身高为….和public void averageWeight()方法设置weight的值为75,并输出美国人的平均身高为…。
4)Beijingman类是Chinese的子类,新增了public void beijingOpera()方法输出“花脸、青衣、花旦和老生”,要求Beijing重写父类的public void averageHeight()设置height为172.5,并输出北京人的平均身高为…和public void averageWeight()方法设置weight为70,并输入北京人的平均体重为….
5) 主类中创建Chinese\American\Beijingman的对象,并通过这些对象调用他们的方法,观察每个方法的输出。
public class Main {
public static void main(String[] args) {
Chinese chinese = new Chinese();
chinese.chinaGongfu();
chinese.speakHello();
chinese.averageHeight();
chinese.averageWeight();
American american = new American();
american.americanBoxing();
american.speakHello();
american.averageHeight();
american.averageWeight();
Beijingman beijingman = new Beijingman();
beijingman.beijingOpera();
beijingman.averageHeight();
beijingman.averageWeight();
}
}
class People {
protected double height;
protected double weight;
public void speakHello() {
System.out.println("yayaya");
}
public void averageHeight() {
height = 173;
System.out.println();
}
public void averageWeight() {
weight = 70;
System.out.println();
}
}
class Chinese extends People {
public void chinaGongfu() {
System.out.println("坐如钟,站如松,睡如弓");
}
public void speakHello() {
System.out.println("您好");
}
public void averageHeight() {
height = 168.78;
System.out.println("中国人的平均身高为" + height);
}
public void averageWeight() {
weight = 65;
System.out.println("中国人的平均体重为" + weight);
}
}
class American extends People {
public void americanBoxing() {
System.out.println("直拳、勾拳、组合拳");
}
public void speakHello() {
System.out.println("how do you do");
}
public void averageHeight() {
height = 176;
System.out.println("美国人的平均身高为" + height);
}
public void averageWeight() {
weight = 75;
System.out.println("美国人的平均体重为" + weight);
}
}
class Beijingman extends Chinese {
public void beijingOpera() {
System.out.println("花脸、青衣、花旦和老生");
}
public void averageHeight() {
height = 172.5;
System.out.println("北京人的平均身高为" + height);
}
public void averageWeight() {
weight = 70;
System.out.println("北京人的平均体重为" + weight);
}
}