public interface Refuel{ //介面的方法,可視為抽象方法! public String doRefuel(); }
修改一下原來的 Sedan 類別:
public class Sedan extends Vehicle implements Refuel{ private char audioClass; private double fuelTank; Sedan(int wheels, String engineType, double weight, char audio , double fuelTank){ super(wheels,engineType,weight); this.audioClass = audio; this.fuelTank = fuelTank; } //實作抽象方法 public void ignite(){ System.out.println("Engine start!"); } //實作介面方的方法 public String doRefuel(){ this.fuelTank = 20.00; return "Refeuel :" + this.fuelTank; } }
實作 Sedan 類別:
public class Demo { public static void main(String[] args){ Sedan bm = new Sedan(4, "Diesel" , 2231.21 , 'A' , 0.0); System.out.println(bm.doRefuel()); bm.ignite(); } }