Java 中静态绑定和动态绑定的区别
本教程介绍了 Java 中静态绑定和动态绑定之间的区别,并列出了一些示例代码以指导你了解该主题。
绑定是将方法调用链接到方法实现,它有两种类型:静态和动态。根据多态性,一个对象可以有多种形式,这些形式可以在编译时或运行时解析。
当方法调用在编译时链接到方法实现时,就会发生静态绑定。动态绑定在运行时发生时发生。让我们详细了解这两种类型的绑定并查看一些示例。
Java 中的静态绑定
如上所述,静态绑定是在编译期间发生的绑定。它也被称为早期绑定,因为它发生在编译时本身。
在静态绑定的情况下,编译器确切地知道要使用该方法的哪个实现。它使用类型或类信息进行绑定;方法重载是静态绑定的一个例子。
请注意,所有静态、最终和私有方法都使用静态绑定,因为子类不能覆盖静态、最终和私有方法。编译器在编译时就知道要使用哪个方法实现。由于编译器知道父类的方法不能被覆盖,所以它总是使用该方法的父类实现。
此外,静态绑定的性能也更好,因为涉及的开销更少。
Java 中的静态绑定示例
在下面的代码中,学校类有一个静态的 ringBell()
方法,这个类是由 Classroom
扩展的。Classroom
也有一个 ringBell()
方法。
由于 ringBell()
方法是静态的,编译器只检查类类型,不考虑分配的对象类。因此,当使用 School
类创建 Classroom(School s2 = new Classroom())
的对象时,将考虑 School
类的实现,而不是 Classroom
类。
class School {
public static void ringBell() {
System.out.println("Ringing the school bell...");
}
}
class Classroom extends School {
public static void ringBell() {
System.out.println("Ringing the classroom bell...");
}
}
public class Main {
public static void main(String[] args) {
School s1 = new School(); // Type is School
s1.ringBell();
Classroom c1 = new Classroom(); // Type is Classroom
c1.ringBell();
School s2 = new Classroom(); // Type is School
s2.ringBell();
}
}
输出:
Ringing the school bell...
Ringing the classroom bell...
Ringing the school bell...
Java 中的动态绑定
动态绑定在运行时解析绑定,因为编译器不知道在编译时使用哪个实现。
它也被称为后期绑定,因为绑定是在运行时的后期而不是在编译时解析的。动态绑定也使用对象而不是类型或类进行绑定;方法覆盖是动态绑定的一个例子。
Java 中的动态绑定示例
在下面的代码中,ringBell()
方法不是静态的,因此会发生覆盖。编译器不知道应该使用 ringBell()
方法的哪个实现,因此绑定是在运行时解析的。
在运行时,会考虑对象而不是用于创建它的类型。因此,当使用 School
类创建 Classroom
的对象时,将考虑 Classroom
类的实现,而不是 School
类。
class School {
public void ringBell() {
System.out.println("Ringing the school bell...");
}
}
class Classroom extends School {
@Override
public void ringBell() {
System.out.println("Ringing the classroom bell...");
}
}
public class Main {
public static void main(String[] args) {
School s1 = new School(); // Type is School and object is of School
s1.ringBell();
Classroom c1 = new Classroom(); // Type is Classroom and object is of Classroom
c1.ringBell();
School s2 = new Classroom(); // Type is School and object is of Classroom
s2.ringBell();
}
}
输出:
Ringing the school bell...
Ringing the classroom bell...
Ringing the classroom bell...
Java 中的静态绑定和动态绑定
静态绑定是在编译时方法调用与方法实现的连接。另一方面,动态绑定是方法调用在运行时与方法实现的连接。
了解这两种技术对于理解多态性的概念很重要。静态绑定被静态、最终和私有方法使用;这种绑定在方法重载的情况下生效。动态绑定用于虚拟方法(在 Java 中默认为虚拟)并用于绑定被覆盖的方法。
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中执行 SQL 查询
发布时间:2024/04/24 浏览次数:1195 分类:Python
-
本教程演示了在 Python 中对 Pandas DataFrame 执行 SQL 查询。
在 Pandas 中使用 stack() 和 unstack() 函数重塑 DataFrame
发布时间:2024/04/24 浏览次数:1289 分类:Python
-
本文讨论了 Pandas 中 stack() 和 unstack() 函数的使用。