黑马程序员技术交流社区
标题:
哈尔滨校区多态练习题
[打印本页]
作者:
xueming5500
时间:
2015-12-1 00:54
标题:
哈尔滨校区多态练习题
ss Demo11 {
public static void main(String[] args) {
BaseStudent a = new BaseStudent("王二",23);
System.out.println(a.getName()+".."+a.getAge());
a.study();
WorkStudent b = new WorkStudent("王三",23);
System.out.println(b.getName()+".."+a.getAge());
b.study();
SmokeBaseStudent c = new SmokeBaseStudent("胡志明",22);
System.out.println(c.getName()+".."+c.getAge());
c.study();
c.Smoke();
}
}
abstract class Student {
private String name;
private int age;
public Student() {}
public Student(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 study();
}
interface Smoking {
public void Smoke();
}
class BaseStudent extends Student {
public BaseStudent() {}
public BaseStudent(String name,int age) {
super(name,age);
}
public void study() {
System.out.println("学习j2se");
}
}
class SmokeBaseStudent extends BaseStudent implements Smoking {
public SmokeBaseStudent() {}
public SmokeBaseStudent(String name,int age) {
super(name,age);
}
public void study() {
System.out.println("学习j2se");
}
public void Smoke() {
System.out.println("不抽烟我浑身难受");
}
}
class WorkStudent extends Student {
public WorkStudent() {}
public WorkStudent(String name,int age) {
super(name,age);
}
public void study() {
System.out.println("学习android");
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2