- public class CH01{
- public static void main(String[] args) {
- Human h1=new Human("湯尼",35,70);
- h1.showProfile();
- h1.eat(0.85);
- h1.showProfile();
- h1.swim(100);
- h1.sing("蝴蝶");
- h1.takeCare();
- }
- }
- abstract class Animal
- {
- int age;
- double w;
- Animal(int a,double w)
- {
- age=a;
- this.w=w;
- }
- abstract void eat(double x);
- abstract void showProfile();
- }
- interface Swimmer
- {
- String LEVEL="專業級";
- void swim(double x);
- }
- interface Singer
- {
- String LEVEL="專業級";
- void sing(String x);
- }
- interface Father
- {
- String LEVEL="新手級";
- void takeCare();
- }
- class Human extends Animal implements Swimmer, Singer, Father
- {
- String name;
- Human(String n,int a,double w)
- {
- super(a,w);
- name=n;
- }
- void showProfile()
- {
- System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
- }
- void eat(double x)
- {
- System.out.println(name+"咕嚕咕嚕吃下了"+x+"公斤的食物.");
- w+=x;
- }
- public void swim(double x)
- {
- System.out.println(name+"以"+Swimmer.LEVEL+"水準,刷刷刷快速游了"+x+"公尺.");
- }
- public void sing(String x)
- {
- System.out.println(name+"以"+Singer.LEVEL+"水準,唱了一首"+x+".");
- }
- public void takeCare()
- {
- System.out.println(name+"以"+Father.LEVEL+"水準,開始扮演父親的角色,照顧小孩.");
- }
- }
複製代碼 |