如何在不使用抽象的情况下强制实现子类中的方法? [英] How to force implementation of a method in subclass without using abstract?

查看:25
本文介绍了如何在不使用抽象的情况下强制实现子类中的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想强制子类实现我母类的一个实现方法.我看这个 Java - 强制实现已实现的方法 但我可以不要将我的母类转换为抽象类.

I want to force subclass to implement an implemented method of my mother class. I look this Java - Force implementation of an implemented method but i can't convert my mother class to an abstract class.

public class myMotherClass { 

   myMethod {

      ...some code ..

   }

}

public class myClass extends myMotherClass {

   myMethod {

      ... other code ...
   }

}

所以,在这个例子中,我想强制 myClass 实现 myMethod.

So, in this exemple, I want to force myClass implement myMethod.

对不起我的英语...

推荐答案

您不能强制子类重写方法.你只能通过抽象来强制它实现一个方法.

You can not force a subclass to override a method. You can only force it to implement a method by making it abstract.

因此,如果您不能使 myMotherClass 抽象,则只能引入另一个扩展 myMotherClass 并委托给必须实现的方法的超类:

So if you can not make myMotherClass abstract you can only introduce another superclass that extends myMotherClass and delegates to the method that must be implemented:

public abstract class EnforceImplementation extends myMotherClass {

        public final void myMethod(){
             implementMyMethod();
        }

        public abstract void implementMyMethod();
}

编辑

我在 hemcrest api 中发现了另一种解决问题的有趣方法,例如由 mockito 使用.

I found another interessting way of solving the problem in the hemcrest api that is e.g. used by mockito.

public interface Matcher<T> extends SelfDescribing {

    /**
     * Evaluates the matcher for argument <var>item</var>.
     * <p/>
     * This method matches against Object, instead of the generic type T. This is
     * because the caller of the Matcher does not know at runtime what the type is
     * (because of type erasure with Java generics). It is down to the implementations
     * to check the correct type. 
     *
     * @param item the object against which the matcher is evaluated.
     * @return <code>true</code> if <var>item</var> matches, otherwise <code>false</code>.
     *
     * @see BaseMatcher
     */
    boolean matches(Object item);

    /**
     * This method simply acts a friendly reminder not to implement Matcher directly and
     * instead extend BaseMatcher. It's easy to ignore JavaDoc, but a bit harder to ignore
     * compile errors .
     *
     * @see Matcher for reasons why.
     * @see BaseMatcher
     */
    void _dont_implement_Matcher___instead_extend_BaseMatcher_();
}

接口指定方法_dont_implement_Matcher___instead_extend_BaseMatcher_.当然,这并不妨碍其他人实现Matcher接口,而是引导开发者朝着正确的方向发展.

The interface specifies a method _dont_implement_Matcher___instead_extend_BaseMatcher_. Of course it does not prevent others from implementing the Matcher interface, but it guides the developer in the right direction.

还有 rel="noreferrerBaseMatcher 类将_dont_implement_Matcher___instead_extend_BaseMatcher_ 方法实现为final

And the BaseMatcher class implements the _dont_implement_Matcher___instead_extend_BaseMatcher_ method as final

public final void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
    // See Matcher interface for an explanation of this method.
}

最后我认为这是一个设计问题,因为 BaseMatcher 显然实现了每个 Matcher 应该实现.因此,最好使 Matcher 抽象类并使用模板方法.

Finally I think that this is a design problem, because the BaseMatcher obviouosly implements logic that every Matcher should implement. Thus it would have been better to make the Matcher an abstract class and use a template method.

但我猜他们这样做是因为这是字节码兼容性和新功能之间的最佳折衷.

But I guess they did it because it was the best compromise between bytecode compatibility and new features.

这篇关于如何在不使用抽象的情况下强制实现子类中的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆