为什么我应该去的接口在C#中时,我可以直接实现方法 [英] Why I should go for Interfaces in C# when I can implement the methods directly

查看:118
本文介绍了为什么我应该去的接口在C#中时,我可以直接实现方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,这是一个非常基本的问题,但一位面试官问我很招的方式。我很无奈:(

I am aware that this is a very basic question, but an interviewer asked me very trick way. I was helpless :(

我知道的接口,并在我的工作在许多项目还实施了,但我真的不明白为什么和如何唯一材料或理论的定义。这是很有用的。

I know only material or theoretical definition for an interface and also implemented in many projects I worked on. But I really don't understand why and how is this useful.

我不明白接口的一件事。也就是说,例如,我们用

I also dont understand one thing in interface. i.e for example, we use

conn.Dispose(); 在finally块,但我没有看到该类正在实施或继承的IDisposable 接口(的SqlConnection )类我的意思。我想知道我怎么可以只调用的方法名。此外,在同样的事情,我不理解如何处置方法工作的,因为我们需要实现函数体用我们自己的实现所有的接口方法,所以接口是如何接受或命名为合同?这些问题一直在我的脑海里翻滚到现在为止,并坦白说,我从来没有见过任何好的线程可以解释在某种程度上我的问题,我可以理解。

conn.Dispose(); in finally block. But I dont see that class is implementing or inheriting IDisposable interface (SqlConnection) class I mean. I am wondering how I can just call the method name. Also in the same thing, I am not understanding how Dispose method work as because, we need to implement the function body with our own implementation for all interface methods. So how Interfaces are accepted or named as contracts ? These questions kept on rolling in my mind till now and frankly I never saw any good thread that would explain my questions in a way that I can understand.

MSDN和往常一样看起来很吓人,没有一行是明确的有(伙计们,请原谅谁是进入高层次发展,我强烈地感到,任何代码或。文章应达到人谁看到它,因此,像许多人说的心中,MSDN是用不)

MSDN as usual looks very scary and no single line is clear there (Folks, kindly excuse who are into high level development, I strongly feel that any code or article should reach the mind of anyone who see it, hence like many others say, MSDN is not of use).

面试官说:

他有5个方法,他很高兴地直接实现它的类,但如果你有去抽象类或接口,你选择哪一个,为什么?我没有回答他所有的,我在不同的博客阅读话说优势,既抽象类和接口的缺点的东西,但他不服气,他正在试图了解为什么界面一般。 为什么抽象类一般来说,即使我可以实现同样的方法只有一个时间,而不是戈纳改变它。

He has 5 methods and he is happy to implement it in the class directly, but if you have to go for Abstract class or interface, which one you choose and why ? I did answered him all the stuffs that I read in various blog saying advantage and disadvantage of both abstract class and interface, but he is not convinced, he is trying to understand "Why Interface" in general. "Why abstract class" in general even if I can implement the same methods only one time and not gona change it.

我看到网没有在那里,我能得到一个文章认为可以解释清楚我对接口和其运作。我的很多程序员,谁仍然不知道接口(我知道的理论和方法,我用的),但不确信我的理解它清楚地之一。

I see no where in net, I could get an article that would explain me clearly about interfaces and its functioning. I am one of those many programmers, who still dont know about interfaces (I know theoretical and methods I used) but not satisfied that I understood it clearly.

推荐答案

当你想创造一些喜欢它的接口是优秀的:

Interfaces are excellent when you want to create something like it:

 using System;

namespace MyInterfaceExample
{
    public interface IMyLogInterface
    {
        //I want to have a especific method that I'll use in MyLogClass
        void WriteLog();       
    }

    public class MyClass:IMyLogInterface
    {

        public void WriteLog()
        {
            Console.Write("MyClass was Logged");
        }
    }

    public class MyOtherClass :IMyLogInterface
    {

        public void WriteLog()
        {
            Console.Write("MyOtherClass was Logged");
            Console.Write("And I Logged it different, than MyClass");
        }
    }

    public class MyLogClass
    {
        //I created a WriteLog method where I can pass as parameter any object that implement IMyLogInterface.
        public static void WriteLog(IMyLogInterface myLogObject)
        {
            myLogObject.WriteLog(); //So I can use WriteLog here.
        }
    }

    public class MyMainClass
    {
        public void DoSomething()
        {
            MyClass aClass = new MyClass();
            MyOtherClass otherClass = new MyOtherClass();

            MyLogClass.WriteLog(aClass);//MyClass can log, and have his own implementation
            MyLogClass.WriteLog(otherClass); //As MyOtherClass also have his own implementation on how to log.
        }
    }
}

在我的例子,我可以是谁写的MyLogClass开发商,以及其他开发人员,可以创建自己的类,而当他们想登录,他们实现接口IMyLogInterface。这是因为他们问我,他们需要实现使用WRITELOG方法MyLogClass什么。答案,他们会发现在接口。

In my example, I could be a developer who write MyLogClass, and the other developers, could create they classes, and when they wanted to log, they implement the interface IMyLogInterface. It is as they were asking me what they need to implement to use WriteLog method in MyLogClass. The answer they will find in the interface.

这篇关于为什么我应该去的接口在C#中时,我可以直接实现方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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