[英]Reference to superclass how can I use subclass methods
Wasn't sure how to call my title, because I'm not sure how to call that problem right now. 不确定如何称呼我的头衔,因为我现在不确定如何称呼该问题。 I got one superclass
and three subclasses
. 我有one superclass
和three subclasses
。 The subclasses
only got an own cunstructor
, all other methods and attributes are listed in the superclass
. subclasses
只有一个自己的cunstructor
,所有其他方法和属性都在superclass
中列出。 Now one of those three subclasses
have to contain a new attribute delay
and a method for that. 现在,这三个subclasses
必须包含一个新的属性delay
及其方法。 But if I implement a somewhat generic
method (because it doesn't care which subclass is using it, because it checks it in the method itself.) but in the superclass in the method I want that if my object is of that specific subclass that it can access the delay. 但是,如果我实现了某种generic
方法(因为它不在乎哪个子类正在使用它,因为它会在方法本身中进行检查。)但是在方法的超类中,我希望如果我的对象属于该特定子类,它可以访问延迟。
Anyone can tell me how to achieve this without implementing the attribute into the superclass itself? 任何人都可以告诉我如何在不将属性实现到超类本身的情况下实现此目标? (Only want that an object this subclass can access this delay attribute) (只希望该子类的对象可以访问此delay属性)
else if (this instanceof repairCar) {
if(this.getDelay() != 0){
}
}
Edit: 编辑:
There is the superclass: Car
有超类: Car
This contains methods like setSpeed and corresponding attributes
ant a method called drive.
now there are the subclasses: FastCar
NormalCar
RepairCar
which all only got a constructor in the subclass. 现在有子类: FastCar
NormalCar
RepairCar
,所有子类中都只有一个构造函数。
The RepairCar should have another attribute delay
due to it being repaired right now. 由于目前正在维修,RepairCar应该有另一个属性delay
。
In the superclass in the drive method
it checks whether the Car is a FastCar
, NormalCar
or RepairCar
. 在drive method
的超类中,它检查Car是FastCar
, NormalCar
还是RepairCar
。 When it is a RepairCar
it should check if the delay is 0 so It can drive otherwise it will wait a turn and lower the delay until its 0. 当它是RepairCar
,应检查延迟是否为0,以便可以行驶,否则它将等待转弯并降低延迟直到其为0。
Now I want that only my RepairCar got this delay attribute and not my other two subclasses. 现在,我只希望我的RepairCar获得此delay属性,而不希望其他两个子类。 But if my drive method is in the superclass it wont let me reference to the delay of a RepairCar due to the superclass not having this attribute. 但是,如果我的驱动器方法在超类中,则由于超类不具有此属性,因此我不会引用RepairCar的延迟。
Any way to implement the drive method for all 3 Cartypes in the Superclass without implementing the delay into the superclass? 有什么方法可以对超类中的所有3种Cartype实现驱动方法,而无需对超类实现延迟?
Why should the superclass be responsible for this? 为什么超类对此负责? This should only be done by the specific class. 这只能由特定的类来完成。
Eg 例如
public class Car {
public void drive() {
// do something all cars do
}
}
Your subclass is: 您的子类是:
public class RepairCar extends Car {
public int delay() {
return this.delay;
}
@Override
public void drive() {
if (delay() == 0) {
super.drive();
} else {
decreaseDelay();
}
}
...
}
Couldn't you do something like: 你不能做这样的事情:
abstract class AbstractCar {
public void performAction() {
// stuff
doSomething();
}
protected abstract void doSomething();
static class RepairCar extends AbstractCar {
private int delay;
@Override
protected void doSomething() {
// do something based on delay
}
}
static class FastCar extends AbstractCar {
@Override
protected void doSomething() {
// do something with no delay
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.