简体   繁体   中英

Multiple inheritance of interfaces

I was tasked with implementing an interface IPerson and class Person that has the following properties:

Title 
Name 
DateOfBirth
Age 

Implement an interface IDetails and class Details that inherits from IPerson and class Person that has these properties:

Religion 
National Insurance Number 

My question is, how can I do multiple inheritance for interfaces? How can IPerson inherit/derive from IDetails?

Technically IDetails extends IPerson , but conceptually it's similar to inheriting:

public interface IPerson
{
    string Title {get; set;}
    string Name {get; set;}
    int DoB {get; set;}
    int Age {get; set;}
}

public interface IDetails : IPerson
{
    int Religion {get; set;}
    int NationalInsuranceNumber {get; set;}
}

Now any class that implements IDetails must provide an implementation for all members of IDetails and IPerson .

An example implementation would be:

public class PersonWithDetails : IDetails
{
    public string Title {get; set;}
    public string Name {get; set;}
    public int DoB {get; set;}
    public int Age {get; set;}

    public int Religion {get; set;}
    public int NationalInsuranceNumber {get; set;}

}

Note that a class can implement multiple interfaces but can only inherit from one base class.

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