Interface instantiation vs class instantiation

前端 未结 4 1150
予麋鹿
予麋鹿 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条回答
  •  Happy的楠姐
    2020-12-08 01:28

    There's really no such thing as "interface instantiation", but there are interface variables. The first line is an example of one such variable.

    With the first line, you could instantiate container to be any concrete class that implements IUnityContainer. With the second line, the container object could only be instantiated from the UnityContainer class or a derived class.

    When you use interface variables in your code, it allows you to more easily switch out the concrete implementation, which makes your code more flexible.

提交回复
热议问题