第八周任务一:继承的练习

好久没来过啦!差点就忘了这个大好的资源,回来看看收获颇丰,看到好多老朋友,现在也来发篇博文吧,不再是C++的文章啦!改成java啦

/* 
  * Copyright (c) 2011, 烟台大学计算机学院 
  * All rights reserved. 
  * 文件名称:test.cpp 
  * 作    者:  田庆
  * 完成日期:2012 年 10 月 18 日 
  * 版 本 号:v1.0 
  * 
  * 输入描述:无 
  * 问题描述
  * 程序输出
  * 问题分析:略
  * 算法设计:略 
  */ 

父类People

package cn.com;
//封装一个People类型,具有height和weight属性,具有speakHello、averageHeight、averageWeight功能。
public class People {
	float height;
	float weight;
	public People(){
		this.height=10.5f;
		this.weight=45.3f;	
		}
	public People(float height,float weight){
		this.height=height;
		this.weight=weight;
	}
	void speakHeight(){
		System.out.print("Hello");
	}
	void averageHeight(){
		
	}
	void averageWeight(){
		
	}
	public float getHeight() {
		return height;
	}
	public void setHeight(float height) {
		this.height = height;
	}
	public float getWeight() {
		return weight;
	}
	public void setWeight(float weight) {
		this.weight = weight;
	}

}

继承类:chinaPeople

package cn.com;
//封装一类ChinaPeople类型是People的子类,新增chinaMartial功能,
//override超类的speakHello、averageHeight、averageWeight功能。

public class chinaPeople extends People{
	void chinaMartial(){
		System.out.println("chinaPeople can play gongfu!!");
	}
	void speakHello(){
		System.out.println("chinaPeople speak Hello!");
	}
	void averageHeight(){
		//setHeight(12.1f);
	 System.out.println("chinaPeople的身高为:"+getHeight());
	}
	void averageWeight(){
		weight=54.2f;
		System.out.println("chinaPeople的体重为:"+weight);
	}

}


继承类:AmericanPeople

package cn.com;
//封装一类AmericanPeople类型是People的子类,新增AmericanBoxing功能,
//override超类的speakHello、averageHeight、averageWeight功能。

public class AmericanPeople extends People{
	void AmericanBoxing(){
		System.out.println("What's the meaning of AmericanBoxing");
	}
	void speakHello(){
		System.out.println("AmericanPeople speak Hello !");
	}
	void averageHeight(){
		// height=10.1f;
		 System.out.println("AmericanPeople的身高为:"+height);
		}
	void averageWeight(){
			weight=50.2f;
			System.out.println("AmericanPeople的体重为:"+weight);
			
		}

}


继承类:BeijingPeople

package cn.com;
//封装一类BeijingPeople类型是ChinaPeople的子类,新增BeijingOpera功能,
//override超类的speakHello、averageHeight、averageWeight功能。

public class BeijingPeople extends People{
	void BeijingOpera(){
		System.out.println("BeijingOpera is very good!");
	}
	void speakHello(){
		System.out.println("BeijingPeople speak Hello !");
	}
	void averageHeight(){
		 height=15.1f;
		 System.out.println("BeijingPeople的身高为:"+height);
		}
	void averageWeight(){
		setWeight(84.2f);
		System.out.println("BeijingPeople的体重为:"+getWeight());
		}
}


测试类:Test

package cn.com;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		chinaPeople cp=new chinaPeople();
		cp.speakHello();
		cp.chinaMartial();
		cp.averageHeight();
		cp.averageWeight();
		System.out.println();
		
		
		AmericanPeople ap=new AmericanPeople();
		ap.speakHello();
		ap.AmericanBoxing();
		ap.averageHeight();
		ap.averageWeight();
		System.out.println();
		
		BeijingPeople bp=new BeijingPeople();
		bp.speakHello();
		bp.BeijingOpera();
		bp.averageHeight();
		bp.averageWeight();
		
		

	}

}


运行结果:

chinaPeople speak Hello!
chinaPeople can play gongfu!!
chinaPeople的身高为:10.5
chinaPeople的体重为:54.2

AmericanPeople speak Hello !
What's the meaning of AmericanBoxing
AmericanPeople的身高为:10.5
AmericanPeople的体重为:50.2

BeijingPeople speak Hello !
BeijingOpera is very good!
BeijingPeople的身高为:15.1
BeijingPeople的体重为:84.2



 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值