@Richard
#Java中子类继承父类的变量和方法,那么在类的加载过程中,子类和父类的加载过程又是怎样的呢?
代码案例
package com.tulun.src06;
/**
- @author Richard
- @date 2019/6/30 0030-14:34
*/
public class TestDemo {
public static void main(String[] args) {
// Person p=new Person();
Student s=new Student();
}
}
class Father{
public Father(){
System.out.println(“父类的实例变量”);
}
}
class staticFather{
public staticFather(){
System.out.println(“父类的静态变量”);
}
}
class Person{
private Father f=new Father();//父类的实例变量
private static staticFather sf=new staticFather(); //父类的静态变量
//父类的构造方法
public Person(){
System.out.println(“父类的构造方法!”);
}
//父类的实例块
{
System.out.println(“父类的实例块!”);
}
//父类的静态块
static{
System.out.println(“父类的静态块!”);
}
}
class Son{
public Son(){
System.out.println(“子类的实例变量!”);
}
}
class staticSon{
public staticSon(){
System.out.println(“子类的静态变量!”);
}
}
class Student extends Person{
private Son s=new Son();//子类的实例变量
private static staticSon ss=new staticSon();//子类的静态变量
//子类的构造函数
public Student(){
System.out.println(“子类的构造函数!”);
}
//子类的实例块
{
System.out.println(“子类的实例块!”);
}
//子类的静态块
static{
System.out.println(“子类的静态块!”);
}
}
运行结果:
由此可见,在继承中,类的加载顺序!