Java 家长车。子类:电力或化石。孩子:私家车还是出租车
我最近学了OOP,这让我很困惑。 我想制造一辆超级级汽车和电动或化石汽车作为它的子类Java 家长车。子类:电力或化石。孩子:私家车还是出租车,java,Java,我最近学了OOP,这让我很困惑。 我想制造一辆超级级汽车和电动或化石汽车作为它的子类 `abstract class Electric extends Car`. 问题是,我只想举私人或出租车的例子。我该如何组织这一点,以便理想情况下我可以作为 Taxi cab1 = new Fossil() 我意识到这是一个答案显而易见的问题,但在看了泛型和接口之后,直到我的眼睛发痛,挫折感一直在增长 如果有人能提醒我如何做到这一点,我将不胜感激,因为我确信我已经知道,而且已经忘记了。汽车类: 电动汽车
`abstract class Electric extends Car`.
问题是,我只想举私人或出租车的例子。我该如何组织这一点,以便理想情况下我可以作为
Taxi cab1 = new Fossil()
我意识到这是一个答案显而易见的问题,但在看了泛型和接口之后,直到我的眼睛发痛,挫折感一直在增长
如果有人能提醒我如何做到这一点,我将不胜感激,因为我确信我已经知道,而且已经忘记了。汽车类:
电动汽车类别:
化石汽车:
希望这有帮助 你是这样想的吗 汽车类别: 发动机等级: IC发动机等级: 电动发动机类别: 出租车等级: 私家车等级: 使用应用程序类:
在绘制类层次结构之前,还要阐明您希望这些对象做什么,因为这将确定作为接口或抽象类有什么意义,以及它们之间的关系应该是什么。例如,出租车和私家车有不同的方法吗?很好。Private和Taxi将有一些不共享的方法,电动出租车将需要从electric继承所有方法,然后是Car。出租车也可以是化石,在这种情况下,它需要继承所有化石方法,而不是电气方法。电动和化石的常用方法将在Car中找到。您可能希望将发动机类型划分为一个单独的类,并使用它来组合自定义类型,例如
Taxi cab1=new Taxi(new ElectricMotor())代码>。事实上,“组件”模式将非常有意义。请注意:您永远无法执行“Taxi cab1=new Fossil()”,因为这需要您可以将Fossil投射到Taxi中。但不是每辆化石车都是出租车!谢谢你的努力。这虽然不是问题本身。我遇到的问题是试图把一辆出租车打成一个延伸汽车的化石。
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;
}
}
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;
}
}
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);
}
}