接口和类有什么区别,为什么我可以在类中直接实现方法时使用接口? [英] 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?

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

问题描述

我知道这是一个非常基本的问题,但面试官问了我一个非常狡猾的方式,我很无助:(

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 个方法,他很乐意直接在类中实现它,但是如果您必须选择抽象类或接口,您选择哪一个,为什么?我确实回答了我在各种博客中读到的所有内容,说抽象类和接口的优缺点,但他不相信,他试图从总体上理解为什么是接口".为什么是抽象类",即使我只能实现一次相同的方法并且不会改变它.

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