zoukankan      html  css  js  c++  java
  • 关于“绑定技术”(binding)(续)

    所谓“绑定(binding)”:就是建立method call(函数调用)和method body(函数本体)的关联。如果绑定发生于程序运行之前,称为“先期绑定(early binding),过程式语言无其他选择,只有先期绑定一种方式;绑定动作在程序执行期才根据对象的型别进行,称为“后期绑定(late binding)”,或称为“执行期绑定(run-time binding),或“动态绑定(dynamic binding)”。
            Java的所有函数,除了声明为final的函数之外,都属于后期绑定,后期绑定动作会自动发生。
            final的作用是禁止对函数进行覆写(overridding),而且告诉编译器该函数不需要后期绑定,程序从而可以获得较佳的效能,但实际上这么做并不会带来程序整体效能的提升。


    例:
     class A {
      
    public void play() {
         System.
    out.println(“A is running.“);
     }


    class B extends A {
      
    public void play() {
         System.
    out.println(“B is running.“);
     }


    class C extends A {
      
    public void play() {
         System.
    out.println(“C is running.“);
     }


    public class TestLateBinding {
      
    static void play(A a) {
        a.play();
      }

      
    public static void main(String[] args) {
       play(
    new A());
       play(
    new B()); // late binding
       play(new C()); // late binding
     }

    }



  • 相关阅读:
    科学计算和可视化,做数据分析与雷达图。
    Leetcode 429 N叉树的层序遍历
    Leetcode 867转置矩阵
    Leetcode 832 翻转图像
    Leetcode 1052 爱生气的书店老板
    Leetcode 337打家劫舍 III
    Leetcode 766 托普利茨矩阵
    Leetcode 1438绝对差不超过限制的最长连续子数组
    Leetcode 697 数组的度
    Leetcode 567 字符串的排列
  • 原文地址:https://www.cnblogs.com/johnny/p/17915.html
Copyright © 2011-2022 走看看