一、实验目的
掌握类的继承,方法的重写;
二、实验项目内容(实验题目)
1、编写程序模拟中国人、美国人是人,北京人是中国人。
有四个类,People、ChinaPeople、AmericanPeople和BeijingPeople类,要求如下:
People类有权限是protected的double型成员变量height和weight,以及public void speakHello()、public void averageHeight()和public void averageWeight()方法。
以上各方法内部,随便写一条输出语句即可。
ChinaPeople类是People的子类,新增了public void chinaGongfu方法。要求重写父类的public void speakHello()、public void averageHeight()和public void averageWeight()方法。
AmericanPeople类是People的子类,新增public void amercianBoxing()方法。
要求AmercicanPeople重写父类的public void speakHello()、public void averageHeight()和public void averageWeight()方法。
BeijingPeople类是ChinaPeople的子类,新增public void beijingOpera()方法。要求BeijingPeople重写父类的public void speakHello()、public void averageHeight()和public void averageWeight()方法。
以上各方法内部,随便写一条输出语句即可。
在主类中,分别对以上四个类进行实例化,并依次调用类中的所有方法。
三、源程序(实验步骤/实验过程/算法)
第一步先创建文件People、ChinaPeople、AmericanPeople和BeijingPeople类,
第二步People类有权限是protected的double型成员变量height和weight,
protected double height;
protected double weight;
第三步public void speakHello()、public void averageHeight()和public void averageWeight()方法。
public void speakHello() {
System.out.println();
}
// 语言
public void averageHeight() {
System.out.println("人平均身高为"+height);
}
// 平均身高
public void averageWeight() {
System.out.println("人平均体重为"+weight);
// 平均体重
}
第四步ChinaPeople类是People的子类,
public class ChinaPeople extends People
第五步新增了public void chinaGongfu方法。要求重写父类的public void speakHello()、public void averageHeight()和public void averageWeight()方法。
public void speakHello() {
System.out.println("你好");
}
// 语言
public void averageHeight() {
System.out.println("人平均身高为"+height);
}
// 平均身高
public void averageWeight() {
System.out.println("人平均体重为"+weight);
}
// 平均体重
public void chinaGongfu() {
System.out.println("成龙");
}
第六步AmericanPeople类是People的子类,
public class AmericanPeople extends People
第七步新增public void amercianBoxing()方法。要求AmercicanPeople重写父类的public void speakHello()、public void averageHeight()和public void averageWeight()方法。
public void speakHello() {
System.out.println("Hello");
}
// 语言
public void averageHeight() {
System.out.println("人平均身高为"+height);
}
// 平均身高
public void averageWeight() {
System.out.println("人平均体重为"+weight);
}
// 平均体重
public void amercianBoxing() {
System.out.println("美国拳击");
//美国拳击
}
第八步BeijingPeople类是ChinaPeople的子类,
public class BeijingPeople extends ChinaPeople
第九步新增public void beijingOpera()方法。要求BeijingPeople重写父类的public void speakHello()、public void averageHeight()和public void averageWeight()方法。
public void speakHello() {
System.out.println("你好");
}
//语言
public void averageHeight() {
height = 150;
System.out.println("人平均身高为"+height);
}
// 平均身高
public void averageWeight() {
weight = 80;
System.out.println("人平均体重为"+weight);
}
// 平均体重
public void beijingOpera() {
System.out.println("京剧");
// 京剧
}
四、源程序调试过程和(或)实验分析(完整的代码)
package shiyanba;
public class People {
protected double height;
protected double weight;
public void speakHello() {
System.out.println();
}
// 语言
public void averageHeight() {
System.out.println("人平均身高为"+height);
}
// 平均身高
public void averageWeight() {
System.out.println("人平均体重为"+weight);
// 平均体重
}
}
package shiyanba;
public class ChinaPeople extends People {
public void speakHello() {
System.out.println("你好");
}
// 语言
public void averageHeight() {
System.out.println("人平均身高为"+height);
}
// 平均身高
public void averageWeight() {
System.out.println("人平均体重为"+weight);
}
// 平均体重
public void chinaGongfu() {
System.out.println("成龙");
}
}
package shiyanba;
public class AmericanPeople extends People {
public void speakHello() {
System.out.println("Hello");
}
// 语言
public void averageHeight() {
System.out.println("人平均身高为"+height);
}
// 平均身高
public void averageWeight() {
System.out.println("人平均体重为"+weight);
}
// 平均体重
public void amercianBoxing() {
System.out.println("美国拳击");
//美国拳击
}
}
package shiyanba;
public class BeijingPeople extends ChinaPeople {
public void speakHello() {
System.out.println("你好");
}
//语言
public void averageHeight() {
height = 150;
System.out.println("人平均身高为"+height);
}
// 平均身高
public void averageWeight() {
weight = 80;
System.out.println("人平均体重为"+weight);
}
// 平均体重
public void beijingOpera() {
System.out.println("京剧");
// 京剧
}
}
package shiyanba;
public class Text {
public static void main(String[] args) {
// 创建、声明对象People
People people = new People();
// 调用People的方法
people.weight = 80;
people.height = 150;
people.speakHello();
people.averageHeight();
people.averageWeight();
// 创建、声明对象chinaPeople
ChinaPeople chinaPeople = new ChinaPeople();
// 调用ChinaPeople
chinaPeople.height = 80;
chinaPeople.weight = 100;
chinaPeople.averageHeight();
chinaPeople.averageWeight();
chinaPeople.speakHello();
// 创建、声明对象AmericanPeople
AmericanPeople americanPeople = new AmericanPeople() ;
// 调用AmericanPeople
americanPeople.height = 50;
americanPeople.weight = 90;
americanPeople.averageHeight();
americanPeople.averageWeight();
americanPeople.speakHello();
// 创建、声明对象 BeijingPeople
BeijingPeople beijingPeople = new BeijingPeople();
// 调用BeijingPeople
beijingPeople.height = 80;
beijingPeople.weight = 500;
beijingPeople.averageHeight();
beijingPeople.averageWeight();
beijingPeople.speakHello();
}
}
运行的结果