[英]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.