学习SpringMVC中优秀的代码编写风格

来源:互联网 发布:火炬红包群 淘宝群 编辑:程序博客网 时间:2024/06/01 10:39

org.springframework.web.servlet.FrameworkServlet 中有下面这段代码

    private class ContextRefreshListener implements ApplicationListener<ContextRefreshedEvent> {        @Override        public void onApplicationEvent(ContextRefreshedEvent event) {            FrameworkServlet.this.onApplicationEvent(event);        }    }    public void onApplicationEvent(ContextRefreshedEvent event) {        this.refreshEventReceived = true;        onRefresh(event.getApplicationContext());    }

这里,ContextRefreshListener 是FrameworkServlet的内部类,监听Context-RefreshedEvent事件,当接收到消息时调用FrameworkServlet的onApplicationEvent方法,在onApplicationEvent中会调用一次onRefresh()方法,并将refreshEventReceived标志设置为已经refresh过。

这种写法,让我注意到FrameworkServlet.this.onApplicationEvent(event);这么一句代码,在内部类中使用外部类名.this.method就可以调用外部类的方法,看起来很不错的样子。所以,动手测试了一下,代码如下:

/** * Created by Administrator on 2016/6/25. */public class Ha {    private class Name {        public void sayHello() {            Ha.this.sayHello();        }    }    public void sayHello() {        System.out.println("Hello");    }    public void use() {        Name name = new Name();        name.sayHello();    }    public static void main(String[] args) {        Ha ha = new Ha();        ha.use();    }}

上面的代码太简单不过了,基本上没有逻辑,就不再解释了,写完问问我女票,看看她的回应,见下一节。

运行结果帮我来说明

运行结果

0 0
原创粉丝点击