Some object-oriented languages allow a class to inherit from multiple base classes, while other object-oriented languages allow inheritance from only one class, but can be arbitrarily inherited from several interfaces or pure abstract classes.
Only C + + supports multilevel inheritance, and many programmers have mixed opinions on this. Multi-level inheritance often causes confusion between classes that inherit, and inherited methods tend not to be unique, so the inheritance of classes in C # can only be one, that is, subclasses only derive from a parent class, and sometimes you have to inherit the attributes of multiple classes, in order to implement multiple inheritance must use interface technology, The following is an introduction to the multiple inheritance of interfaces:
Using System;
Defines an interface that describes a point
Interface IPoint
{
int x {
get;
Set;
}
int y {
get;
Set;
}
}
Interface IPoint2
{
int y {
get;
Set;
}
}
Two parent class interfaces were inherited in point and two methods of parent class interfaces were used
Class Point:ipoint,ipoint2
{
Defines a private member variable for two-class internal access
private int PX;
private int PY;
Public point (int x,int y) {
Px=x;
Py=y;
}
Defined properties, IPoint interface method implementation
public int X
{
Get
{return PX;}
Set
{PX =value;}
}
IPoint1 interface Method implementation
public int Y
{
Get
{return PY;}
Set
{PY =value;}
}
}
Class Test
{
private static void OutPut (IPoint p)
{Console.WriteLine ("X={0},y={1}", p.x,p.y);}
public static void Main () {
Point P =new Point (15,30);
Console.Write ("The New point is:");
OutPut (P);
String MyName =console.readline ();
Console.Write ("My name is {0}", myName);
}
}
C # inheritance mechanism multilevel inheritance