开发者

Static logger for cross-plattform, e.g. Java (Applet) and Android

I want to use somthing like:

MyLogger.e("MyApp","I have an error.");

in a cross-plattform p开发者_StackOverflowroject, where MyLogger should be static to make it available in the whole project.

So I was trying something like:

MyAndroidLogger implements MyLogger {
   public static void e(String strApp, String strErr) {
     Log.e(strApp, strErr);
}

MyAppletLogger implements MyLogger {
   private static Logger logger = Logger.getLogger();
   public static void e(String strApp, String strErr) {
     logger.e(strApp, strErr);
}

The compiler complains about the static. How do I do this? Is it possible at all? If not, what's the right approach?

Thanks in advance...


You can't call a interface the way you are trying to. You have to call either

MyAndroidLogger.e("MyApp","I have an error.");

or

MyAppletLogger.e("MyApp","I have an error.");

If you want to use same call on both platforms you have to create a helper class that has a reference to the correct class:

MyLoggerHelper.e("MyApp","I have an error.");

where the MyLoggerHelper is a class that knows which one of the first two calls it should call and forwards this call to appropriate one.


Interfaces don't have static methods. That's why you can't invoke them.

You may try using a singleton.

YourWhateverLogger.getInstance().e("Hello");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜