在 Java 接口中添加新方法,以便对继承类进行最少的更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11069123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:39:56  来源:igfitidea点击:

Adding new method in Java Interface so it involves minimal changes to inheriting classes

javainheritanceinterface

提问by Sougata Bhattacharya

Possible Duplicate:
Adding Extra method to interface

可能的重复:
向接口添加额外的方法

There is scenario where I have Interface X, which has been implemented with my thousands of classes. Now I want to add new method in that Interface X. So how to make the changes in minimal way to solve the problem of overridden of methods in all my classes

有一个场景,我Interface X已经实现了我的数千个类。现在我想在其中添加新方法Interface X。那么如何以最小的方式进行更改来解决我所有类中方法被覆盖的问题

回答by wattostudios

I would create an extension of your interface for only the classes that need the additional methods...

我只会为需要附加方法的类创建接口的扩展...

public interface BaseInterface {
    public int exampleMethod();
}

public interface ExtendedInterface extends BaseInterface {
    public int anotherMethod();
}

The thousands of classes already implement BaseInterface. For the classes that need the extra method, you change them to implement ExtendedInterface.

数以千计的类已经实现了BaseInterface. 对于需要额外方法的类,您将它们更改为 implementation ExtendedInterface

If your objects are stored in a collection such as a BaseInterface[]array, this still works because objects of type ExtendedInterfaceare also objects of type BaseInterface, so they can still be stored in the same common collection.

如果您的对象存储在一个集合(例如BaseInterface[]数组)中,这仍然有效,因为 typeExtendedInterface的对象也是 type 的对象BaseInterface,因此它们仍然可以存储在同一个公共集合中。

For example, this is still perfectly valid...

例如,这仍然是完全有效的......

BaseInterface[] objects = new BaseInterface[2];
objects[0] = new ClassThatImplementsBaseInterface();
objects[1] = new ClassThatImplementsExtendedInterface();

However, if you need to access the new method of the ExtendedInterface, but the object is stored in a BaseInterfacecollection, you'll need to cast it into an ExtendedInterfacebefore you can use it...

但是,如果您需要访问 的新方法ExtendedInterface,但对象存储在BaseInterface集合中,则需要ExtendedInterface先将其转换为 ,然后才能使用它...

BaseInterface[] objects = new BaseInterface[1];
objects[0] = new ClassThatImplementsExtendedInterface();

if (objects[0] instanceof ExtendedInterface){
    // it is an ExtendedInterface, so we can call the method after we cast it
    ((ExtendedInterface)objects[0]).anotherMethod();
}
else {
    // it is a BaseInterface, and not an ExtendedInterface
}

This may or may not be suitable, depending on your usage.

这可能适合也可能不适合,具体取决于您的使用情况。

If you really need all your thousands of objects to implement the new method, you'll have to add the method to the BaseInterfaceand then use a feature of your IDE or text editor to implement the method in all your classes. For example, you could open them all in a text editor and do a find-replace to findsomething thats common to each class, and replaceit with the common code + the default code for the new method. Pretty quick and painless. I'm sure that some IDEs would probably also automatically add the method declaration to all inheriting classes, or at least have an option to do this in a right-click menu.

如果您确实需要所有数千个对象来实现新方法,则必须将方法添加到 中BaseInterface,然后使用 IDE 或文本编辑器的功能在所有类中实现该方法。例如,您可以在文本编辑器中将它们全部打开并执行查找替换以查找每个类共有的内容,并将其替换为公共代码 + 新方法的默认代码。相当快速和无痛。我确信某些 IDE 可能还会自动将方法声明添加到所有继承类,或者至少有一个选项可以在右键单击菜单中执行此操作。

回答by Gene

If the new method is a true extension of the interface, then the right thing to do is edit the interface and use your development environment's tools to find all the places where the new functionality must be implemented. Then do the work. Eclipse and Netbeans will do a fine job.

如果新方法是接口的真正扩展,那么正确的做法是编辑接口并使用您的开发环境的工具来查找必须实现新功能的所有位置。然后做功课。Eclipse 和 Netbeans 会做得很好。

[NB I'm a bit surprised that refactoring tools don't take care of some of the manual effort, but so it is.]

[注意,重构工具没有处理一些手动工作,这让我有点惊讶,但事实确实如此。]

If the new method won't be called most of the time in old code, consider the new interface to be an extension of the old one:

如果在旧代码中大部分时间不会调用新方法,请将新接口视为旧接口的扩展:

public interface NewInterface extends OldInterface {
    void newMethod();
}

If you have needs to pass old interface objects to new interface consumers with a null version of newMethod(), you can do something like:

如果您需要将旧接口对象传递给具有 null 版本的新接口使用者newMethod(),您可以执行以下操作:

public class NewInterfaceWrapper<T extends OldInterface> implements NewInterface {

    private T wrapped;

    public NewInterfaceWrapper(T wrapped) {
        this.wrapped = wrapped;
    }

    // Define all the old interface methods and delegate to wrapped.method 

    // Now provide the null implementation of new method.
    void newMethod() { }
}

...

wantsNewInterface(new NewInterfaceWrapper(oldImplementer));

It's not pretty, but big systems usually grow rough edges like this as they age.

这并不漂亮,但随着年龄的增长,大型系统通常会像这样长出粗糙的边缘。

回答by Thihara

There is no easy way to do that. If you add a method to the interface all implementing classes must override it. If you change the interface to an abstract class then you have to refactor the implementing classes as well.

没有简单的方法可以做到这一点。如果向接口添加方法,则所有实现类都必须覆盖它。如果将接口更改为抽象类,则还必须重构实现类。

But you have a class hierarchy right? So you can minimize the work by implementing that method in the base classes only. But that depends on your specific requirements and details so I guess happy implementing!

但是你有一个类层次结构对吗?因此,您可以通过仅在基类中实现该方法来最大限度地减少工作。但这取决于您的具体要求和细节,所以我想实施起来很愉快!

And if there's no easy class hierarchy that you can use to implement new methods like that perhaps it's time you think about a major rewrite in favor of future maintenance effort reduction.

如果没有简单的类层次结构可以用来实现这样的新方法,那么也许是时候考虑进行重大重写以减少未来的维护工作了。