int i=1; i=i++; int j=i++; int k=i+ ++i*i++; System.out.println("i="+i); System.out.println("j="+j); System.out.println("k="+k);
单例设计模式,即某个类在整个系统中只能有一个实例对象可被获取和使用的代码模式。
两种常见模式
饿汉式:直接创建对象,不存在线程安全问题
/** * 饿汉式: * 直接创建实例对象,不管你是否需要这个对象都会创建* * (1)构造器私有化 * (2)自行创建,并且用静态变量保存 * (3)向外提供这个实例 * (4)强调这是一个单例,我们可以用final修改 */ public class Singleton1 { public static final Singleton1 INSTANCE =new Singleton1(); private Singleton1(){ }
懒汉式:延迟创建对象
private static singleton4 instance; private singleton4(){} public static singleton4 getInstance(){ if(instance == null){ instance = new singleton4(); } return instance; }
①哪些方法不可以被重写
②对象的多态性
public class Father { private int i =test(); private static int j = method(); static { System.out.println("(1)"); } Father(){ System.out.println("(2)"); } { System.out.println("(3)"); } public int test(){ System.out.println("(4)");return 1; } public static int method(){ System.out.println("(5)");return 1; } }
public class Son extends Father { private int i =test(); private static int j = method(); static { System.out.println("(6)"); } Son(){ System.out.println("(7)"); } { System.out.println("(8)"); } public int test(){ System.out.println("(9)");return 1; } public static int method(){ System.out.println("(10)");return 1; } }
public class test { public static void main(String[] args) { Son s1 = new Son(); System.out.println(); Son s2 = new Son(); } }
(5) (1) (10) (6) (9) (3) (2) (9) (8) (7)
(9) (3) (2) (9) (8) (7)
new子类()
子类初始化<clinit>
子类实例化方法<init>:
public class test1 { public static void main(String[] args) { int a = 1; String b = "hello"; Integer c = 200; int[] d = {1, 2, 3, 4, 5}; MyData e = new MyData(); change(a,b,c,d,e); System.out.println("a = "+a); System.out.println("b = "+b); System.out.println("c = "+c); System.out.println("d = "+Arrays.toString(d)); System.out.println("e = "+e.a); } public static void change(int a1, String b1, Integer c1, int[] d1, MyData e1){ a1 += 1; b1 += " world"; c1 += 1; d1[0] += 1; e1.a += 1; } } class MyData{ int a = 10;}