黑马程序员技术交流社区
标题:
关于抽象、继承的案例:老师和学生
[打印本页]
作者:
tf0612
时间:
2015-11-13 22:02
标题:
关于抽象、继承的案例:老师和学生
/*
老师和学生案例,加入抽烟的额外功能
属性:姓名,年龄
行为:学生学习,老师教学,学生和老师都睡觉
*/
class Demo_People {
public static void main(String[] args) {
Student s = new Student("嘉嘉",19);
System.out.println("*学生***"+s.getName()+"***年龄*"+s.getAge()+"****");
s.study();
s.sleep();
System.out.println();
Teacher t = new Teacher("艳艳",24);
System.out.println("*老师***"+t.getName()+"***年龄*"+t.getAge()+"****");
t.teach();
t.sleep();
System.out.println();
SmokeTeacher st = new SmokeTeacher("李龙",32);
System.out.println("*老师***"+st.getName()+"**年龄**"+st.getAge());
st.smoke();
System.out.println();
SmokeStudent ss = new SmokeStudent("张海",29);
System.out.println("*老师***"+ss.getName()+"**年龄**"+ss.getAge());
ss.smoke();
}
}
abstract class People {
private String name;
private int age;
public People(){}
public People(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 sleep();
}
class Student extends People {
public Student(){}
public Student(String name,int age){
super(name,age);
}
public void sleep(){
System.out.println("趴着睡");
}
public void study() {
System.out.println("好好学习,天天向上");
}
}
class Teacher extends People {
public Teacher(){}
public Teacher(String name,int age) {
super(name,age);
}
public void sleep(){
System.out.println("躺着睡");
}
public void teach(){
System.out.println("教学授课");
}
}
interface Smoke {
public void smoke();
}
class SmokeStudent extends Student implements Smoke {
public SmokeStudent() {}
public SmokeStudent(String name,int age) {
super(name,age);
}
public void smoke(){
System.out.println("爱抽烟");
}
}
class SmokeTeacher extends Teacher implements Smoke {
public SmokeTeacher () {}
public SmokeTeacher (String name,int age) {
super(name,age);
}
public void smoke(){
System.out.println("爱抽烟");
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2