[英]How to call methods from other classes not having an instance?
如何從不是另一個類中的對象的類調用方法,並且與其他類沒有任何共同之處?
就我而言:
class GridUI {
com.google.gwt.user.cellview.client.DataGrid grid;
public void refresh() {
dataGrid.redraw();
}
}
class SomeBackendService() {
public foo() {
//have to trigger refresh of a specific grid
}
}
一種可能性是使refresh()
和grid
static
。 但這是糟糕的設計,我不能使用這種方法,因為我想使用GridUI
幾個實現。
那么,我如何從不包含此網格作為對象的任何服務刷新我的應用程序中的某個網格類?
或者創建一個組件注冊表(基本上是Map<String,Grid>
),然后使用其id作為注冊表中的鍵從SomeBackendService
獲取網格。 (我想你知道要刷新哪個網格,對吧?)
但要注意注冊表:
很抱歉沒回答那么長時間,我在度假。
接口是某種類。 但是他們沒有實現任何方法,他們有空方法體。
實現接口的每個類通常必須實現其方法。
在C#你會做:
enter code here
interface IMyInterface
{
void PleaseImplementMe();
}
class clMyInterface :IMyInterface
{
void IMyInterface.PleaseImplementMe()
{
MessageBox.Show("One implementation");
}
}
end
請告訴我,這是否可以幫到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.