getLog(this.getClass()) 与 getLog

2019-10-19  本文已影响0人  神之试炼者

基本没什么区别, 唯一的区别是前者在继承类中子类可以直接使用,无需重新定义.

public class Father {
    protected final Log log = LogFactory.getLog(this.getClass());

    void test() {
        log.info("father");
    }
}

public class Son extends Father{
    void test(){
        log.info("son");
    }
}

因为java类的动态特性, 子类获得log对象其实是LogFactory.getLog(Son.class), 虽然是从父类继承过来

上一篇 下一篇

猜你喜欢

热点阅读