1 package com.hanqi; 2 3 public class Test11Car11 { 4 5 //静态 6 //实例属性 7 private int m = 0; 8 //静态属性 9 10 //所有实例共有的,在内存里只有一个 11 private static int n = 0 ; 12 13 //实例方法 14 public void run () 15 { 16 for (int i = 0; i < 10; i++) 17 { 18 m++; 19 20 n++; 21 22 System.out.println("m = "+ m + " n = " + n); 23 } 24 } 25 //静态方法 26 public static void testStatic() 27 { 28 29 //访问内部成员 30 31 //m = 10; 32 33 n = 10; 34 35 //run() 36 37 Test11Car11 t1 = new Test11Car11(); 38 39 t1.run(); 40 41 42 43 System.out.println("测试静态方法"); 44 45 46 //设计模式: 23种,单例模式,工厂模式, 47 48 49 50 } 51 }
1 package com.hanqi; 2 3 public class Test12 { 4 5 public static void main(String[] args) { 6 // TODO 自动生成的方法存根 7 8 Test11Car11 t1 = new Test11Car11(); 9 //实例方法 10 t1.run(); 11 12 Test11Car11 t2 = new Test11Car11(); 13 14 t2.run(); 15 16 17 //调用类的静态方法 18 Test11Car11.testStatic(); 19 20 //调用类的实例方法 21 //t1.testStatic(); 22 System.out.println("t3 实例"); 23 Test13 t3 = Test13.instance(); 24 t3.run(); 25 System.out.println("t4 实例"); 26 Test13 t4 = Test13.instance(); 27 t4.run(); 28 } 29 30 }