我如何实现接口 [英] How do I implement Interface

查看:92
本文介绍了我如何实现接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接口,有四种方法,比如m1(),m2(),m3()和m4()。父类和父类继承的接口由子类继承。现在我的问题是我可以在父类中实现m1()和m2()方法,在子类中实现m3()和m4()方法吗?

I have a Interface with four methods say m1(),m2(),m3() and m4(). My interface inherited by parent class and parent class is inherited by child class.Now my question is can I implement m1() & m2() methods in parent class and m3() & m4() methods in child class?

推荐答案

除了解决方案1和2:



首先,让我了解背后的理由。接口的目的是......为一些不同类型的对象提供一些公共接口,不仅是类,还有 struct 类型, 不可知方式(与具体运行时类型和实现细节无关)。在这种情况下,这些对象的用户获得编译时类型的引用,这是一些接口类型。接口类型引用提供对接口的所有成员的访问。如果某些成员没有实现,那就意味着要找到一个不存在的成员,试图到达一些随机存储区域,结果不可预测。



所以,你需要实施所有成员。但确实如此,有时您需要一些知道如何仅实现接口部分的基类。那么,实际上该做什么呢?首先,请注意,这种基类的实例化不具有任何实际意义。所以你可能想把它抽象化。然后你仍然需要实现整个接口,但有些实现可能是虚拟。然后派生类可以重新实现虚拟成员。这是最简单的例子:

In addition to solutions 1 and 2:

First of all, let me understand the rationale behind that. The purpose of the interface is to… provide some common interface to some objects of different types, not only classes, but also struct types, in an agnostic manner (agnostic to concrete runtime types and implementation detail). In this case, the user of these objects gets the reference of the compile time type which is some interface type. Interface-type reference provides access to all the members of the interface. If some of the members is not implemented, it would mean addressing a non-existent member, trying to reach some random memory area, with unpredictable results.

So, you need to implement all members. But true, sometimes you want some base class which "knows" how to implement only the part of the interface. So, what to do, practically? First of all, note that instantiation of such base class would not make any practical sense. So you may want to make it abstract. And then you still need to implement the whole interface, but some of the implementations could be "dummy". Then the derived class could re-implement the dummy members. This is the simplest trivial example:
interface IDemo {
    string First(string input);
    void Second();
}

abstract class Base : IDemo {
    string IDemo.First(string input) { return input; }
    void IDemo.Second() { }
    // ...
}

class Derived : Base, IDemo {
    void IDemo.Second() { DoSomething(); }
    //...
    void DoSomething() { /* ... */ }
}



只要有可能,我使用显式接口成员实现,但你也可以隐式实现(通过 public )。另请参阅下面的最后一段了解一些理由。



这个例子不是很好。首先,通过阅读代码,哪个方法实现是虚拟的,哪个是真实的,这一点并不明显。它有效,但这种方法的敏感性看起来有问题。让我们尝试创造更重要的东西:


Whenever possible, I use explicit interface member implementations, but you could also implement implicitly (via public). See also my last paragraph below for some rationale.

This example is not very good. First of all, it is not obvious, from reading the code, which method implementation is dummy and which is "real". It works but the sensibility of this approach looks questionable. Let's try to invent something more essential:

interface IDemo {
    string First(string input);
    void Second();
    void Third();
}

abstract class Base : IDemo {
    string IDemo.First(string input) { return FirstImplementation(input); }
    void IDemo.Second() { SecondImplementation(); }
    void IDemo.Third() { DoSomethingEssential(); }
    protected virtual string FirstImplementation(string input) { return input; }
    protected abstract void SecondImplementation();
    void DoSomethingEssential() { /* ... */ }
    // ...
}

class Derived : Base, IDemo {
    protected override void SecondImplementation() { DoSomethingReal(); }
    void DoSomethingReal() { }
}

您是否明白这一点:

接口仍然完全实现,但这一次,你可以清楚地告诉哪个实现是虚拟的:使用抽象方法的实现。因此,如果您创建派生类,并且它是非抽象的,编译器将强制您实现抽象方法。对于非抽象虚拟方法,您可以自由地覆盖它或保留 Base 类的实现。最后,第三方法已完全实现且无法覆盖,但各个接口成员可以重新实现,就像上面描述的第一种方法一样。



您可以根据自己的计划结合所有这些方法。我所描述的所有内容也适用于属性,也适用于隐式成员实现。



隐式实现看起来更简单 - 请参阅解决方案1.同时,显式实现更好地从类的其余部分抽象实现,这从用户的角度来看很重要:接口成员使用类引用保持对用户隐藏,仅对接口引用的用户可用。这提供了更好的封装,更好的隐藏实现细节。



-SA

Are you getting the idea:
Interface is still fully implemented, but this time, you can clearly tell which implementation is dummy: the one using the abstract method. So, if you create derived class, and if it is non-abstract, the compiler will force you to implement the abstract method. As to the non-abstract virtual method, you are free to either override it or leave the implementation of the Base class. Finally, the Third method is fully implemented and cannot be overridden, but respective interface member can be reimplemented, like in the first approach described above.

You can combine all these approaches according to your own plan. All I described is applicable to the properties as well, and to the implicit member implementations.

The implicit implementation looks much simpler — please see Solution 1. At the same time, explicit implementation better abstracts implementation from the rest of the class, which is important from the user's point of view: the interface members remain hidden from the user using the class reference, only available to the user of interface reference. This provides better encapsulation, better hides implementation detail.

—SA


NO,如果它是一个具体的父类,然后它必须实现所有的方法。



如果它是一个抽象的父类,那么它可以部分实现该接口。



示例:



NO,if it is a concrete parent class then it have to implement all the methods .

If it is a abstract parent class then it can partially implement the interface.

Example:

interface Ia
   {
       void m1();
       void m2();


   }
abstract   class a : Ia
   {
       public void m1()
       {
           Console.WriteLine("Hello");
       }
    public abstract void m2();

   }
   class MyClass:a
   {
         public override void m2()
       {
           Console.WriteLine("hi");
       }
   }


否。

如果你的类没有实现所有接口方法,那么它不是叫做实施......
No.
If your class does not implement all interface methods it is not called implementation...


这篇关于我如何实现接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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