Interface instantiation vs class instantiation

前端 未结 4 1137
予麋鹿
予麋鹿 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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-08 01:36

    Interfaces can't be instantiated by definition. You always instantiate a concrete class.

    So in both statements your instance is actually of type UnityContainer.

    The difference is for the first statement, as far as C# is concerned, your container is something that implements IUnityContainer, which might have an API different from UnityContainer.


    Consider:

    interface IAnimal 
    {
        void die();
    }
    
    class Cat : IAnimal 
    {
        void die() { ... }
        void meow() { ... }
    }
    

    Now :

    IAnimal anAnimal = new Cat();
    Cat aCat= new Cat();
    

    C# knows for sure anAnimal.die() works, because die() is defined in IAnimal. But it won't let you do anAnimal.meow() even though it's a Cat, whereas aCat can invoke both methods.

    When you use the interface as your type you are, in a way, losing information.

    However, if you had another class Dog that also implements IAnimal, your anAnimal could reference a Dog instance as well. That's the power of an interface; you can give them any class that implements it.

    0 讨论(0)
  • 2020-12-08 01:38

    Interface instantiation is not possible. But when we created an an object for interface using its implemented class it works.

    IUnityContainer container = new UnityContainer()// It is assigning an object of its implemented class.
    UnityContainer container = new UnityContainer() // To access directly by using like this.
    

    Assume that there is a multiple inheritance, to achieve this we need to go with first.

    There is no need to provide such things we go with second way.

    Interfaces restricts direct access of the data and members of its class.

    0 讨论(0)
提交回复
热议问题