简体   繁体   English

在实现的抽象方法中调用超类方法

[英]Calling super class method inside implemented abstract method

Basicaly I have a need for several methods that do the same thing but with different parameters the sub-classes can chose from, and still force the implementation. Basicaly我需要几个方法做同样的事情,但子类可以选择不同的参数,并仍然强制实现。 Is this a correct approach/design ? 这是正确的方法/设计吗?

EDIT: I have edited the addItem() body, these methods contain the final logic that is used to handle the passed parameters 编辑:我编辑了addItem()体,这些方法包含用于处理传递参数的最终逻辑

public abstract Class A {
    public abstract void addItemImpl()

    addItem(String s) {
        // do stuff
    }
    addItem(Collection c) {
       // do stuff
    }
    addItem(Item item) {
        // do stuff
    }
}

public  Class B extends A {
    addItemImpl() {
        addItem("text, text2")
    }
}

public Class C extends A {
    addItemImpl() {
        addItem([item, item2])
    }
}

No, this will not work. 不,这不行。

You will not be able to define the "doStuff()" method because you have to handle the parameters. 您将无法定义“doStuff()”方法,因为您必须处理参数。 You provide not enough information to give you detailed help. 您提供的信息不足,无法为您提供详细的帮助。 But it's possible that generics might come in handy: 但是仿制药可能会派上用场:

public abstract Class A<T> {
    public addItem(T t) {
        // dostuff with t
    }
}

public  Class B extends A<String> {
}

public Class C extends A<Collection> {
}

What you have is technically correct, but with out knowing what addItem actually does it is difficult to know if this is the best solution. 你所拥有的在技术上是正确的,但是不知道addItem实际上做了什么,很难知道这是否是最好的解决方案。 My guess would be that there probably is a better way. 我的猜测是,可能有更好的方法。

If addItem essentially set values to be used in the doStuff, I would just do that work in the Class B and C instead. 如果addItem基本上设置了要在doStuff中使用的值,我只会在B类和C类中执行此操作。 Any others that need to do it the same way as B could extend it instead of A . 任何其他需要以与B相同的方式执行此操作的人可以将其扩展而不是A

Edit: Based on your edit, I would say this is probably a bad example to use an abstract class. 编辑:根据你的编辑,我会说这可能是一个使用抽象类的坏例子。 There is no truely shared functionality. 没有真正共享的功能。 An interface would be more appropriate as you need a different implementation for each. 接口更合适,因为您需要为每个接口实现不同的实现。 You are just trying to hide that inside an abstract class. 你只是试图在抽象类中隐藏它。 I would change A to an interface along with using generics. 我会将A更改为接口以及使用泛型。

Only go the abstract class route if there is actually shared code that is exactly the same in all the classes without having to do any tricks to make it work (like above). 如果实际上共享代码在所有类中完全相同而不必使任何技巧使其工作(如上所述),则只进入抽象类路由。

This is a perfect case for: Favor composition over inheritance. 这是一个完美的案例:支持组合而不是继承。

Your subclasses don't fully benefit from the superclass and don't depend on its implementation details. 您的子类不会从超类中充分受益,也不依赖于其实现细节。 Then define an interface for the contract B and C must obey ( addItemImpl() ) and compose them with A . 然后为契约B定义一个接口, C必须遵守( addItemImpl() )并用A组合它们。

Ask yourself: is B really an A ? 问问自己: B真的是A吗? is C really and A ? C真的和A

If you need force implementation for few methods, then Abstract methods are ideal. 如果您需要强制实现几种方法,那么Abstract方法是理想的。

But be careful only the very first Non-Abstract sub-class of the Super-class is bound to implement all the abstract methods in it.... 但要小心,只有超级类的 第一个 Non-Abstract 子类必然会实现其中的所有抽象方法....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM