Java基础 构造对象初始化变量的顺序浅见
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
A b = new B(8);
System.out.println(A.aa + " " + B.bb);
}
}
class A {
int a = f();
static int aa = sf();
static {
aa = 11;
System.out.println("A kuai static " + aa);
}
{
aa = 111;
a = 1;
System.out.println("A kuai " + a);
}
A() {
System.out.println("A constract begin. " + a);
}
A(int r) {
System.out.println("A constract begin. " + a);
}
int f() {
System.out.println("A kuai f");
return 2;
}
static int sf() {
System.out.println("A kuai sf");
return 22;
}
}
class B extends A {
static int bb = sfun();
int b = fun();
static {
bb = 44;
System.out.println("B kuai static " + bb);
}
{
b = 4;
System.out.println("B kuai " + b);
}
B() {
System.out.println("B constract begin. " + b);
}
B(int r) {
System.out.println("B constract begin." + b);
}
int fun() {
System.out.println("B kuai fun");
return 3;
}
static int sfun() {
System.out.println("B kuai sfun");
return 33;
}
}
/*
A kuai sf
A kuai static 11
B kuai sfun
B kuai static 44
A kuai f
A kuai 1
A constract begin. 1
B kuai fun
B kuai 4
B constract begin.4
111 44
*/
初始化顺序:
父类的静态变量、子类的静态变量、父类的实例变量、父类的构造方法、子类的实例变量、子类的构造方法
其中静态变量的初始化:
1)。定义时,2)。静态块;初始化顺序按照他们在代码中的顺序初始化;
实例变量的初始化:
1)。定义时,2)。静态块,3)。构造方法;
其中1、2先于3)执行,对于1、2按照其在代码中的顺序执行。
都是先定义,开辟内存,赋给其初始值(null,0等),最后按照赋值语句赋值,就是最终的值。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
System.out.println(A.sf());
}
}
class A {
static A Aa = new A(2);
static int aa = 11;
int a = 1;
static {
System.out.println("static " + aa);
}
{
System.out.println("A kuai " + a);
}
A() {
sf();
System.out.println("A constract begin. " + a + " " + aa + " " + " "
+ sf());
}
A(int r) {
sf();
System.out.println("A constract begin. " + a + " " + aa + " " + " "
+ sf());
}
int f() {
System.out.println("A kuai f " + a);
return a;
}
static int sf() {
System.out.println("A kuai sf " + aa);
return aa;
}
}
/*
A kuai 1
A kuai sf 0
A kuai sf 0
A constract begin. 1 0 0
static 11
A kuai sf 11
11
*/
A kuai 1
为什么会输出来呀?为什么静态的变量是0,而实例变量却有值呀!
这样是不是和上面的描述有了矛盾呀?不懂?请路过的大牛指导!谢谢!
个人理解:
当static A Aa = new A(2);进行时,会先进行对象实例创建,故有以上结果
class Parent{ protected int num = 1; public Parent(){ System.out.println("Parent=" + num); print(); add('a'); } protected void add(int i) { System.out.println("add Parent"); } protected void print(){ System.out.println("Parent=print=" + num); } } class Son extends Parent{ protected int num = 2; public Son(){ System.out.println("Son=" + num); } @Override protected void print() { System.out.println("Son=print=" + num); } @Override protected void add(int i) { System.out.println("add Son"); } } class Son1 extends Parent{ protected int num = 2; public Son1(){ System.out.println("Son1=" + num); } @Override protected void print() { System.out.println("Son1=print=" + num); }
//此处是重构 protected void add(char i) { System.out.println("add Son1"); } }
new Son();
new Son1();
Parent=1
Son=print=0
add Son
Son=2
Parent=1
Son1=print=0
add Parent
Son1=2
char转换为int

浙公网安备 33010602011771号