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

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

问题描述

我看过很多次从class.Why生成的接口实例时只用本身的派生类的帮助下创建了这一明智?接口实例使用一个接口,我们可以通过这个实例访问仅此接口的成员。这是如何给出一个优势呢?我很困惑。

 接口IPRINT
{
    无效打印();
}类样品:IPRINT
{
    公共无效打印()
    {
        Console.WriteLine(打印...);
    }    公共无效样品()
    {
        Console.WriteLine(样本...);
    }
}类节目
{
    静态无效的主要(字串[] args)
    {
        iPrint的打印=新样品();
        print.Print();
    }
}


解决方案

接口定义的类必须能够做一些事情。这意味着,你知道对象正在处理会做你希望能够做什么。它可以让你更大的自由和OOP的优势。这是一个深刻的话题,而是一个非常基本的例子是这样的:

 公共接口IAnimal
{
    串说话();
}公共类犬:IAnimal
{
    公共字符串说话()
    {
        返回汪,汪
    }
}公共类猫:IAnimal
{
    公共字符串说话()
    {
        返回喵;
    }
}公共类鹦鹉:IAnimal
{
    公共字符串说话()
    {
        返回Sqwark!;
    }
}

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

 类节目
{
    静态无效的主要(字串[] args)
    {
        //写汪汪,汪汪
        IAnimal动物=新的狗();
        Console.WriteLine(animal.Speak());        //现在喵写道:
        动物=新猫();
        Console.WriteLine(animal.Speak());        //现在写Sqwark等
        动物=新鹦鹉();
        Console.WriteLine(animal.Speak());
    }
}

这也可以让你再进入的东西例如反转的控制权在那里你会采取一个项目像这样,你可以通过狗,猫或鹦鹉和方法总是工作,而不是知道或关心哪些动物是:

 公共无效ShoutLoud(IAnimal动物)
{
    MessageBox.Show(怒吼+ animal.Speak());
}

这则使得ShoutLoud 单元测试因为你可以使用一个模拟对象,而不是一个真正的动物。它基本上使你的code灵活和动态的而不是僵化和紧密耦合。

此外,扩大对马修的问题。在C#中,你只能从一个基类继承,但你可以有多个接口。所以,你可以有:

 公共类犬:IAnimal,IMammal,ICarnivor

这可以让你有小的接口(推荐)的话,您可以建立在什么项目可以/必须这样做给人最大程度的控制。

I have seen many times an Interface instance generated from a class.Why does use an Interface in this wise?An Interface instance created only itself with the help of the derived class and we can access only this 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();
    }
}

解决方案

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());
}

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.

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天全站免登陆