简体   繁体   English

如果我有两个接口,那么一个类可以继承两个接口吗?

[英]If I have two interfaces, can one class inherit both?

I have a class with 2 interfaces, and I have some superclasses with subclasses, I would like the superclasses to inherit both interfaces. 我有一个带有2个接口的类,并且我有一些带有子类的超类,我希望这些超类继承这两个接口。 if I just reference the class the interfaces its in, will it work? 如果我只引用该类的接口,它将起作用吗? ie SuperClass : Myinterfaces 即超类:Myinterfaces

here is the class with the interfaces 这是带有接口的类

public class Myinterfaces
{
    public interface IBakeable
    {
        int OvenTemp { get; }
    }

    public interface IAccounting
    {
       int Cost { get; }
    }

    public enum Colors
    { 
        red = 1,
        blue,
        yellow
    }
}

and heres an example of the superclass 这是超类的一个例子

public class CeramicsSuperClass : Myinterfaces
{
    public string ItemName { get; set; }
    public int Cost { get; set; }
    public int OvenTemp { get; set; }
}
public class Vases : CeramicsSuperClass
{
    private int _BaseDiam;
    public Vases(int diam)
    {
        _BaseDiam = diam;
    }

}

You are doing in a wrong way to implement multi-interfaces for a class, try this instead: 您执行错误的方式来实现类的多接口,请尝试以下操作:

public class CeramicsSuperClass : IBakeable, IAccounting {
  public string ItemName { get; set; }
  public int Cost { get; set; }
  public int OvenTemp { get; set; }
}

A class can inherit from only another class but it can implement as many interfaces as possible. 一个类只能从另一个类继承,但是它可以实现尽可能多的接口。 When a class inherits from another class and implement some interface, the base class should be listed first, then the interfaces go after like this: 当一个类从另一个类继承并实现某个接口时,应首先列出基类,然后再按如下所示进行接口:

//class A inherits from class B and implements 2 interfaces IC and ID
public class A : B, IC, ID {
  //...
}

Simple answer: You can inherit mulitple interfaces, not multiple classes. 简单的答案:您可以继承多个接口,而不是多个类。

public interface InterfaceA 
{
    string PropertyA {get;}
}

public interface InterfaceB
{
    string PropertyB {get;}
}

public abstract class BaseClassForOthers : InterfaceA, InterfaceB
{
    private string PropertyA {get; private set;}
    private string PropertyA {get; private set;}

    public BaseClassForOthers (string a, string b)
    {
        PropertyA  = a;
        PropertyB  = b;
    }

}

public class SubClass : BaseClassForOthers 
{
    public SubClass (string a, string b)
        : base(a, b)
    {
    }

}

may be looking here will get you in the general direction (msdn link about interface usage): http://msdn.microsoft.com/en-us/library/vstudio/ms173156.aspx 可能会在这里找到大致的指导(有关接口使用的msdn链接): http : //msdn.microsoft.com/zh-cn/library/vstudio/ms173156.aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以强制泛型类从两个接口之一继承类型? - Can a generic class be forced to have a type inherit from one of two interfaces? 如果两个脚本继承自同一个 class 并且可以调用 function 来影响两者,我如何找出它影响哪一个? - If two scripts inherit from the same class and a function can be called to effect both, how can I find out which one it affects? 我的程序有两个接口,并且都有一个同名的方法。 那么,我们如何在继承两个接口的子类中实现它们呢? - My program has two interfaces and both have a method with the same name. So, how can we implement them in a child class inheriting both interfaces? 如何在一个类中同时具有抽象方法和虚拟方法? - How can I have both abstract and virtual methods in one class? 我可以在父类中拥有一个“受保护”成员并在子类中继承它吗? - Can I have a 'protected' member in a parent class and inherit it in a child class? 为什么AC#类可以隐式和明确地从一个接口继承? - Why Can A C# Class Inherit From One Interface Both Implicitly and Explicitly? 如何使一个类既实现接口又从另一个类继承 - How can I make a class both implement an Interface and inherit from another class 我如何串联两个不同的列表 <T> 两者都有相同的基类? - How can i concatenate two different list<T> where both have same base class? 为什么我不能在Windsor中为多个接口注册一个类? - Why can I not register one class for multiple interfaces in Windsor? 一个类是否可以从另一个类继承并同时实现了一个接口? - can one class inherit from an other class an have implemented an interface at same time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM