Java超类和子类的结果

huangapple go评论61阅读模式
英文:

java super class and sub class result

问题

我有以下的Java代码。

class A{
    static {
        System.out.print("1 ");
    }
    A(){
        System.out.print("3 ");
    }
    {
        System.out.print("2 ");
    }
}
class B extends A{
    static {
        System.out.print("4 ");
    }
    B(){
        System.out.print("6 ");
    }
    {
        System.out.print("5 ");
    }
}
class Demo{
    public static void main(String args[]){
        new B();
    }
}

结果如下所示:
1 4 2 3 5 6

这涉及到了父类和子类,但我无法理解结果是如何生成的?

英文:

I have following java code.

class A{
	static {
		System.out.print("1 ");
	}
	A(){
		System.out.print("3 ");
	}
	{
		System.out.print("2 ");
	}
}
class B extends A{
	static {
		System.out.print("4 ");
	}
	B(){
		System.out.print("6 ");
	}
	{
		System.out.print("5 ");
	}
}
class Demo{
	public static void main(String args[]){
		new B();
	}
}

the result is above as following
1 4 2 3 5 6

this is include super class and sub class but I could not understand how is the result generating?

答案1

得分: 0

这样想。在类本身被初始化之前,您无法创建该类的实例。主方法位于 Demo 类内部,其中 (Object -> Demo) 在那个顺序中静态初始化这些类。由于 main 是静态方法,我们没有创建 Demo 的实例,因此在这段时间内不会运行实例初始化块。这里没有打印任何内容,所以不太有趣。类在首次访问类时被初始化

下一步是要注意,立即在主方法中我们正在请求创建一个 B 对象。因此我们必须查看 B 类的依赖关系,这里是 (Object -> A -> B)。Object 的静态初始化块已经运行过了(如果有的话),所以我们剩下 A,然后是 B。

在 A 的静态初始化块之后

> 1

在 B 的静态初始化块之后

> 1 4

现在,由于 new B(); 的请求是为了 B 类的一个实例,我们必须按顺序遍历每个父类(Object -> A -> B)并运行它们的实例初始化块,然后是任何相关的构造方法代码。与上面的静态初始化块不同,这将在每次构造实例时都执行。这是逐类进行的。这个例子是一个比较简单的情况,每个类只有一个构造函数。请注意,初始化块在构造函数之前执行。从 Object 类中没有打印输出,所以我们可以忽略它。

在 A 的实例初始化块之后:

> 1 4 2

在 A 构造函数之后(与我们的请求相关)

> 1 4 2 3

在 B 实例初始化块之后

> 1 4 2 3 5

在 B 构造函数之后(与我们的请求相关)

> 1 4 2 3 5 6

英文:

Think of it this way. You can't make an instance of the class until the class itself is initialized. The main method lies within Demo class with (Object -> Demo) which initializes those classes statically in that order. We are not creating an instance of demo since main is a static method, hence instance initialization blocks are not run for either at this time. Nothing is printed so it is less interesting here. Classes are initialized upon first access of the class

The next step is to identify that immediately in the main method we are requesting to create a B object. Hence we have to look at the B class dependency here is (Object -> A -> B). The static initialization blocks for Object would have been already run (if any) so we are left with A, then B.

After A Static Initialization Blocks

> 1

After B Static Initialization Blocks

> 1 4

Now since the request from new B(); is for an instance of B, we have to go through each of the parent-most class (Object -> A -> B) and run their instance initialization blocks in order, followed by any relevant Constructor methods code. Unlike with the static initialization blocks above, this will be done EACH time an instance is constructed. This is done on a class-by-class basis. This example is a simpler case where there is only one constructor per class. Note that initialization blocks precede constructors. Nothing prints from Object class so we can disregard that.

After A instance initialization Blocks:

> 1 4 2

After A constructor (pertinent to our request)

> 1 4 2 3

After B instance Initialization Blocks

> 1 4 2 3 5

After B Constructor (pertinent)

> 1 4 2 3 5 6

huangapple
  • 本文由 发表于 2020年9月22日 22:43:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64012170.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定