
不同绑定的比较
1、静态绑定发生在编译期,动态绑定发生在运行期。
2、动态绑定的灵活性高于静态绑定,因为静态绑定是在编译过程中确定的,动态绑定在编译过程中不知道调用哪种方法。
3、静态绑定调用方法比动态绑定快,因为静态绑定可以直接调用,动态绑定需要搜索方法表。
实例
静态绑定
classSuper{
publicstaticvoidsample(){
System.out.println("Thisisthemethodofsuperclass");
}
}
PublicclassSubextendsSuper{
Publicstaticvoidsample(){
System.out.println("Thisisthemethodofsubclass");
}
Publicstaticvoidmain(Stringargs[]){
Sub.sample()
}
}
(2)动态绑定
classSuper{
publicvoidsample(){
System.out.println("Thisisthemethodofsuperclass");
}
}
PublicclassextendsSuper{
Publicstaticvoidsample(){
System.out.println("Thisisthemethodofsubclass");
}
Publicstaticvoidmain(Stringargs[]){
newSub().sample()
}
}
以上就是java静态和动态绑定的对比,希望对大家有所帮助。更多Java学习指路:Java基础
原文来自:https://www.py.cn© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END

















































暂无评论内容