Java继承的进一步联系

题目:

1)封装一个People类型,具有heightweight属性,具有speakHelloaverageHeightaverageWeight功能。

2)封装一类ChinaPeople类型是People的子类,新增chinaMartial功能,override超类的speakHelloaverageHeightaverageWeight功能。

3)封装一类AmericanPeople类型是People的子类,新增AmericanBoxing功能,override超类的speakHelloaverageHeightaverageWeight功能。

4)封装一类BeijingPeople类型是ChinaPeople的子类,新增BeijingOpera功能,override超类的speakHelloaverageHeightaverageWeight功能。

5)用一个程序执行入口Test测试上述对象。

 

源代码:

people.java

 

package com.ytu.guo;

class people
	{
		private double height;
		private double weight;
		
		public people(double height, double weight){
			this.height = height;
			this.weight = weight;
		}

		public void speakHello(){
			System.out.println("hello!");
		}
		public void averageHeight(people p){
			System.out.println("The avrage height is  "+(this.height + p.height)/2);
		}
		public void averageWeight(people p){
			System.out.println("The avrage weight is  "+(this.weight + p.weight)/2);

		}
	}
class ChinaPeople extends people{
		public ChinaPeople(double height, double weight) {
		super(height, weight);
		// TODO Auto-generated constructor stub
	}

		public void ChinaMartial(){
			System.out.println("ChinaMartial");
		}
	}
class AmericanPeople extends people{
		public AmericanPeople(double height, double weight) {
		super(height, weight);
		// TODO Auto-generated constructor stub
	}

		public void AmericanBoxing(){
			System.out.println("AmericanBoxing");
		}
	}
class BeijingPeople extends ChinaPeople{
		public BeijingPeople(double height, double weight) {
		super(height, weight);
		// TODO Auto-generated constructor stub
	}

		public void BeijingOpera(){
			System.out.println("BeijngOpera");
		}
	}

	

Test.java

package com.ytu.guo;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		people p1 = new people(182,68);
		people p2 = new people(178,62);
		
		p1.speakHello();
		p1.averageHeight(p2);
		p1.averageWeight(p2);
		
		ChinaPeople cp1 = new ChinaPeople(175,62);
		cp1.ChinaMartial();
		
		AmericanPeople ap1 = new AmericanPeople(198,100);
		ap1.AmericanBoxing();
		
		BeijingPeople bp1 =new BeijingPeople(162,60);
		bp1.BeijingOpera();

	}

}


运行结果:

hello!
The avrage height is  180.0
The avrage weight is  65.0
ChinaMartial
AmericanBoxing
BeijngOpera



 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值