简体   繁体   English

如何从没有实例的其他类调用方法?

[英]How to call methods from other classes not having an instance?

How can I call a method from a class that is not an object within another class, and has nothing in common with this other class? 如何从不是另一个类中的对象的类调用方法,并且与其他类没有任何共同之处?

In my case: 就我而言:

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
   }
}

One possibility might be to make the refresh() and grid static . 一种可能性是使refresh()grid static But that's bad design, and I cannot use this approach as I want to use several implementations of GridUI . 但这是糟糕的设计,我不能使用这种方法,因为我想使用GridUI几个实现。

So, how can I refresh a certain gridclass in my app from any service that does not contain this grid as an object?? 那么,我如何从不包含此网格作为对象的任何服务刷新我的应用程序中的某个网格类?

Just create and fire an Event for it in your service and make your grid register for that Event . 只需在您的服务中为它创建并触发一个Event ,并使您的网格注册该Event It's probably best to use an EventBus . 最好使用EventBus

Using a static Map<String, Grid> as was suggested in the accepted answer will work but it's not proper. 使用静态Map<String, Grid>就像在接受的答案中建议的那样可行,但它不合适。 You risk making mistakes and it's not as easy to manage when the number of grids increases. 您冒着犯错的风险,当网格数量增加时,管理起来并不容易。

The EventBus approach is more work upfront but in the end it's a better approach. EventBus方法是更多的前期工作,但最终它是一种更好的方法。 You'll be able to reuse the EventBus throughout your application. 您将能够在整个应用程序中重用EventBus It really helps keep your coupling down. 它确实有助于保持你的耦合。 You'll also easily be able to get different objects act on the same Event with little effort. 您也可以轻松地让不同的对象轻松地在同一个Event上执行操作。

Alternatively create a components registry (basically a Map<String,Grid> ), then fetch the grid from SomeBackendService using its id as key in the registry. 或者创建一个组件注册表(基本上是Map<String,Grid> ),然后使用其id作为注册表中的键从SomeBackendService获取网格。 (I guess you know which grid you want to refresh, right?) (我想你知道要刷新哪个网格,对吧?)

Be careful with registries though: 但要注意注册表:

  • make sure they are thread safe if need be (probably true in an UI app) 如果需要,确保它们是线程安全的(在UI应用程序中可能是真的)
  • they tend to fill up and leak memory if not properly handled 如果处理不当,它们往往会填满并泄漏内存

Sorry for not answering that long time, i was in vacation. 很抱歉没回答那么长时间,我在度假。

Interfaces are some kind of classes. 接口是某种类。 But they do not implement any method, they have empty method bodies. 但是他们没有实现任何方法,他们有空方法体。

Each class, which implements an interface usually MUST implement its methods. 实现接口的每个类通常必须实现其方法。

In C# You would Do : 在C#你会做:

enter code here

    interface IMyInterface
    {
       void PleaseImplementMe();
    }


    class clMyInterface :IMyInterface
    {

        void  IMyInterface.PleaseImplementMe()
        {
            MessageBox.Show("One implementation");
        }    
    }





end

Please let me know, whether this is what can help You or not. 请告诉我,这是否可以帮到你。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM