本文参考 本篇文章参考自<Effective Java>第三版第三条"Enforce the singleton property with a private constructor or an enum type" 原文的不足之处 原文给的示例代码比较简单,提供的是一种饿汉式的单例模式 // Singleton with public final field public class Elvis { public static final Elvis INSTA
一般this在各类语言中都表示“调用当前函数的对象”,java中也存在这种用法: public class Leaf { int i = 0; Leaf increment(){ i++; return this; //this指代调用increment()函数的对象 } void print(){ System.out.println("i = " + i); } public static void main(String[] args) { Leaf x = new Leaf()
本节我们主要来介绍泛型的基本概念和原理 后续章节我们会介绍各种容器类,容器类可以说是日常程序开发中天天用到的,没有容器类,难以想象能开发什么真正有用的程序.而容器类是基于泛型的,不理解泛型,我们就难以深刻理解容器类.那,泛型到底是什么呢? 什么是泛型? 一个简单泛型类 我们通过一个简单的例子来说明泛型类的基本概念.实现原理和好处. 基本概念 我们直接来看代码: public class Pair<T> { T first; T second; public Pair(T first, T se