Java 继承 练习题 1

     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);
	}
	
}

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值