class Demo3_Person { public static void main(String[] agse) { Person p = new Student("大雄",6,"男"); p.eat(); p.sleep();
System.out.println("------------------------------");
Person p1 = new Teacher("夯哥",28,"男"); p1.eat(); p1.sleep(); Teacher t = (Teacher)p1; t.yuxi(); } }
abstract class Person { private String name; private int age; private String gender;
public Person() {}
public Person(String name,int age,String gender) { this.name = name; this.age = age; this.gender = gender; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } public void setGender(String gender) { this.gender = gender; } public String getGender() { return gender; }
public abstract void eat(); public abstract void sleep(); }
interface Chouyan { public void yuxi(); }
class Student extends Person { public Student() {}
public Student(String name,int age,String gender) { super(name,age,gender); System.out.println("姓名:"+name+",年龄:"+age+",性别:"+gender); } public void eat() { System.out.println("爱上课睡觉!"); } public void sleep() { System.out.println("爱吃快餐!"); } }
class Teacher extends Person implements Chouyan{ public Teacher() {}
public Teacher(String name,int age,String gender) { super(name,age,gender); System.out.println("姓名:"+name+",年龄:"+age+",性别:"+gender); } public void eat() { System.out.println("在办公室睡觉!"); } public void sleep() { System.out.println("爱吃大餐!"); } public void yuxi() { System.out.println("玉溪不倒谁都不屌!"); } }
|