![](/img/trans.png)
[英]Using Gson How to convert Json object that contains object of an interface that implemented by many classes - Java
[英]how to properly change an interface after it has been implemented by many classes? java
我有一個
public interface DoIt {
void dosomething (int i, double x);
int dosomethingelse (String s);
}
實現它的一些類是class_of_a
等...我想在接口中添加一個新方法,讓我們說diditwork(int x);
我怎么想這樣做,同時避免重新編譯的問題或可能出現的任何問題? 什么是新的等級?
創建一個新界面並擴展舊界面,如:
interface DoIt2 extends DoIt
{
// void doSomething(int i, double x);
// int doSomethingelse(String s);
void didItWork(int x);
}
您可以像這樣擴展現有的界面:
interface DoItMore extends DoIt { diditwork(int x); }
因此,您將擁有低級別類的舊接口和高級別的新接口。 然后,您必須更改高級類中使用的接口。
創建另一個接口,讓給定的類實現這兩個接口
例如
public interface DoIt {
void dosomething(int i, double x);
int dosomethingelse(String s);
}
和新的界面
public interface CheckIt {
boolean diditwork(int x);
}
沒有必要檢查的類只會實現DoIt接口,有必要檢查的類也會實現CheckIt接口
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.