我有一个接口A我如何才能通过类B继承接口A [英] i have an interface A how can i inherit the interface A by Class B

查看:93
本文介绍了我有一个接口A我如何才能通过类B继承接口A的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请给我看看例子
接口如何通过类继承

can please show me the example
how the interface inherit by the class

推荐答案

hello

hello

Public Interface IConfirmable
  Sub Confirm()
End Interface
Public Class WebSalesOrder()
  Inherits SalesOrder
  Implements IConfirmable
  Public Sub Confirm() Implements IConfirmable.Confirm
    ' Code to confirm a web order
  End Sub
  ' Other WebSalesOrder code
End Class



sanjeev



sanjeev


给定接口:

Given the interface:

internal interface IConfirmable {
    bool Confirm();
    string Name { get; set; } // let's demonstrate properties as well
}



有两种形式:



There are two forms:

internal class MyConfirmableClassPublic : MyBaseClass, IConfirmable {
    public bool Confirm() { return this.Calculate(); }
    public string Name {
        get { return this.fName; }
        set { this.fName = value;}
    }
    bool Calculate() {/*...*/}
    string fName;
}

//explicit
internal class MyConfirmableClassPublic : MyBaseClass, IConfirmable {
    bool IConfirmable.Confirm() { return this.Calculate(); }
    string IConfirmable.Name {
        get { return this.fName; }
        set { this.fName = value;}
    }
    bool Calculate() {/*...*/}
    string fName;
}



尝试选择第二(显式)形式.它不使用类类型的变量提供对接口成员的访问.您需要将引用传递给接口,而不是传递给调用者的类.它改善了编程纪律.

另外,您可以实现多个接口.通过显式实现,您可以在相同的实现者类中具有相同的方法,这些方法具有来自相同接口的相同签名,但不能具有第一个(隐式公共形式).

给许多人带来一点惊喜:您不仅可以在类上实现接口,而且可以在结构上实现接口.我使第二种多态成为可能:基于接口的无层次结构.

通常,接口是"多重继承的弱形式"的工具:基类只是一个,但在继承列表中是多个接口.

—SA



Try to prefer second (explicit) form. It does not provide access to interface members using the variable of the class type. You need to pass the reference to interface, not to the class to the caller. It improved discipline of programming.

Also, you can implement more than one interface. With explicit implementation you can have identical methods with identical signature from different interface in the same implementor class, but not in the first (implicit public form).

A little surprise to many: you can implement interfaces not only on classes, but also on structures. I makes the second form of polymorphism possible: hierarchy-free, based on interfaces.

Generally, interfaces is a tool for the "weak form of multiple inheritance": base class is only one, but multiple interfaces in the inheritance list.

—SA


public interface A
{
   void DoFOO();
}

public class B: A
{
  public void DoFoo()
  { //implrementation
  }
}


这篇关于我有一个接口A我如何才能通过类B继承接口A的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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