简体   繁体   English

引用超类如何使用子类方法

[英]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 superclassthree 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是FastCarNormalCar还是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.

相关问题 如何在超类中使用子类方法? - How to use subclass methods in superclass? 如何从超类方法返回子类引用 - how can I return a subclass reference from superclass method 我如何在子类java中使用超类方法 - how can i use a superclass method in a subclass java 我可以在超类中使用子类的名称吗? - can i use the name of subclass in my superclass? 如果我使用超类来初始化子类对象,为什么该对象具有子类的属性,但是具有超类的方法呢? - If I use Superclass to initialize a subclass object, why will the object have the attributes of the subclass and but the methods of the superclass? 子类如何使用超类的比较器? - how can a subclass use the superclass's comparator? 如何在我创建了ArrayList的另一个类中使用超类的子类的方法 <SuperclassName> ? - How to use the methods of a subclass of a superclass in another class in which I created a ArrayList<SuperclassName>? 如何在一个子类中的不同方法中调用超类的几个对象? - How can I call few objects of superclass inside different methods in one subclass? 子类引用变量如何指向超类对象 - How subclass reference-variable can point to superclass object 如何从超类对象调用子类的方法 - How to call methods of subclass from superclass object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM