What is the difference between static and dynamic binding?

前端 未结 6 869
予麋鹿
予麋鹿 2021-01-31 09:41

Binding times can be classified between two types: static and dynamic. What is the difference between static and dynamic binding?

Could you give a quick example of each

6条回答
  •  眼角桃花
    2021-01-31 10:34

    In the most general terms, static binding means that references are resolved at compile time.

    Animal a = new Animal();
    a.Roar(); // The compiler can resolve this method call statically.
    

    Dynamic binding means that references are resolved at run time.

    public void MakeSomeNoise(object a) {
       // Things happen...
       ((Animal) a).Roar(); // You won't know if this works until runtime!
    }
    

提交回复
热议问题