[Java]
/*
* Start the program header annotation.
* Copyright and version Declaration of the program
* Copyright (c) 2011, a student from the computer College of Yantai University
* All rights reserved.
* File name: inherited exercise
* Author: Xue Guangchen
* Completion date: January 1, October 17, 2012
* Version No.: x1.0
* Description of tasks and Solutions
* Input description:
* Problem description: (1) encapsulate a People type with the height and weight attributes and the speakHello, averageHeight, and averageWeight functions.
(2) encapsulate a type of ChinaPeople that is a subclass of People. The chinaMartial function is added, and the override super class speakHello, averageHeight, and averageWeight functions are provided.
(3) encapsulate a class of AmericanPeople, which is a subclass of People. The AmericanBoxing function is added. The override superclasses include speakHello, averageHeight, and averageWeight.
(4) encapsulate the BeijingPeople type to be a subclass of ChinaPeople. The BeijingOpera function is added, and the override class speakHello, averageHeight, and averageWeight functions are added.
(5) Test the above objects with a program execution entry
* Program output:
* End the comment in the program Header
*/
// People class
Package xgc;
Public class People {
Private double height;
Private double weight;
Public People ()
{
This. height = 10;
This. weight = 10;
}
Public People (double height, double weight)
{
This. height = height;
This. weight = weight;
}
Public double getHeight (){
Return height;
}
Public void setHeight (double height ){
This. height = height;
}
Public double getWeight (){
Return weight;
}
Public void setWeight (double weight ){
This. weight = weight;
}
Public void speakHello ()
{
System. out. println ("Hello ");
}
Public void averageHeight ()
{
}
Public void averageWeight ()
{
}
}
// ChinaPeople
Package xgc;
Public class ChinaPeople extends People {
Public void chinaMartial ()
{
System. out. println ("ChinaPeople taiji very good ");
}
Public void speakHello ()
{
System. out. println ("Hello, ChinaPeople ");
}
Public void averageHeight ()
{
SetHeight (1.7 );
System. out. println ("ChinaPeople averageHeight =" + getHeight ());
}
Public void averageWeight ()
{
SetWeight (70 );
System. out. println ("ChinaPeople averageWeight =" + getWeight ());
}
}
// AmericanPeople
Package xgc;
Public class AmericanPeople extends People {
Public void AmericanBoxing (){
System. out. println ("AmericanPeople Boxing very good ");
}
Public void speakHello ()
{
System. out. println ("AmericanPeople say Hello ");
}
Public void averageHeight ()
{
SetHeight (1.8 );
System. out. println ("AmericanPeople averageHeight =" + getHeight ());
}
Public void averageWeight ()
{
SetWeight (75 );
System. out. println ("AmericanPeople averageWeight =" + getWeight ());
}
}
// BeijingPeople
Package xgc;
Public class BeijingPeople extends ChinaPeople {
Public void BeijingOpera (){
System. out. println ("BeijingOpera very good ");
}
Public void speakHello ()
{
System. out. println ("BeijingPeople say hello ");
}
Public void averageHeight ()
{
SetHeight (1.75 );
System. out. println ("ChinaPeople averageHeight =" + getHeight ());
}
Public void averageWeight ()
{
SetWeight (73 );
System. out. println ("AmericanPeople averageWeight =" + getWeight ());
}
}
// Test
Package xgc;
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 ();
System. out. println ();
}
}
Running result: