父母 - 车。子类:电力或化石。儿童:私家车或出租车

时间:2017-01-26 17:43:16

标签: java

我最近拿起OOP,这让我非常困惑。 我想把超级轿车和电动或化石汽车作为其子类。

`abstract class Electric extends Car`. 

问题是我只想制作私人或出租车的实例。我如何构建这个,以便理想情况下我可以作为

发表声明
Taxi cab1 = new Fossil()

我意识到这是一个有明显答案的问题,但在看到仿制药和界面之后,直到我的眼睛酸痛,这种挫折感一直在增长。

如果有人能提醒我如何做到这一点,我将不胜感激,因为我很肯定我已经知道并且忘记了。

2 个答案:

答案 0 :(得分:1)

你在想这样的事吗?

汽车类:

public class Car{

private int yearOfManufacture;
private String brand;
private String model;
private Engine engine;  

public Car(String pBrand, String pModel, int pYearOfManufacture, Engine pEngine){
    this.yearOfManufacture = pYearOfManufacture;
    this.brand = pBrand;
    this.model = pModel;
    this.engine = pEngine;
}

}

引擎类:

public class Engine {

 private int power;
 private int emission;

 public Engine(int power, int emission) {
  this.power = power;
  this.emission = emission;
 }
}

IC引擎类:

public class ICEngine extends Engine {

 private int numOfValves;

 public ICEngine(int power, int emission, int numOfValves) {
  super(int power, int emission);
  this.numOfValves = numOfValves;
 }
}

电动引擎类:

public class ElectricEngine extends Engine {

 private int wattage;

 public ICEngine(int power, int emission, int wattage) {
  super(int power, int emission);
  this.wattage = wattage;
 }
}

出租车类:

public class Taxi extends Car{

    private String driverName;

    public Taxi(String driverName, String pBrand, String pModel, int pYearOfManufacture, Engine engine){
        super(pBrand, pModel, pYearOfManufacture, engine);
        this.driverName = driverName;
    }
}

私家车类:

public class PrivateCar extends Car {

 private String ownerName;

 public PrivateCar(String ownerName, String pBrand, String pModel, int pYearOfManufacture, Engine engine) {
  super(pBrand, pModel, pYearOfManufacture, engine);
  this.ownerName = ownerName;
 }


}

使用应用类:

public class MyApp {

 public static void main(String[] args) {

  Engine eEngine1 = new ElectricEngine(100, 10, 10000);
  Car myElectricTaxi = new Taxi("Driver1", "Honda", "Accord", 2013, eEngine1);

  Engine icEngine1 = new ICEngine(100, 10, 6);
  Car myICTaxi = new Taxi("Driver2", "Honda", "Accord", 2013, icEngine1);

  Engine eEngine2 = new ElectricEngine(100, 10, 10000);
  Car myElectricPrivateCar = new PrivateCar("Owner1", "Honda", "Accord", 2013, eEngine2);

  Engine icEngine2 = new ICEngine(100, 10, 4);
  Car myICPrivateCar = new PrivateCar("Owner2", "Honda", "Accord", 2013, icEngine2);

 }

}

答案 1 :(得分:0)

汽车类:

public class Car{

private int yearOfManufacture;
private String brand;
private String model;

private boolean forRent;


public Car(String pBrand, String pModel, int pYearOfManufacture, boolean pForRent){
    this.yearOfManufacture = pYearOfManufacture;
    this.brand = pBrand;
    this.model = pModel;
    this.forRent = pForRent;
}


public boolean isPrivate(){
    return (!forRent);
}

public int getYear()                
    {
        return this.yearOfManufacture;
    }

public String getBrand()
    {
        return this.brand;
    }

public void setYear(int year)
    {
        this.yearOfManufacture = year;
    }

public void setBrand(String carBrand)
    {
        this.brand = carBrand;
    }

public void setModel(String model)                        
    {
        this.model = model;   

public String getModel()
    {
        return this.model;
    }

}

电动车类:

public class ElectricCar extends Car{

    private int electricPowerOutput;

    public ElectricCar(String pBrand, String pModel, int pYearOfManufacture, int pElectricPowerOutput, boolean pForRent){
        super(pBrand,pModel,pYearOfManufacture,pForRent);
        this.electricPowerOutput = pElectricPowerOutput;
    }

    public int getElectricPowerOutput(){
        return this.electricPowerOutput;
    }

    public void setElectricPowerOutput(int electricPower){
        this.electricPowerOutput = electricPower;
    }

}

化石车:

public class FossilCar extends Car{

    private int emissionRating;

    public ElectricCar(String pBrand, String pModel, int pYearOfManufacture, int pEmissionRating, boolean pForRent){
        super(pBrand,pModel,pYearOfManufacture,pForRent);
        this.emissionRating = pEmissionRating;
    }

    public int getEmissionRating(){
        return this.emissionRating;
    }

    public void setElectricPowerOutput(int emissionRating){
        this.emissionRating = emissionRating;
    }

}

希望这会有所帮助!!