实验5.1 子类和父类在同一包中的继承性
【实验要求】编写应用程序,有4个类文件:People.java,Student.java,UniverStudent.java,Example5_1.java,保存在同一目录中。
【实验步骤】
People.java
public class People {
int age,leg = 2,hand = 2;
protected void showPeopleMess() {
System.out.printf("%d岁,%d只脚,%d只手\t",age,leg,hand);
}
}
Student.java
public class Student extends People {
int number;
void tellNumber() {
System.out.printf("学号:%d\t",number);
}
int add(int x,int y) {
return x+y;
}
}
UniverStudent.java
public class UniverStudent extends Student {
int multi(int x,int y) {
return x*y;
}
}
Example5_1.java
public class Example5_1 {
public static void main(String args[]) {
Student zhang = new Student();
【代码1】 //访问继承的成员变量age,并赋值17
zhang.number=100101;
【代码2】 //调用继承的方法showPeopleMess()
zhang.tellNumber();
int x=9,y=29;
System.out.print("会做加法:");
int result=zhang.add(x,y);
System.out.printf("%d+%d=%d\n",x,y,result);
UniverStudent geng = new UniverStudent();
【代码3】 //访问继承的成员变量age,并赋值21
geng.number=6609;
【代码4】 //调用继承的方法showPeopleMess()
geng.tellNumber(); //调用继承的方法
System.out.print("会做加法:");
result=geng.add(x,y); //调用继承的方法
System.out.printf("%d+%d=%d\t",x,y,result);
System.out.print("会做乘法:");
result=geng.multi(x,y);
System.out.printf("%d×%d=%d\n",x,y,result);
}
}
运行代码:
People.java
public class People {
int age,leg = 2,hand = 2;
protected void showPeopleMess() {
System.out.printf("%d岁,%d只脚,%d只手\t",age,leg,hand);
}
}
Student.java
public class Student extends People {
int number;
void tellNumber() {
System.out.printf("学号:%d\t",number);
}
int add(int x,int y) {
return x+y;
}
}
UniverStudent.java
public class UniverStudent extends Student {
int multi(int x,int y) {
return x*y;
}
}
Example5_1.java
public class Example5_1 {
public static void main(String args[]) {
Student zhang = new Student();
zhang.age=17; //访问继承的成员变量age,并赋值17
zhang.number=100101;
zhang.showPeopleMess(); //调用继承的方法showPeopleMess()
zhang.tellNumber();
int x=9,y=29;
System.out.print("会做加法:");
int result=zhang.add(x,y);
System.out.printf("%d+%d=%d\n",x,y,result);
UniverStudent geng = new UniverStudent();
geng.age=21; //访问继承的成员变量age,并赋值21
geng.number=6609;
geng.showPeopleMess(); //调用继承的方法showPeopleMess()
geng.tellNumber(); //调用继承的方法
System.out.print("会做加法:");
result=geng.add(x,y); //调用继承的方法
System.out.printf("%d+%d=%d\t",x,y,result);
System.out.print("会做乘法:");
result=geng.multi(x,y);
System.out.printf("%d×%d=%d\n",x,y,result);
}
}
运行结果:
实验5.2 子类与对象
【实验要求】 编写应用程序,子类ChinaPeople的对象调用继承的方法操作未被继承的变量。
【实验步骤】
Example5_2.java
class People {
private int averHeight = 166;
public int getAverHeight() {
return averHeight;
}
}
class ChinaPeople extends People {
int height;
public void setHeight(int h) {
height = h;
}
public int getHeight() {
return height;
}
}
public class Example5_2 {
public static void main(String args[]) {
ChinaPeople zhangSan = new ChinaPeople();
/* 子类ChinaPeople的对象zhangSan通过调用继承的方法getAverHeight()得到变量averageHeight的值 */
System.out.println("子类对象未继承的averageHeight的值是:"+【代码1】);
zhangSan.setHeight(178);
// zhangSan通过调用方法getHeight()得到height的值
System.out.println("子类对象的实例变量height的值是:"+【代码2】);
}
}
运行代码:
class People {
private int averHeight = 166;
public int getAverHeight() {
return averHeight;
}
}
class ChinaPeople extends People {
int height;
public void setHeight(int h) {
height = h;
}
public int getHeight() {
return height;
}
}
public class Example5_2 {
public static void main(String args[]) {
ChinaPeople zhangSan = new ChinaPeople();
/* 子类ChinaPeople的对象zhangSan通过调用继承的方法getAverHeight()得到变量averageHeight的值 */
System.out.println("子类对象未继承的averageHeight的值是:"+zhangSan.getAverHeight());
zhangSan.setHeight(178);
// zhangSan通过调用方法getHeight()得到height的值
System.out.println("子类对象的实例变量height的值是:"+zhangSan.getHeight());
}
}
运行结果:
实验5.3 中国人、北京人和美国人
【实验要求】编写程序模拟中国人、美国人是人,北京人是中国人。除主类外,程序中还有4个类:People、ChinaPeople、AmericanPeople和BeijingPeople类。
【实验步骤】
People.java
public class People {
protected double weight,height;
public void speakHello() {
System.out.println("yayayaya");
}
public void averageHeight() {
height=173;
System.out.println("average height:"+height);
}
public void averageWeight() {
weight=70;
System.out.println("average weight:"+weight);
}
}
ChinaPeople.java
public class ChinaPeople extends People {
public void speakHello() {
System.out.println("您好");
}
public void averageHeight() {
height = 168.78;
System.out.println("中国人的平均身高:"+height+" 厘米");
}
【代码1】 //重写averageWeight()方法,输出:"中国人的平均体重:65公斤"
public void chinaGongfu() {
System.out.println("坐如钟,站如松,睡如弓");
}
}
AmericanPeople.java
public class AmericanPeople extends People {
【代码2】 //重写speakHello()方法,输出"How do you do"
【代码3】 //重写averageHeight()方法,输出"American's average height:176 cm"
public void averageWeight() {
weight = 75;
System.out.println("American's average weight:"+weight+" kg");
}
public void americanBoxing() {
System.out.println("直拳、钩拳、组合拳");
}
}
BeijingPeople.java
public class BeijingPeople extends ChinaPeople {
【代码4】 //重写averageHeight()方法,输出:"北京人的平均身高: 172.5厘米"
【代码5】 //重写averageWeight()方法,输出:"北京人的平均体重:70公斤"
public void beijingOpera() {
System.out.println("花脸、青衣、花旦和老生");
}
}
Example.java
public class Example {
public static void main(String args[]) {
ChinaPeople chinaPeople = new ChinaPeople();
AmericanPeople americanPeople = new AmericanPeople();
BeijingPeople beijingPeople = new BeijingPeople();
chinaPeople.speakHello();
americanPeople.speakHello();
beijingPeople.speakHello();
chinaPeople.averageHeight();
americanPeople.averageHeight();
beijingPeople.averageHeight();
chinaPeople.averageWeight();
americanPeople.averageWeight();
beijingPeople.averageWeight();
chinaPeople.chinaGongfu();
americanPeople.americanBoxing();
beijingPeople.beijingOpera();
beijingPeople.chinaGongfu();
}
}
运行代码:
People.java
public class People {
protected double weight,height;
public void speakHello() {
System.out.println("yayayaya");
}
public void averageHeight() {
height=173;
System.out.println("average height:"+height);
}
public void averageWeight() {
weight=70;
System.out.println("average weight:"+weight);
}
}
ChinaPeople.java
public class ChinaPeople extends People{
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+"公斤");
}//重写averageWeight()方法,输出:"中国人的平均体重:65公斤"
public void chinaGongfu() {
System.out.println("坐如钟,站如松,睡如弓");
}
}
AmericanPeople.java
public class AmericanPeople extends People {
public void speakHello(){ //重写speakHello()方法,输出"How do you do"
System.out.println("How do you do");
}
public void averageHeight(){ //重写averageHeight()方法,输出"American's average height:176 cm"
height=176;
System.out.println("American's average height:"+height+"cm");
public void averageWeight() {
weight = 75;
System.out.println("American's average weight:"+weight+" kg");
}
public void americanBoxing() {
System.out.println("直拳、钩拳、组合拳");
}
}
BeijingPeople.java
public class BeijingPeople extends ChinaPeople {
public void averageHeight(){ //重写averageHeight()方法,输出:"北京人的平均身高: 172.5厘米"
height=172.5;
System.out.println("北京人的平均身高:"+height+"cm");
}
public void averageWeight(){ //重写averageWeight()方法,输出:"北京人的平均体重:70公斤"
weight=70;
System.out.println("北京人的平均体重:"+weight+"公斤");
}
public void beijingOpera() {
System.out.println("花脸、青衣、花旦和老生");
}
}
Example.java
public class Example {
public static void main(String args[]) {
ChinaPeople chinaPeople = new ChinaPeople();
AmericanPeople americanPeople = new AmericanPeople();
BeijingPeople beijingPeople = new BeijingPeople();
chinaPeople.speakHello();
americanPeople.speakHello();
beijingPeople.speakHello();
chinaPeople.averageHeight();
americanPeople.averageHeight();
beijingPeople.averageHeight();
chinaPeople.averageWeight();
americanPeople.averageWeight();
beijingPeople.averageWeight();
chinaPeople.chinaGongfu();
americanPeople.americanBoxing();
beijingPeople.beijingOpera();
beijingPeople.chinaGongfu();
}
}
运行结果: