/*
需求:
老师和学生案例,加入抽烟的额外功能
*/
class Test_Person {
public static void main(String[] args) {
SomeStudent ss =new SomeStudent("刘亦菲",26);
ss.study();
ss.eat();
ss.smoke();
System.out.println("这个学生是" + ss.getName() +"她今年" + ss.getAge() + "岁了");
System.out.println("---------------------------------");
SomeTeacher st = new SomeTeacher();
st.setName("章老师");
st.setAge(36);
st.teach();
st.eat();
System.out.println("这个老师是" + st.getName() +"她今年" + st.getAge() + "岁了");
}
}
abstract class Person {
private String name;
private int age;
public Person(){}
public Person(String name,int age){
this.name =name;
this.age = age;}
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 abstract void eat();
}
class Teacher extends Person {
public Teacher(){}
public Teacher(String name,int age){
super(name,age);}
public void teach(){
System.out.println("老师需要讲课");}
public void eat(){
System.out.println("老师吃教师餐");}
}
class Student extends Person {
public Student(){}
public Student(String name,int age){
super(name,age);}
public void study(){
System.out.println("学生需要学习");}
public void eat(){
System.out.println("学生吃学生餐");}
}
interface Smoking {
public abstract void smoke();
}
class SomeStudent extends Student implements Smoking{
public SomeStudent(){}
public SomeStudent(String name,int age){
super(name,age);}
public void smoke(){
System.out.println("有些学生抽烟");}
}
class SomeTeacher extends Teacher implements Smoking{
public SomeTeacher(){}
public SomeTeacher(String name,int age){
super(name,age);}
public void smoke(){
System.out.println("有些老师抽烟");}
} |
|