The polymorphism of north and south food culture
Require:
1. Define four classes in a file named Test.java: human Person, southern human SouthPerson and northern human NorthPerson inherit Person, test class Test, and write simple comments while writing code.
2. Human Person is required to reflect the following:
(1) Private member variables: name, age
(2) Member method: setXxx()/getXxx() method
Show( ) method displays member information
The eat() method displays "eat!"
3. SouthPerson inherits Person, rewrites the eat() method to display "Southerners like to eat rice!", and gives the member method DoBusiness() to display "Southerners are good at doing business".
4. NorthPerson inherits Person, rewrites the eat() method to show "Northerners like to eat noodles!", and gives the member method Research() to show that "Northerners are good at doing research".
5. The test class Test requires the implementation:
(1) Test the southerners: Create a Person object p with polymorphism, use the setXxx() method to assign a value to it ("Chen Guo", 40), and call its show() method, eat() method, and DoBusiness() method.
(2) Test the northerners: use polymorphism to assign the northerners to the object p, use the setXxx() method to assign values to it ("Li Ping", 16), call it to call the show() method, eat() method, Research( )method.
Source code:
package TestPerson;
//Humanity
class Person {
private String name;
private int age;
public Person() {
super();
// TODO Auto-generated constructorstub
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void show() {
System.out.println("Name: " + name + ", Age: " + age);
}
public void eat() {
System.out.println("Eating");
}
}
// southerners
class SouthPersonextends Person {
public void eat() {
System.out.println("Southerners like to eat rice");
}
public void DoBusiness() {
System.out.println("Southerners are good at doing business");
}
}
// northerner
class NorthPersonextends Person {
public void eat() {
System.out.println("Northerners like to eat noodles");
}
public void Research() {
System.out.println("Northerners like to do research");
}
}
// test class
public class Test{
// test for southerners
public static void main(String[] args) {
Person p = new SouthPerson();
p.setName("Chen Guo");
p.setAge(40);
p.show();
p.eat();
SouthPerson s = (SouthPerson) p;
s.DoBusiness ();
// test northerners
p = new NorthPerson();
p.setName("Li Ping");
p.setAge(16);
p.show();
p.eat();
NorthPerson n = (NorthPerson) p;
n.Research();
}
}