class Demo_Person {
public static void main(String[] args) {
Student s = new Student("张三",23); //创建对象学生张三
System.out.println("他叫"+s.getName()+",他"+s.getAge()+"岁了,是学生");
s.eat();
s.study();
Teacher t = new Teacher("李四",24); //创建对象老师李四
System.out.println("他叫"+t.getName()+",他"+t.getAge()+"岁了,是老师");
t.eat();
t.teach();
System.out.println("-------------------------");
SmokeStudent ss = new SmokeStudent("王五",25); //创建抽烟学生对象王五
System.out.println("他叫"+ss.getName()+",他"+ss.getAge()+"岁了,是学生");
ss.eat();
ss.study();
ss.smoke();
SmokeTeacher st = new SmokeTeacher("朱六",26); //创建抽烟老师对象朱六
System.out.println("他叫"+st.getName()+",他"+st.getAge()+"岁了,是老师");
st.eat();
st.teach();
st.smoke();
}
}
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 = name;
}
public void setAge(int age) { //设置年龄
this.age = age;
}
public int getAge() { //获取年龄
return age;
}
public abstract void eat(); //抽象eat方法
}
interface Smoking { //抽烟接口
public void smoke(); //smoke方法
}
class Student extends Person { //学生类继承人类
public Student() {} //空参构造
public Student(String name,int age) { //带参构造
super(name,age);
}
public void eat() { //eat方法重写
System.out.println(this.getName()+"吃盒饭");
}
public void study() { //成员方法
System.out.println(this.getName()+"学习");
}
}
class SmokeStudent extends Student implements Smoking { //学生类继承人类并实现抽烟接口
public SmokeStudent() {} //空参构造
public SmokeStudent(String name,int age) { //带参构造
super(name,age);
}
public void smoke() { //smoke方法重写
System.out.println("他抽烟");
}
}
class Teacher extends Person { //老师类继承人类
public Teacher() {} //空参构造
public Teacher(String name,int age) { //带参构造
super(name,age);
}
public void eat() { //eat方法重写
System.out.println(this.getName()+"下馆子");
}
public void teach() { //成员方法
System.out.println(this.getName()+"讲课");
}
}
class SmokeTeacher extends Teacher implements Smoking { //老师类继承人类并实现抽烟接口
public SmokeTeacher() {} //空参构造
public SmokeTeacher(String name,int age) { //带参构造
super(name,age);
}
public void smoke() { //smoke方法重写
System.out.println("他抽烟");
}
}
运行结果就不发了
|
|