📜  说明Java继承中的类加载和静态块

📅  最后修改于: 2022-05-13 01:54:58.456000             🧑  作者: Mango

说明Java继承中的类加载和静态块

类加载是指读取.class文件并将相应的二进制数据存储在方法区中。对于每个.class文件,JVM 都会在 Method Area 中存储相应的信息。现在在类加载中加入继承。在Java继承中,JVM首先加载并初始化父类,然后加载并初始化子类。

例子

Java
// Java program to Illustrate Class Loading
 
// Importing input output files
import java.io.*;
 
// Helper class 1
class A {
 
    // First static block of this class
    static
    {
 
        // Print message
        System.out.println(
            "Loading class A 1st static block ");
    }
 
    // Second static block of this class
    static
    {
 
        // Print message
        System.out.println(
            "Loading class A 2nd static block B.c="
            + B.c);
    }
 
    // Third static block of this class
    static
    {
 
        // Print message
        System.out.println(
            "Loading class A 3rd static block ");
    }
 
    static int a = 50;
    A() {}
}
 
// Helper class 2 extending from
// helper class 1
class B extends A {
 
    static
    {
        // Print message
        System.out.println(
            "Loading class B static block A.a=" + A.a);
    }
 
    static int c = 100;
}
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Execution statement in main class
        new B();
    }
}


Java
// Java Program to illustrate static blocks in inheritance
 
// Importing all input output classes
import java.io.*;
 
// Class 1- Parent Class
class GFG {
 
    // Declaring a static method m1
    public static void m1() {}
}
 
// Class 2 -  child class of GFG
class c extends GFG {
 
    // Declaring a non- static method m1
    public void m1() {}
}


Java
// Java Program to illustrate static blocks in inheritance
 
// Importing all input output classes
import java.io.*;
 
// Class 1 - Parent class
class GFG {
 
    // Declaring a non-static method m1
    public void m1() {}
}
 
// Class 2 - child class of parent
// child class of GFG
class C extends GFG {
 
    // Declaring a static method m1
    public static void m1() {}
}



输出
Loading class A 1st static block 
Loading class A 2nd static block B.c=0
Loading class A 3rd static block 
Loading class B static block A.a=50

静态块将在类加载时执行,因此在类加载时,如果我们想要执行任何活动,我们必须在静态块中定义它。在静态块继承中,必须记住以下两条规则:

  1. 不能覆盖静态块,因为将生成非静态 else 编译时错误。
  2. 非静态块不能被覆盖为静态块。

对于上述场景,静态块在继承中的使用如下所示,因为将生成的编译器错误分别显示如下:

情况 1:不能覆盖静态块,因为将生成非静态 else 编译时错误

例子

Java

// Java Program to illustrate static blocks in inheritance
 
// Importing all input output classes
import java.io.*;
 
// Class 1- Parent Class
class GFG {
 
    // Declaring a static method m1
    public static void m1() {}
}
 
// Class 2 -  child class of GFG
class c extends GFG {
 
    // Declaring a non- static method m1
    public void m1() {}
}

输出:

情况 2:非静态块不能被覆盖为静态块

例子

Java

// Java Program to illustrate static blocks in inheritance
 
// Importing all input output classes
import java.io.*;
 
// Class 1 - Parent class
class GFG {
 
    // Declaring a non-static method m1
    public void m1() {}
}
 
// Class 2 - child class of parent
// child class of GFG
class C extends GFG {
 
    // Declaring a static method m1
    public static void m1() {}
}

输出: