在父类中记录静态方法 [英] logging static methods in a parent class

查看:52
本文介绍了在父类中记录静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象类Parent,有2个子类A和B.Parent有一个称为do()的静态方法.我想知道是否有一种静态方法可以在Logger中为类A添加日志信息(称为A.do()),并在日志B中添加日志信息(称为B.do()).通常

I have an abstract class Parent, with 2 subclasses A and B. Parent has one static method called do(). I am wondering if there is a way for that static method to add log info in Logger for class A when it's called as A.do() and log B when it's called as B.do(). The usual

protected final Logger LOGGER = Logger.getLogger(getClass());

不会像do()是静态方法那样工作,因此Logger也需要是静态的,但是getClass()方法显然不是静态的.

won't work as do() is a static method so Logger needs to be static as well, but the getClass() method is obviously not static.

谢谢.

推荐答案

我不推荐使用,但是如果您真的想要...

I wouldn't recommend it, but if you really want it...

public class A {
  public static void do() {
    doImpl(A.class);
  }
  protected static void doImpl(Class<?> refClass) {
  }
}

public class B extends A {
  public static void do() {
    doImpl(B.class);
  }
}

这篇关于在父类中记录静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆