简体   繁体   中英

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. if I just reference the class the interfaces its in, will it work? ie SuperClass : 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM