簡體   English   中英

理解實現 Java 擴展類

[英]understanding implementing Java Extended Classes

我目前正在學習 Java,我的任務是理解 OOP。 我了解類的理論,但我對實現有疑問。

以下面的代碼為例:

class Vehicle {
  private String engine;
  private int wheels;
  private int seats;
  private int fuelTank;
  private String lights;

  public Vehicle() {
    this.engine = "petrol";
    this.wheels = 4;
    this.seats = 4;
    this.fuelTank = 35;
    this.lights = "LED";

  }

  public Vehicle(String engine, int wheels, int seats, int fuelTank, String lights) {

    this.engine = engine;
    this.wheels = wheels;
    this.seats = seats;
    this.fuelTank = fuelTank;
    this.lights = lights;
  }

  public String getEngine() {
    return engine;
  }

  public int getWheels() {
    return wheels;
  }

  public int getSeats() {
    return seats;
  }

  public int getFueTank() {
    return fuelTank;
  }

  public String getLights() {
    return lights;
  }

}

class Car extends Vehicle {
  private String steering;
  private String musicSystem;
  private String airConditioner;
  private String fridge;
  private String entertainmentSystem;

  public Car() {
    super();
    this.steering = "Power Steering";
  }

  public Car(String steering, String engine, int wheels, int seats, int fueTank, String lights) {
    super(engine, wheels, seats, fueTank, lights);
    this.steering = steering;
  }

  public String getSteering() {
    return steering;
  }
}

class Demo {
  public static void main(String[] args) {

    Car car = new Car("Power steering", "deisel", 4, 4, 40, "LED");

    System.out.println("Steering: " + car.getSteering());
    System.out.println("Engine type: " + car.getEngine());
    System.out.println("Number of seats: " + car.getSeats());
    System.out.println("Fuel tank capacity: " + car.getFueTank());
    System.out.println("Head lamp type: " + car.getLights());
    System.out.println("Number of wheels: " + car.getWheels());
  }

}

我在這里理解,您可以通過使用默認構造函數或參數化構造函數來創建Vehicle對象。 我也可以對從Vehicle擴展的Car對象做同樣的事情。 我知道通過在默認構造函數中使用super()它將使用Vehicle的默認構造函數以及Car對象轉向變量 default。 與使用super(args)的參數化構造函數相同。

我在理解如何在每個擴展類別的車輛中對車輪進行硬編碼時遇到問題。

為了解釋更多,我不想在創建Car對象時在構造函數中包含輪子。 我希望它默認為 4。另外,如果我要創建一個從Vehicle擴展的Bike對象,我希望輪子變量默認為 2。

如果您真的想使用您的超類屬性,請將它們的訪問修飾符類型更改為受保護,否則您將無法直接在您的子類中使用它們。

然后,您可以使用 IIB(實例初始化塊)為每個不同的子類定義輪子變量值。

示例解決方案:

車輛.java

public class Vehicle{

    //attributes with protected access modifier
    protected String engine;
    protected int wheels;
    protected int seats;
    protected int fuelTank;
    protected String lights;

    public Vehicle(){//No-args constructor

    }

    
     public Vehicle(String engine, int seats, int fuelTank, String lights) {

         this.engine = engine;
         this.seats = seats;
         this.fuelTank = fuelTank;
         this.lights = lights;

    }

     public Vehicle(String engine, int wheels, int seats, int fuelTank, String lights) {

         this.engine = engine;
         this.wheels = wheels;
         this.seats = seats;
         this.fuelTank = fuelTank;
         this.lights = lights;
     }

       
}

Car.java

public class Car extends Vehicle{

     private String steering;
     private String musicSystem;
     private String airConditioner;
     private String fridge;
     private String entertainmentSystem;

    {//IIB block to initialize wheels with default value

        wheels = 4;//Defining the wheels variable 


    }

     public Car(String steering,String engine,int seats,int fuelTank,String lights) 
     {//Overloaded constructor
         super(engine,seats,fuelTank,lights);
         this.steering = steering;

     }

}

您可以按如下方式進行。

private final String steering; // final = constant, will not change.

public Car(String steering, String engine, int seats, int fueTank, String lights) {
    super(engine, 4 /*wheels*/, seats, fueTank, lights);
    this.steering = steering;
}

public Car(String steering, String engine, int fueTank, String lights) {
    this(steering, engine, 4 /*seats*/, fueTank, lights);
}

正如您在此處看到的,多個構造函數和許多參數可能會變得很麻煩。

有時最好為特殊字段設置 setter。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM