1. 程式人生 > >Week4 互評作業 (介面、抽象類練習)

Week4 互評作業 (介面、抽象類練習)

編寫一個小的程式,其中定義一些介面、類、抽象類,定義它們的成員(欄位及方法), 要求使用使用setter/getter, static, final, abstract,@Override等語法要素,並寫一個main函式來使用它們。這些類、介面可以是圍繞以下選題之一

飛翔世界:來一次飛翔接力(即多個可飛翔的物件依次呼叫);

動物世界:來一次吃西瓜大賽;

圖書館:模擬一天的借閱過程;

學校:模擬選課過程;

等等

package week4;

public class FlyGame {
	public static void main(String[] args) {
		Bird bird1 = new Bird();
		bird1.setFlyDistance(10);
		bird1.setType("dujuan");
		Bird bird2 = new Bird();
		bird1.setFlyDistance(20);
		bird1.setType("bugu");
		AirPlane air1 = new AirPlane();
		air1.setFlyDistance(1000);
		air1.setName("by747");
		AirPlane air2 = new AirPlane();
		air2.setFlyDistance(2000);
		air2.setName("dafeiji");
		System.out.println("飛行接力開始");
		air1.fly();
		bird1.fly();
		bird2.fly();
		air2.fly();
		
	}
}

interface flyable{
	void fly();
}

abstract class animal {
	public void eat(){}
	public void sleep(){}
	
}
//鳥類繼承動物類,並有飛的能力
class Bird extends animal implements flyable{
	private String type;
	private double flyDistance;
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public double getFlyDistance() {
		return flyDistance;
	}
	public void setFlyDistance(double flyDistance) {
		this.flyDistance = flyDistance;
	}
	public static final int maxFlyDistance=100;
	@Override
	public void fly() {
		System.out.println(type+"起飛");
		System.out.println("我飛"+flyDistance+"m");
		System.out.println("降落");
	}
	
}
//飛機實現飛行介面
class AirPlane implements flyable{
	private String name;
	private double flyDistance;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getFlyDistance() {
		return flyDistance;
	}
	public void setFlyDistance(double flyDistance) {
		this.flyDistance = flyDistance;
	}
	@Override
	public void fly() {
		System.out.println(name+"起飛");
		System.out.println("我飛"+flyDistance+"m");
		System.out.println("降落");		
		
	}
	
}