Interface instantiation vs class instantiation

前端 未结 4 1138
予麋鹿
予麋鹿 2020-12-08 00:51

Could someone please helpme to understand if the following codes are same. If not what\'s the difference between class and interfance instantiation.

IUnityCo         


        
4条回答
  •  离开以前
    2020-12-08 01:18

    The object instantiated and stored in the IUnityContainer container variable is considered by the compiler to have only the members defined in the IUnityContainer interface. That is, if the UnityContainer class contains members that aren't defined by the IUnityContainer interface, you won't be able to invoke them. On the other hand, you could "put" any object that implements the IUnityContainer interface in the IUnityContainer container variable--not just instances of UnityContainer. With the second declaration, you're stuck with instances of UnityContainer and objects in its inheritance hierarchy.

    Check out the C# Programming Guide to Interfaces for more information on interfaces and how they're used.

提交回复
热议问题