简体   繁体   English

如何在多个类实现后正确更改接口? java的

[英]how to properly change an interface after it has been implemented by many classes? java

I have an 我有一个

public interface DoIt {
    void dosomething (int i, double x);
    int dosomethingelse (String s);
}

some class that implement it is class_of_a etc... and i want to add an new method in the interface lets say diditwork(int x); 实现它的一些类是class_of_a等...我想在接口中添加一个新方法,让我们说diditwork(int x); How am i suppose to do that while avoiding the problems of recompiling or whatever problem that might be ? 我怎么想这样做,同时避免重新编译的问题或可能出现的任何问题? What would be the new hierarchy ? 什么是新的等级?

Create a new interface and extend the old interface, something like: 创建一个新界面并扩展旧界面,如:

interface DoIt2 extends DoIt
{
    // void doSomething(int i, double x);
    // int doSomethingelse(String s);
    void didItWork(int x);
}

you can extend your existing Interface like this: 您可以像这样扩展现有的界面:

interface DoItMore extends DoIt {  diditwork(int x);  }  

so you will have your old interface for low level Classes and your new Interface for high level. 因此,您将拥有低级别类的旧接口和高级别的新接口。 Then you have to change the used Interface in your High level class. 然后,您必须更改高级类中使用的接口。

create another interface and let the given class implement both interfaces 创建另一个接口,让给定的类实现这两个接口

eg 例如

public interface DoIt { 
    void dosomething(int i, double x);
    int dosomethingelse(String s);
}

and new interface 和新的界面

public interface CheckIt {
    boolean diditwork(int x);
}

classes without necessity to check would implement only DoIt interface, classes with necessity to check would implement also CheckIt interface 没有必要检查的类只会实现DoIt接口,有必要检查的类也会实现CheckIt接口

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

相关问题 使用Gson如何转换Json对象,该对象包含由许多类实现的接口的对象-Java - Using Gson How to convert Json object that contains object of an interface that implemented by many classes - Java 该算法是否已正确实施? - Has this algorithm been implemented properly? 如何在java中找到已实现接口的所有子类? - How to find all sub classes of implemented interface in java? 显示JFrame后如何更改它的内容(java) - how to change the contents of a JFrame after it has been displayed (java) AOP是如何实现改变Java界面内容的? - How is AOP being implemented to Change Java interface content? 有没有用Java实现的语言? - Has any language been implemented in Java? 如何确保在循环遍历列表后添加的Java并发列表中的元素得到正确处理? - How to make sure elements of a concurrent list in java, that are added after the list has been looped through, are handled properly? 将新方法添加到由许多类直接实现的接口 - Add new method to an interface which is directly implemented by many classes 为GUI创建一个通用接口,并由许多类实现 - Create one general interface for GUI, have it implemented by many classes 使用Java 7的Linux上的立体声问题-将组件添加到父组件后如何更改其GC - Stereo problems on Linux using Java 7 - how to change the GC of a component after it has been added to a parent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM