Java Learning 8:実用的なオブジェクト指向プログラミング(北京大学&Xidian実験的コンテンツ):最初のトピック:「The Flying World」

最初のトピック:北京大学の唐先生

ここに画像の説明を挿入
ここには4つの選択肢があり、4人全員がそれを行います。

最初のサブトピック:空飛ぶ世界:空飛ぶリレー

package Main;

public class Main{
    
    
	static int FlyLen = 1000;
	static int AllTime = 0;
	static String MVPName = "";
	static int EveFly = 0;
	static int MVPFly = 0;
	public static void main(String[] args){
    
    
		Bird bird = new Bird("鸽子", 6, 20, 5, 2, 3, "扇翅膀", 100);
		
		AirPlane ap = new AirPlane(800, "China", 100, 10, 20, 4, "波音747");
		
		int i = 1;
		while(FlyLen > 0) {
    
    
			if(1 == i % 2) {
    
     
				System.out.println("现在出场的是第 " + i + " 位选手:" + bird.GetterName());
				System.out.println("快看:它起飞了!");
				bird.SetOff(bird.GetterAct(), bird.GetterUpTime());
				bird.FlyTim(bird.GetterHigh(), bird.GetterTimeOnce());
				bird.SetDow(bird.GetterAct(), bird.GetterDownTime());
				Main.AllTime += bird.GetterAllTime();
				Main.EveFly++;
				Main.FlyLen -= bird.GetterTimeOnce() * bird.GetterFly();
				if(Main.MVPFly < bird.GetterFly()) {
    
    
					Main.MVPFly = bird.GetterFly();
					Main.MVPName = bird.GetterName();
				}
				System.out.println();
			}
			else {
    
    
				System.out.println("现在出场的是第 " + i + " 位选手:" + ap.GetterName());
				System.out.println("快看:它起飞了!");
				ap.SetOff(ap.GetterUpTime());
				ap.FlyTim(ap.GetterTimeOnce());
				ap.SetDow(ap.GetterDownTime());
				Main.AllTime += ap.GetterAllTime();
				Main.EveFly++;
				Main.FlyLen -= ap.GetterTimeOnce() * ap.GetterFly();
				if(Main.MVPFly < ap.GetterFly()) {
    
    
					Main.MVPFly = ap.GetterFly();
					Main.MVPName = ap.GetterName();
				}
			}
			i++;
			System.out.println();
		}
		System.out.println("激烈的接力赛终于是结束了!!!揭晓今天的MVP是:" + Main.MVPName + ",它的飞翔能力是:" + Main.MVPFly);
	}	
}

interface WanaFly {
    
    
	abstract void SetOff(String Act, int Time);
	abstract void FlyTim(int High, int Time);
	abstract void SetDow(String Act, int Time);
}

abstract class Animal{
    
    
	protected String name;
	int age;
	int FlyEng;// 飞翔能力:每分钟能够飞多远
	Animal(String name, int age, int FlyEng){
    
    
		this.name = name;
		this.age = age;
		this.FlyEng = FlyEng;
	}
	abstract void Discribe();
}

class Bird extends Animal implements WanaFly {
    
    
	private int TimeOnce;// 一次飞翔能坚持的时间
	private int UpTime;
	private int DownTime;
	private String Act;
	private int High;
	
	public int GetterTimeOnce() {
    
    
		return this.TimeOnce;
	}
	
	public int GetterAllTime() {
    
    
		return this.TimeOnce + this.UpTime + this.DownTime;
	}
	
	public int GetterUpTime() {
    
    
		return this.UpTime;
	}
	
	public int GetterDownTime() {
    
    
		return this.DownTime;
	}
	
	public String GetterAct() {
    
    
		return this.Act;
	}
	
	public int GetterHigh() {
    
    
		return this.High;
	}
	
	public String GetterName() {
    
    
		return this.name;
	}
	
	public int GetterFly() {
    
    
		return super.FlyEng;
	}
	
	public Bird(String name, int age, int FlyEng, int TimeOnce, int UpTime, int DownTime, String Act, int High){
    
    
		super(name, age, FlyEng);
		this.TimeOnce = TimeOnce;
		this.UpTime = UpTime;
		this.DownTime = DownTime;
		this.Act = Act;
		this.High = High;
	}
	
	public void Discribe() {
    
    
		System.out.println("这只鸟的名字叫:" + this.name + ",它的年龄是:" + this.age + ",它的飞行能力是:" + this.FlyEng + "每分钟");
	}
	
	public void SetOff(String Act, int Time) {
    
    
		System.out.println(this.name + "的起飞姿势是:" + Act + ",它起飞的时间是:" + Time);
	}
	
	public void FlyTim(int High, int Time) {
    
    
		System.out.println(this.name + "它的飞翔能达到的高度是:" + High + "在这个高度下它能飞:" + Time);
	}
	
	public void SetDow(String Act, int Time) {
    
    
		System.out.println(this.name + "的降落姿势是:" + Act + ",它的降落用时:" + Time);
	}
}

abstract class Vehicle{
    
    
	int price;
	String BuiFie;
	int FlyEng;
	
	Vehicle(int price, String BuiFie, int FlyEng) {
    
    
		this.price = price;
		this.BuiFie = BuiFie;
		this.FlyEng = FlyEng;
	}
}

class AirPlane extends Vehicle {
    
    
	private int UpTime;
	private int DownTime;
	private int TimeOnce;
	private String name;
	
	AirPlane(int price, String BuiFie, int FlyEng, int UpTime, int DownTime, int TimeOnce, String name) {
    
    
		super(price, BuiFie, FlyEng);
		this.UpTime = UpTime;
		this.DownTime = DownTime;
		this.TimeOnce = TimeOnce;
		this.name = name;
	}
	
	public int GetterAllTime() {
    
    
		return this.TimeOnce + this.UpTime + this.DownTime;
	}
	
	public int GetterTimeOnce() {
    
    
		return this.TimeOnce;
	}
	
	public int GetterUpTime() {
    
    
		return this.UpTime;
	}
	
	public int GetterDownTime() {
    
    
		return this.DownTime;
	}
	
	public String GetterName() {
    
    
		return this.name;
	}
	
	public int GetterFly() {
    
    
		return super.FlyEng;
	}
	
	public void SetOff(int Time) {
    
    
		System.out.println(this.name + " 起飞需要的时间是:" + Time);
	}
	
	public void FlyTim(int Time) {
    
    
		System.out.println(this.name + " 飞翔需要的时间是:" + Time);
	}
	
	public void SetDow(int Time) {
    
    
		System.out.println(this.name + " 降落需要的时间是:" + Time);
	}
}

空飛ぶ世界の結果は次のとおりです。

ここに画像の説明を挿入

備考:

この実験のコード量は比較的大量です!質問は全部で6つくらいありますので、ゆっくりやりましょう!
質問や良い提案があれば、コメントしたり、個人的に私とチャットしたいです!私の前任者の同僚が私のコードについて助言してくれることを願っています。

おすすめ

転載: blog.csdn.net/qq_44274276/article/details/105008863