接口和类之间的区别是什么?为什么可以在类中直接实现方法,为什么应该使用接口? [英] What is the difference between an interface and a class, and why I should use an interface when I can implement the methods directly in the class?

查看:112
本文介绍了接口和类之间的区别是什么?为什么可以在类中直接实现方法,为什么应该使用接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个非常基本的问题,但是一名面试官以非常棘手的方式问我,我很无助:(

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

我只知道接口的材料或理论定义,并且在我从事的许多项目中也都实现了它.但是我真的不明白为什么这样做以及如何有用.

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

我也不了解界面中的一件事.例如,我们使用

I also don't understand one thing in interface. i.e for example, we use

conn.Dispose();在finally块中.但是我看不到该类正在实现或继承IDisposable接口(SqlConnection)类.我想知道如何仅可以调用方法名称.同样,我也不了解Dispose方法的工作原理,因为我们需要为所有接口方法使用自己的实现来实现函数体.那么接口如何被接受或命名为合同呢?直到现在,这些问题一直在我的脑海中浮现,坦率地说,我从未见过任何好的线索可以用我能理解的方式解释我的问题.

conn.Dispose(); in finally block. But I don't 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 works 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种方法,他很乐意直接在类中实现它,但是如果您必须使用Abstract类或接口,则选择哪种方法,为什么?我确实回答了我在各个博客中读到的所有东西,它们都说了抽象类和接口的优缺点,但是他不相信,他是在试图理解为什么要使用接口".即使我只能一次实现相同的方法而又不打算改变它,通常还是为什么要抽象类".

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 specific 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 a parameter any object that implements 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.就像他们问我要使用MyLogClass中的WriteLog()方法需要实现什么一样.他们将在界面中找到答案.

In my example, I could be a developer who writes MyLogClass, and the other developers, could create their 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.

这篇关于接口和类之间的区别是什么?为什么可以在类中直接实现方法,为什么应该使用接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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