標題:
PeopleLife
[打印本頁]
作者:
ray
時間:
2012-10-17 20:53
標題:
PeopleLife
public class People
{
private String mName;
private int mAge;
private boolean mSex;
public People(String name,int age, boolean sex)
{
this.mName = name;
this.mAge = age;
this.mSex = sex;
}
public void play(String what)
{
System.out.println(this.mName+"在玩"+what);
}
public void eat(String what)
{
System.out.println(this.mName+"在吃"+what);
}
public void action(int time)
{
switch(time)
{
case 1:
eat("早餐");
break;
case 2:
eat("午餐");
break;
case 3:
eat("晚餐");
play("遊戲");
break;
default:
;
}
}
}
複製代碼
作者:
ray
時間:
2012-10-17 21:20
public class Adult extends People
{
public Adult(String name,int age, boolean sex)
{
super(name,age,sex);
}
public void work()
{}
public void action(int time)
{
super.action(time);
switch(time)
{
case 1:
work();
break;
case 2:
work();
break;
case 3:
break;
default:
;
}
}
}
複製代碼
作者:
ray
時間:
2012-10-17 21:36
public class Doctor extends Adult
{
public Doctor(String name,int age, boolean sex)
{
super(name,age,sex);
}
public void work()
{
System.out.println(this.mName+"在看病");
}
}
複製代碼
作者:
ray
時間:
2012-10-17 21:37
public class LifeTest
{
public static void main(String[] args)
{
People ray = new Doctor("Ray",38,true);
ray.action(1);
ray.action(2);
ray.action(3);
}
}
複製代碼
作者:
ray
時間:
2012-10-19 19:38
public interface PartTime
{
public void hardwork();
}
複製代碼
作者:
ray
時間:
2012-10-19 19:46
public class YoungDoctor extends Doctor implements PartTime
{
public YoungDoctor(String name,int age, boolean sex)
{
super(name,age,sex);
}
@Override
public void hardwork()
{
System.out.println(this.mName+"在加班看病");
}
}
複製代碼
作者:
ray
時間:
2012-10-19 20:04
public class People
{
protected String mName;
protected int mAge;
protected boolean mSex;
public People(String name,int age, boolean sex)
{
this.mName = name;
this.mAge = age;
this.mSex = sex;
}
public void play(String what)
{
System.out.println(this.mName+"在玩"+what);
}
public void eat(String what)
{
System.out.println(this.mName+"在吃"+what);
}
public void action(int time)
{
switch(time)
{
case 1:
eat("早餐");
break;
case 2:
eat("午餐");
break;
case 3:
eat("晚餐");
if(this instanceof PartTime)
((PartTime)this).hardwork();
else
play("遊戲");
break;
default:
;
}
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2