JAVA多态及接口练习

要求:有三种交通工具飞机、轮船、汽车

          它们有各自的行驶方式(水、路、空)

          它们有各自的载人数量

          它们有各自的行驶速度

          只有轮船和汽车可以打开窗子

          用多态和接口实现:

public class text001 {
	public static void main(String[] args) {
		AAA(new car(), "陆地行使", 20, 90.0);
		AAA(new plan(), "空中行使", 60, 1000.0);
		AAA(new ship(), "水上行使", 40, 300.0);
		SSS();
	}

	public static void AAA(jiaotonggongju Q, String a, int b, double c) {
		Q.HMseat(b);
		Q.HMspeed(c);
		Q.Howwork(a);
	}
	public static void SSS(){
	    OpenWindow C=new car();
	    C.open();
	    OpenWindow D=new ship();
	    D.open();
	}
}

=========================================================================================
public class jiaotonggongju {
	private String work;
	private int seat;
	private double speed;

	public String getWork() {
		return work;
	}

	public void setWork(String work) {
		this.work = work;
	}

	public int getSeat() {
		return seat;
	}

	public void setSeat(int seat) {
		this.seat = seat;
	}

	public double getSpeed() {
		return speed;
	}

	public void setSpeed(double speed) {
		this.speed = speed;
	}

	public void Howwork(String b) {

	}

	public void HMseat(int a) {

	}

	public void HMspeed(double c) {

	}
}

=========================================================================================
public class plan extends jiaotonggongju {
	public void Howwork(String b) {
		this.setWork(b);
		System.out.println("飞机的工作方式为:" + getWork());
	}

	public void HMseat(int a) {
		this.setSeat(a);
		System.out.println("飞机能坐的人数为:" + getSeat());
	}

	public void HMspeed(double c) {
		this.setSpeed(c);
		System.out.println("飞机的速度为:" + getSpeed());
	}
}
=========================================================================================
public class ship extends jiaotonggongju implements OpenWindow {
	public void Howwork(String b) {
		this.setWork(b);
		System.out.println("船的工作方式为:" + getWork());
	}

	public void HMseat(int a) {
		this.setSeat(a);
		System.out.println("船能坐的人数为:" + getSeat());
	}

	public void HMspeed(double c) {
		this.setSpeed(c);
		System.out.println("船的速度为:" + getSpeed());
	}
	public void open(){
		System.out.println("船可以打开窗子");
	}
}
=========================================================================================
public class car extends jiaotonggongju implements OpenWindow {
	public void Howwork(String b) {
		this.setWork(b);
		System.out.println("汽车的工作方式为:" + getWork());
	}

	public void HMseat(int a) {
		this.setSeat(a);
		System.out.println("汽车能坐的人数为:" + getSeat());
	}

	public void HMspeed(double c) {
		this.setSpeed(c);
		System.out.println("汽车的速度为:" + getSpeed());
	}
   public void open(){
	   System.out.println("汽车能开窗子");
   }
}
=========================================================================================
public interface OpenWindow {
void open();
}

 

你可能感兴趣的:(JAVA学习篇)