gpt4 book ai didi

java - 学习接口(interface)和层次结构,在哪里放置某些变量和方法?

转载 作者:行者123 更新时间:2023-11-30 10:45:58 24 4
gpt4 key购买 nike

因此,作为汽车租赁系统的一部分,我需要编写类来表示大型和小型汽车,它们之间的区别在于它们具有不同大小的油箱并且以不同的速率消耗燃料。目前我的方法是有一个接口(interface) Car,由一个抽象类 AbstractCar 实现,它由两个具体类 SmallCar 和 LargeCar 扩展。然而,这是我第一次使用接口(interface)和抽象类(我们只是在类里面介绍它们,这个作业旨在评估我们对它们的了解),我不知道在哪个类中放置什么。

fill方法的实现完全一样,只需要引用正确的FUEL_CAPACITY值即可,所以感觉应该在AbstractCar类中实现这些方法,但后来不知道怎么获取引用正确的 FUEL_CAPACITY 值。字段 fuelLevel 显然也由所有汽车持有,所以我觉得我应该在 AbstractCar 中声明它,但如果不删除它的隐私,我就无法从子类访问它。

谁能帮我弄清楚我做错了什么或对接口(interface)和继承有什么误解?我一直在考虑的一件事是生成一个枚举 CarType,让 AbstractCar 将 CarType 作为一个字段,所有实现都在 AbstractCar 类中完成,使用 if 语句切换到正确的 FUEL_CAPACITY 值,并简单地使用 SmallCar 和 LargeCar 作为构造函数或没有太多甚至没有任何实际实现的工厂类。

提前感谢您提供的任何帮助,我意识到它有点啰嗦,但是我试图确保我完全理解我们正在学习的概念并且我正在正确地实现它们,而不是仅仅将“有效”的东西混在一起' 但不一定是正确或最优雅的解决方案。

最佳答案

您可以使用您指出的值将逻辑传输到 AbstractCar。然后只需在 SmallCar 和 LargeCar 的构造函数中设置这些值。这将是一种方法。正如您所指出的,您始终必须在父类中拥有通用逻辑。你想避免重复的代码。然后你只需要确保在构造函数中设置不同的值。如果您知道固定值(就像您从给定的示例中所做的那样),您甚至可以省略为 SmallCar 或 LargeCar 构造函数提供参数,而只需在构造函数内的 super() 调用中设置这些固定值。

这是我的解决方案的实现。

接口(interface) Car,我在其中删除了 getFuelMethod() 方法,因为必须保护访问级别:

public interface Car {

RegistrationNumber getRegistration();

int getFuelCapacity();

// int getFuelLevel(); this can not be implemented
// all methods in an interface are PUBLIC
// so you have to lower the access level by removing it from the interface

// HERE goes the rest of the method signatures

}

抽象类AbstractCar:

public abstract class AbstractCar implements Car {
// this is the common variable
// that is why we save it in the parent class
private int fuelCapacity;

private int fuelLevel;

// we forward the value to the parent constructor with the super call
public AbstractCar(int fuelCapacity) {
this.fuelCapacity = fuelCapacity;
// I set the value to 0 for the start, but
// you can also pass the value to the super call,
// same as fuelCapacity - it is up to you
this.fuelLevel = 0;
}

// The getters and setter allow us to retrieve the values
// from the abstract class through capsulation!

// here we have the getter to be able to retrieve the value from SmallCar and LargeCar
public int getFuelCapacity() {
return.fuelCapacity;
}

public void setFuelCapacity(int fuelCapacity) {
this.fuelCapacity = fuelCapacity;
}

protected int getFuelLevel() {
return fuelLevel;
}

protected void setFuelLevel(int fuelLevel) {
this.fuelLevel = fuelLevel;
}

// HERE goes the rest of the code

}

这是 SmallCar 的实现:

public class SmallCar extends AbstractCar {

private static final int FUEL_CAPACITY = 45;

public SmallCar() {
// we set the value in the parent class
super(FUEL_CAPACITY);
}

public int drive() {
// HERE goes the logic for drive for SmallCar. Same method is needed
// in the LargeCar class, because the logic differes.
}

// HERE goes the rest of the code

}

关于java - 学习接口(interface)和层次结构,在哪里放置某些变量和方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36799232/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com