`
不吃鱼的猫
  • 浏览: 25445 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论
阅读更多

写了好几年的程序的人不见得全部对语言基础都非常了解

比如说Java的内部类,除了在些框架里提供的之如spring里的callback以及GUI里面的常用匿名内部类,至少我个人很少在其他地方利用到,正好不忙,把这块部分好好补一补。

下面是总结的一些tips:

1.static inner class.

用法一:用于改进的singleton类(created by Jeremy Manson and Brian Goetz)
the Initialization on Demand Holder (IODH) idiom which requires very little code and has zero synchronization overhead. Zero, as in even faster than volatile. IODH requires the same number of lines of code as plain old synchronization, and it's faster than DCL.

java 代码
  1. public class Something   
  2.  {   
  3.      private Something()    
  4.      {   
  5.      }   
  6.         
  7.      private static class LazyHolder    
  8.      {   
  9.          private static final Something something = new Something();   
  10.      }   
  11.         
  12.      public static Something getInstance()   
  13.      {   
  14.          return LazyHolder.something;   
  15.      }   
  16.  }   

延伸阅读
http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html#dcl JSR 133 (Java Memory Model) FAQ
http://en.wikipedia.org/wiki/Initialization_on_demand_holder_idiom

用法二:懒人的测试工具
http://www.javaworld.com/javaworld/javatips/jw-javatip106.html
因为内部类编译成OutClass$InnerClass.class,所以可以在dev,test环境上保留这个类,在prod环境上不打包这个类。

所以可以在innerClass写些测试方法用来简单的单元测试,不过对于职业程序员来说还是要保持用好Junit 的习惯。

分享到:
评论

相关推荐

    swift tips

    ### Swift Tips:Swift 开发者必备技巧概览 Swift 是苹果公司在 2014 年全球开发者大会(WWDC)上推出的全新编程语言。它不仅汲取了多种语言的优点,而且具备简洁优雅的语法和轻便灵活的特点。本书作者王巍...

    Java2核心技术卷I+卷2:基础知识(第8版) 代码

    Chapter 6: Interfaces and Inner Classes 241 Interfaces 242 Object Cloning 249 Interfaces and Callbacks 255 Inner Classes 258 Proxies 275 Chapter 7: Graphics Programming 281 Introducing Swing...

    java.核心技术.第八版 卷1

    For thorough coverage of Java fundamentals–including interfaces and inner classes, GUI programming with Swing, exception handling, generics, collections, and concurrency–look for the eighth edition ...

    Quasar Input:type="number" 去掉上下小箭头 实现加减按钮样式功能

    <span class="tips">(必填) <div class="col-6"> v-model.number="model" type="number" outlined class="input" > dense flat icon="remove" class="number-btn" @click="numberSub(model)" /...

    Java邮件开发Fundamentals of the JavaMail API

    1. Tutorial tips 2 2. Introducing the JavaMail API 3 3. Reviewing related protocols 4 4. Installing JavaMail 6 5. Reviewing the core classes 8 6. Using the JavaMail API 13 7. Searching ...

    三星9305收索

    top:3px}#shouji{margin-right:14px}#u{display:none}#c-tips-container{display:none}.bdsug{position:absolute;width:418px;background:#fff;display:none;border:1px solid #817f82}.bdsug li{width:402px;color:...

Global site tag (gtag.js) - Google Analytics