为什么我们从接口而不是类创建对象实例? [英] Why we do create object instance from Interface instead of Class?

查看:24
本文介绍了为什么我们从接口而不是类创建对象实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经多次看到从一个类生成一个接口实例.为什么我们这样使用接口?接口实例仅在派生类的帮助下创建,我们只能通过该实例访问这些接口成员.这如何带来优势?我好糊涂.

I have seen an Interface instance being generated from a class many times. Why do we use interface this way? An interface instance is created only itself with the help of the derived class and we can access only these interface members through this instance. How does this give an advantage? I'm so confused.

interface IPrint
{
    void Print();
}

class Sample : IPrint
{
    public void Print()
    {
        Console.WriteLine("Print...");
    }

    public void Sample()
    {
        Console.WriteLine("Sample...");
    }
}

class Program
{
    static void Main(string[] args)
    {
        IPrint print = new Sample();
        print.Print();
    }
}

推荐答案

接口定义了一个类必须能够做一些事情.这意味着您知道正在处理的对象将执行您希望能够执行的操作.它让您拥有更大的 OOP 自由和优势.这是一个很深的话题,但一个非常基本的例子是:

Interfaces define that a class MUST be able to do something. This means that you know the object being worked on will do what you want to be able to do. It allows you greater freedom and advantages of OOP. This is a deep topic but a very basic example would be this:

public interface IAnimal
{
    string Speak();
}

public class Dog : IAnimal
{
    public string Speak()
    {
        return "Woof, woof";
    }
} 

public class Cat : IAnimal
{
    public string Speak()
    {
        return "Meow";
    }
} 

public class Parrot : IAnimal
{
    public string Speak()
    {
        return "Sqwark!";
    }
} 

然后你可以使用任何你喜欢的动物!

Then you could use any animal you like!

class Program
{
    static void Main(string[] args)
    {
        // Writes Woof, Woof
        IAnimal animal = new Dog();
        Console.WriteLine(animal.Speak());        

        // Now writes Meow
        animal = new Cat();
        Console.WriteLine(animal.Speak());

        // Now writes Sqwark etc
        animal = new Parrot();
        Console.WriteLine(animal.Speak());
    }
}

这也允许您进入诸如控制反转之类的事情,在那里您可以像这样接收一个项目,您可以通过一只狗、猫或鹦鹉,该方法将始终有效,而不是知道或关心它是哪种动物:

This also allows you to then get into things like Inversion Of Control where you would take an item in like this and you could pass a dog, cat or parrot and the method would always work, not knowing or caring which animal it was:

public void ShoutLoud(IAnimal animal)
{
    MessageBox.Show("Shout " + animal.Speak());
}

这使得 ShoutLoud 单元可测试,因为您可以使用模拟对象而不是真正的动物.它基本上使您的代码灵活和动态,而不是僵化和紧耦合.

This then makes ShoutLoud unit testable because you could use a mock object rather than a real animal. It basically makes your code flexible and dynamic rather than rigid and tightly coupled.

另外,扩展马修的问题.在 C# 中,您只能从一个基类继承,但可以有多个接口.所以,你可以:

Also, expanding on Matthew's question. In C# you can only inherit from one base class but you can have multiple interfaces. So, you could have:

public class Dog : IAnimal, IMammal, ICarnivor

这允许您拥有小型接口(推荐),然后您可以进行构建,从而最大限度地控制项目可以/必须做什么.

This allows you to have small interfaces (recommended) that then allow you to build up so giving maximum control over what an item can / must do.

这篇关于为什么我们从接口而不是类创建对象实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆