类和对象的创建及使用原理
先看一个例子:
public class Base {
public static int s;
private int a;
static {
System.out.println("基类静态代码块, s: " + s);
s = 1;
}
{
System.out.println("基类实例代码块, a: " + a);
a = 1;
}
public Base() {
System.out.println("基类构造方法, a: " + a);
a = 2;
}
protected void step() {
System.out.println("base s: " + s + ", a: " + a);
}
public void action() {
System.out.println("start");
step();
System.out.println("end");
}
}
public class Child extends Base {
public static int s;
private int a;
static {
System.out.println("子类静态代码块, s: " + s);
s = 10;
}
{
System.out.println("子类实例代码块, a: " + a);
a = 10;
}
public Child() {
System.out.println("子类构造方法, a: " + a);
a = 20;
}
protected void step() {
System.out.println("child s: " + s + ", a: " + a);
}
public static void main(String[] args) {
System.out.println("---- new Child()");
Child c = new Child();
System.out.println("\n---- c.action()");
c.action();
Base b = c;
System.out.println("\n---- b.action()");
b.action();
System.out.println("\n---- b.s: " + b.s);
System.out.println("\n---- c.s: " + c.s);
}
}
你能准确写出这段代码的输出吗?
我们在评论区公布答案,先让我们看看类和对象的运行原理:
步骤 | 说明 | 例子详解 |
类加载过程 | 基本准则:
类的信息:
类加载过程:
| 类的内存布局:
|
对象创建过程 | 创建对象过程包括:
| 创建和赋值后,内存布局: |
方法调用过程 | java基本准则:
虚方法表:在类加载的时候为每个类创建一个表,记录该类的对象所有动态绑定的方法(包括父类的方法)及其地址,但一个方法只有一条记录,子类重写了父类方法后只会保留子类的。 |
b.action(),这句代码的输出和c.action()是一样的,这称为动态绑定。 Child和Base的虚方法表如图: 对Child类型来说:
当通过对象动态绑定方法的时候,只需要查找这个表就可以了,而不需要挨个查找每个父类。 |
变量访问过程 | 对变量的访问是静态绑定的,无论是类变量还是实例变量。 | b.s和c.s,通过对象访问类变量,系统会转换为直接访问类变量Base.s和Child.s。 |
27届毕业生-Java知识专辑 文章被收录于专栏
知其然知其所以然,只有掌握了底层原理,借助第一性原理,才可以在日常开发和项目中运用自如,潇洒走江湖。 专为27届毕业生准备,托起您的就业梦。 该专辑会不定时更新,建议27届同学订阅,入职后扎实的基本功可以帮您争取更好的机会和项目。

查看78道真题和解析