内容比较简单。
1.单例设计模式。


1 public class TestSingleClass { 2 private static TestSingleClass _instance = null; 3 4 private TestSingleClass() { 5 6 } 7 8 public static TestSingleClass Current() { 9 if (_instance == null) { 10 _instance = new TestSingleClass(); 11 System.out.println("实例化"); 12 } 13 return _instance; 14 } 15 16 public void showName() { 17 System.out.println("Hello Java"); 18 } 19 } 20 21 22 public class TestSingleClassMain { 23 24 /** 25 * @param args 26 */ 27 public static void main(String[] args) { 28 TestSingleClass.Current().showName(); 29 } 30 31 }
2.static的简单基本应用。
(1)统计实例化对象个数。static一个int的变量,放在类的构造函数中进行自增。
(2)自动命名。这个其实就是将自增的号放到名字的后面。
(3)配合const关键字,定义不可编辑的全局变量。持久层用到比较方便。


1 public class TestAutoIncreaseClass { 2 private static int i = 0; 3 4 private String name = ""; 5 6 public TestAutoIncreaseClass() { 7 i++; 8 this.name = ("NoName-" + i); 9 } 10 11 public String getName() { 12 return this.name; 13 } 14 15 public TestAutoIncreaseClass(String value) { 16 this.name = value; 17 } 18 19 public void showName() { 20 System.out.println("姓名:" + this.name); 21 } 22 } 23 24 25 public class TestAutoIncreaseClassMain { 26 27 /** 28 * @param args 29 */ 30 public static void main(String[] args) { 31 TestAutoIncreaseClass a = new TestAutoIncreaseClass(); 32 a.showName(); 33 a = new TestAutoIncreaseClass("张三"); 34 a.showName(); 35 a = new TestAutoIncreaseClass(); 36 a.showName(); 37 a = new TestAutoIncreaseClass("李四"); 38 a.showName(); 39 a = new TestAutoIncreaseClass(); 40 a.showName(); 41 a = new TestAutoIncreaseClass("王五"); 42 a.showName(); 43 44 } 45 46 }