java--Eighth Week experiment--the practice of inheriting __java

Source: Internet
Author: User

Topic:

(1) Encapsulation of a people type, with the height and weight properties, with Speakhello, Averageheight, Averageweight function.

(2) Package A class of chinapeople type is people, add chinamartial function, override Super Speakhello, Averageheight, Averageweight function.

(3) Package A class of americanpeople type is people, add americanboxing function, override Super Speakhello, Averageheight, Averageweight function.

(4) Package A class of beijingpeople type is chinapeople, add Beijingopera function, override Super Speakhello, Averageheight, Averageweight function.

(5) Use a program to execute entrance test to test the above object.

People type:

Abstract class People {
	float height;
	float weight;
	void Speakhello () {
		System.out.println ("Hello");
	}
	abstract void Averageheight ();
	 void Averageweight () {
	}
}


Americanpeople type

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 = n;
		System.out.println ("americanpeople weight =" +weight);
	}
	People subclass, add americanboxing function, override Speakhello, Averageheight, averageweight function of super class.
	void Americanboxing () {
		System.out.println ("Americanpeople Boxing OK");
	}


Beijingpeople type

public class Beijingpeople extends chinapeople{
//Is chinapeople subclass, new Beijingopera feature, override for Super class Speakhello, Averageheight, Averageweight function.
	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 =;
		System.out.println ("beijingpeople weight =" +weight);
	}
	void Beijingopera () {
		System.out.println ("Beijingpeople Opera best");
	}


Chinapeople type

public class Chinapeople extends people{

	@Override
	void Averageheight () {
		//TODO auto-generated method stub
		height = 1.7f;
		System.out.println ("chinapeople height =" +height);
	}
	Add Chinamartial function, override Speakhello, Averageheight, Averageweight function.
	void Speakhello () {
		System.out.println ("Chinapeople say Hello");
	}
	void Averageweight () {
		weight =;
		System.out.println ("chinapeople weight =" +weight);
	}
	void Chinamartial () {
		System.out.println ("Chinapeople martial Best");
	}

Test the above object

public class Test {

	/**
	 * @param args */public
	static void Main (string[] args) {
		//TODO Auto-generat Ed 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 ();

	}



Run Result:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.