Bootstrap

JAVA--第八周实验--继承的练习

题目:

(1)封装一个People类型,具有height和weight属性,具有speakHello、averageHeight、averageWeight功能。

(2)封装一类ChinaPeople类型是People的子类,新增chinaMartial功能,override超类的speakHello、averageHeight、averageWeight功能。

(3)封装一类AmericanPeople类型是People的子类,新增AmericanBoxing功能,override超类的speakHello、averageHeight、averageWeight功能。

(4)封装一类BeijingPeople类型是ChinaPeople的子类,新增BeijingOpera功能,override超类的speakHello、averageHeight、averageWeight功能。

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

People类型

abstract class People {
	float height;
	float weight;
	void speakHello(){
		System.out.println("Hello");
	}
	abstract void averageHeight();
	 void averageWeight(){
	}
}


AmericanPeople类型

public class AmericanPeople extends People{

	@Override
	void averageHeight() {
		// TODO Auto-generated method stub
		height = 1.8f;
		System.out.println("AmericanPeople height = "+height);
	}
	void speakHello(){
		System.out.println("AmericanPeople say Hello");
	}
	void averageWeight(){
		weight = 75;
		System.out.println("AmericanPeople weight = "+weight);
	}
	//People的子类,新增AmericanBoxing功能,override超类的speakHello、averageHeight、averageWeight功能。
	void AmericanBoxing(){
		System.out.println("AmericanPeople Boxing well");
	}
}


BeijingPeople类型

public class BeijingPeople extends ChinaPeople{
//是ChinaPeople的子类,新增BeijingOpera功能,override超类的speakHello、averageHeight、averageWeight功能。
	void averageHeight() {
		// TODO Auto-generated method stub
		height = 1.75f;
		System.out.println("BeijingPeople height = "+height);
	}
	void speakHello(){
		System.out.println("BeijingPeople say Hello");
	}
	void averageWeight(){
		weight = 73;
		System.out.println("BeijingPeople weight = "+weight);
	}
	void BeijingOpera(){
		System.out.println("BeijingPeople Opera best");
	}
}


ChinaPeople类型

public class ChinaPeople extends People{

	@Override
	void averageHeight() {
		// TODO Auto-generated method stub
		height = 1.7f;
		System.out.println("ChinaPeople height = "+height);
	}
	//新增chinaMartial功能,override超类的speakHello、averageHeight、averageWeight功能。
	void speakHello(){
		System.out.println("ChinaPeople say Hello");
	}
	void averageWeight(){
		weight = 70;
		System.out.println("ChinaPeople weight = "+weight);
	}
	void chinaMartial(){
		System.out.println("ChinaPeople Martial best");
	}
}

 

Test测试上述对象

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ChinaPeople CP = new ChinaPeople();
		CP.averageHeight();
		CP.averageWeight();
		CP.speakHello();
		CP.chinaMartial();
		
		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.averageHeight();
		BP.averageWeight();
		BP.BeijingOpera();

	}

}


 

运行结果:


;