什么是 Java 中的绑定?
在 Java 中,方法调用与方法体间的关联称为绑定。有两种绑定。
静态绑定
在静态绑定中,方法调用在编译时与方法体绑定。这也被称为早期绑定。这使用静态的、私有的和最终方法来完成。
示例
class Super{
public static void sample(){
System.out.println("This is the method of super class");
}
}
Public class Sub extends Super{
Public static void sample(){
System.out.println("This is the method of sub class");
}
Public static void main(String args[]){
Sub.sample()
}
}
输出
This is the method of sub class
动态绑定
在动态绑定中,方法调用在运行时与方法体绑定。这也被称为后期绑定。这使用实例方法来完成。
示例
class Super{
public void sample(){
System.out.println("This is the method of super class");
}
}
Public class extends Super{
Public static void sample(){
System.out.println("This is the method of sub class");
}
Public static void main(String args[]){
new Sub().sample()
}
}
输出
This is the method of sub class
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP