Inheritance, interface, polymorphism practice Java

Source: Internet
Author: User
Tags stub

Topic:

To implement a program in which the characters included in the program are divided by nationality: Chinese, American, Japanese, European these kinds of human beings have many of the same attributes (such as height, weight, etc.) and some peculiar attributes (such as the structure of the name are not the same) these people also have some of the same behavior, but the implementation of different ways ( such as eating useful chopsticks some with knives and forks and some hand-grasping, etc. these kinds of behavior can be achieved in a variety of ways (such as go to work can walk, drive, bike, etc.)

Package com.humanactivity;

public class Person {

protected float height;//Height
protected float weight;//Weight
String firstname;//Last Name
String middlename;//
String lastname;//Name
/**
*
* @param height
* @param weight
*/
Public person (float height, float weight) {

This.height = height;
This.weight = weight;
}

/**
* Travel
*/
public void travelling () {

}
/**
*
*/
public void Show () {
System.out.println ("Height: T" + this.height + "T weight: \ t" + this.weight);
}
public float getheight () {
return height;
}

public float getweight () {
return weight;
}

public void SetHeight (float height) {
This.height = height;
}

public void Setweight (float weight) {
This.weight = weight;
}

}

//////////////////////////////////////////////////////////////////////////////////////////////

Package com.humanactivity;

Public class American extends person implements Ibehaviour {

Public American (String lastname,string MiddleName,
String firstname,float height, float weight) {
Super (height, weight);
American name composition
Last name=family name Middle name is the middle name, which is usually a Christian or to commemorate who
First name + middle name = given name
For Example:john William Croft
String name=firstname+ "." +middlename+ "" +lastname;
System.out.println ("american:\t" +name);
TODO auto-generated Constructor stub
}

Implementing interface Methods
public void drink () {
TODO auto-generated Method Stub
System.out.println ("Americans like to drink Coca-Cola ...");
}

public void Drive () {
TODO auto-generated Method Stub
SYSTEM.OUT.PRINTLN ("Americans drive to work ...");
}

public void Eat () {
TODO auto-generated Method Stub
SYSTEM.OUT.PRINTLN ("Americans eat with knives and forks ...");
}

public void Onfoot () {
TODO auto-generated Method Stub

}
public void Ride_a_bike () {
TODO auto-generated Method Stub

}

/**
* Display Information
*/
public void Show () {
Super.show ();
}

/**
* Travel method
*/
public void travelling () {
Super.travelling ();
This.drive ();
}
}

////////////////////////////////////////////////////////////////////////////

Package com.humanactivity;

Public interface Ibehaviour {

/**
* Interface implementation is different is the behavior
*/
Eat
public void eat ();
Drink
public void drink ();
Walk
public void Onfoot ();
Drive
public void Drive ();
Ride a bike
public void Ride_a_bike ();

}

////////////////////////////////////////////////////////////////


Package com.humanactivity;

public class Testmain {

/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub

John William Croft
American A = New American ("Croft", "William", "John", 120, 45);
A.show ();
A.eat ();
A.drink ();
A.travelling ();

Last name + Name
Chinese c = new Chinese ("King", "Ming", 170, 45);
C.show ();
C.eat ();
C.drink ();
C.travelling ();

Japanese only last Name
Japanese j = new Japanese ("Sato", 160, 50);
J.show ();
J.eat ();
J.travelling ();

Japanese only last Name
European e = new European ("Hammer", "Victor", 175, 50);
E.show ();
E.eat ();
E.travelling ();

}
}

Related Article

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.