java之 介面的簡單使用方法———停車場是否可以停下車的問題
阿新 • • 發佈:2018-11-29
public class Parkinglot{ public static void main(String[] args){ Parking parking = new Parking(8,4); Car bus = new Bus(); parking.park(bus); } } class Parking{ private int standardLength = 1; private int standardWidth = 1; public Parking(int length,int width){ this.standardLength = length; this.standardWidth = width; } public void park(Car car){ if(car.length() <= standardLength&&car.width() <= standardWidth){ System.out.println("這個車可以停"+car); }else{ System.out.println("這個車不可以停"+car); } } } interface Car{ //介面 int length(); //車的長度 寬度(單位m) int width(); } class Bus implements Car{ public int length(){ return 9; } public int width(){ return 4; } public String toString(){ return"bus length = "+this.length()+" bus width ="+this.width(); } }