[英]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.