需求:
南方人和北方人都有姓名,年龄以及性别,都有"吃饭"的功能,但是南方人经常吃米饭,北方人经常吃面条,
南方人主要还去经商,北方人主要考学,请使用面向对象编程并且使用多态形式进行测试南方人和北方人!
class Person{
private String name;
private int age;
private String sex;
public Person(){
}
public Person(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public void eat(){
System.out.println("人喜欢吃东西");
}
}
class SouthPerson extends Person{
public SouthPerson() {
}
public SouthPerson(String name, int age, String sex) {
super(name, age, sex);
}
public void eat(){
System.out.println("南方人喜欢吃米饭");
}
public void work(){
System.out.println("男方人擅长经商");
}
}
class NorthPerson extends Person{
public NorthPerson() {
}
public NorthPerson(String name, int age, String sex) {
super(name, age, sex);
}
public void eat(){
System.out.println("北方人喜欢吃面食");
}
public void work(){
System.out.println("北方人要考学");
}
}
public class PersonTest{
public static void main(String[] args) {
Person p = new SouthPerson("广东人",30,"男");
System.out.println("姓名:"+p.getName()+",年龄:"+p.getAge()+",性别:"+p.getSex());
p.eat();
SouthPerson s = (SouthPerson) p;
s.work();
System.out.println("______________________");
Person p2 = new NorthPerson("北京人",28,"女");
System.out.println("姓名:"+p2.getName()+",年龄:"+p2.getAge()+",性别:"+p2.getSex());
p2.eat();
NorthPerson n = (NorthPerson) p2;
n.work();
}
}