飞翔世界:来一次飞翔接力(即多个可飞翔的对象依次调用)

 

题目:

  编写一个小的程序,其中定义一些接口、类、抽象类,定义它们的成员(字段及方法), 要求使用使用setter/getter, static, final, abstract,@Override等语法要素,并写一个main函数来使用它们。这些类、接口可以是围绕以下选题之一

飞翔世界:来一次飞翔接力(即多个可飞翔的对象依次调用);

动物世界:来一次吃西瓜大赛;

图书馆:模拟一天的借阅过程;

学校:模拟选课过程;

等等

要求写个简要说明。

 

接口:

 

public interface flyable {
	double FLYLENGTH=100.0;
	void takeoff();
	void fly();
	void land();
}

 

Animal类

 

abstract class Animal {
	private int speed;
	private String sex;
	public int getspeed(){
		return speed;
	}
	public void setspeed(int sp) {
		speed=sp;
	}
	public String getsex() {
		return sex;
	}
	public void sayhello() {
		System.out.println("hello!");
	}
	Animal(){}
	public Animal(int speed,String sex) {
		this.speed=speed;
		this.sex=sex;
	}
}

Vehicle类

abstract class Vehicle {
	private int speed;
	private int price;
	public int getspeed() {
		return speed;
	}
	public int getprice() {
		return price;
	}
	public void setspeed(int sp) {
		speed=sp;
	}
	Vehicle(){}
		public Vehicle(int speed,int price) {
			this.speed=speed;//解决局部变量与与变量同名的问题,访问局部变量
			this.price=price;
		}
	}

子类1:

public class kazi extends Animal implements flyable {
	int age;
	@Override
	public void takeoff() {
		System.out.println("kazi is takeoff!");

	}
	@Override
	public void fly() {
		int kazispeed=getspeed();
		System.out.printf("kazi fly %.2f seconds at a speed of %3d\n",FLYLENGTH/kazispeed,kazispeed);
	}
	@Override
	public void land() {
		System.out.println("kazi is land");
	}
	@Override
	public void sayhello() {
		System.out.println("hello! I'm kazi");
	}
	void intro() {
		System.out.printf("age:%d, sex:%s",age,getsex());
	}
	kazi(){
		super(100,"no need");
		age=18;
	}
}

子类2:

public class Bird extends Animal implements flyable {
	int age;
	@Override
	public void takeoff() {
		System.out.println("Bird is takeoff");
	}
	@Override
	public void fly() {
		int birdspeed=getspeed();
		System.out.printf("Bird fly %.2f seconds in a speed of %3d", FLYLENGTH/birdspeed,birdspeed);
	}
	@Override
	public void land() {
		System.out.println("Bird is land");
	}
	@Override
	public void sayhello() {
		System.out.println("hello! I'm Bird");
	}
	void intro() {
		System.out.printf("age:%d, sex:%s",age,getsex());
	}
	Bird(){
		super(200,"man");
		age=5;
	}
}

Vehicle的子类:

public class plane extends Vehicle implements flyable{
	@Override
	public void takeoff() {
		System.out.println("plane is takeoff");
	}
	@Override
	public void fly() {
		int planespeed=getspeed();
		int planeprice=getprice();
		System.out.printf("plane's speed is %3d,price is %ld", planespeed,planeprice);
	}
	@Override
	public void land() {
		System.out.println("plane is land");
	}
	plane(){
		super(400,20000000);
		
	}
}

主类:

import java.util.List;
import java.util.ArrayList;

public class flying{
	public static void main(String[] args) {
		List<flyable> Anlist = new ArrayList<>(); 
		for(int i=0;i<2;++i) {
			kazi kazi0=new kazi();
			kazi0.setspeed((int)(Math.random()*20));
			Anlist.add(kazi0);
			Bird bird0=new Bird();
			bird0.setspeed(5);
			Anlist.add(bird0);
			plane plane0=new plane();
			plane0.setspeed(20);
			Anlist.add(plane0);
		}
		/*飞翔接力*/
		for(flyable ani:Anlist) {//增强for语句
			if(ani instanceof kazi) {
				
				((kazi) ani).sayhello();
				((kazi) ani).intro();
			}
			ani.takeoff();
			ani.fly();
			ani.land();
		}
	}
}

代码都是我抄来的,心好累,里面的方法,关键字也不是很会用,有时间回来修改

阅读更多

没有更多推荐了,返回首页