Current location - Quotes Website - Signature design - Problems in C# Inheritance
Problems in C# Inheritance
Single inheritance mechanism, a subclass can only have one parent class.

C# does not allow a subclass to have more than one parent class.

C# allows multiple inheritance of interfaces, but does not allow multiple inheritance of classes, so two or more inheritance in c# can only be realized through interfaces, and multiple inheritance of classes is allowed in c++.

Examples are as follows

Use the system;

Use the system. Assemble. Generics;

Use the system. Linq

Use the system. Text;

Namespace inheritance

{

Common interface Ishape

{

double Area();

double GramLength();

Intermediate edge

{

get

}

}

Common interface IshapePlay

{

void Play();

}

Open class square: Ishape, IshapePlay

{

Private int party;

public int SideLength

Public Square ()

{

Number of sides = 4;

}

Public interior

{

get

{

Return to both sides;

}

}

Public double area ()

{

Return ((double) (side length * side length));

}

Public double GramLength ()

{

return((double)(Sides * side length));

}

Public invalid game ()

{

Console. Writeline ("\ nThe square area is calculated as follows:");

Console. WriteLine ("side length:", this. Side length);

Console. WriteLine ("number of sides:", this. sides);

Console. WriteLine ("area: ",this. area());

}

}

Class plan

{

Static void Main(string[] args)

{

Square sq = new Square();

sq。 Side length = 8;

sq。 play();

}

}

}

Classes with the keyword abstract but not fully defined in the implementation content are called abstract classes.

The differences between abstract classes and interfaces are as follows:

① When a class inherits an abstract class, it only needs to implement some concrete methods and all abstract methods, and the interface needs to implement all the methods inside.

② There are no member variables in the interface, but there can be member variables in the abstract class.

The introduction of interfaces in Java is mainly to solve the problem of multiple inheritance.

The 1) interface cannot have non-abstract methods, but abstract classes can.

2) A class can implement multiple interfaces, but only one parent class.

3) The interface does not belong to the inheritance structure, and actually has nothing to do with inheritance, so unrelated classes can also implement the same interface.

Abstract classes and methods

In all our instrument examples, the methods in the basic instrument class are definitely "pseudo" methods. If you call these methods, you will get an error. This is because the purpose of Instrument is to create a common interface for all classes derived from it.

The only reason for establishing this universal interface is that it can make different representations for different subtypes. It establishes a basic form for us to define what is "universal" in all derived classes. Another way to explain this concept is to call Instrument "abstract basic class" (abstract class for short). If you want to handle a series of classes through this common interface, you need to create an abstract class. All derived methods that are consistent with the signature declared by the base class can be called through the dynamic binding mechanism (however, as pointed out in the previous section, if the method name is the same as the base class, but the independent variables or parameters are different, overloading will occur, which may not be what we want).

If there is an abstract class like Instrument, then the objects of that class are almost certainly meaningless. In other words, the function of Instrument is only to express the interface, not to express some specific implementation details. So it is meaningless to create an instrument object, and we should usually prohibit users from doing so. In order to achieve this goal, all methods in the instrument can display error messages. However, this will delay the information until the runtime, and it needs to be thoroughly and reliably tested at the user end. In any case, the best way is to find problems during compilation.

To solve this problem, Java provides a mechanism called "abstract method". It is an incomplete method, containing only one declaration and no method body. The following syntax is used when declaring abstract methods:

Abstract void x ();

Classes that contain abstract methods are called abstract classes. If a class contains one or more abstract methods, the class must be designated as abstract. Otherwise, the compiler will report an error message to us.

If an abstract class is incomplete, what will the compiler do once someone tries to generate an object of that class? Because it is unsafe to create an object belonging to an abstract class, the compiler will give an error prompt. In this way, the compiler can ensure the "purity" of abstract classes, and we don't have to worry about misuse.

If you inherit from an abstract class and want to generate a new type of object, you must provide method definitions for all abstract methods in the base class. If you don't do this (you can choose not to do it at all), the derived class will also be abstract, and the compiler will force us to mark the "abstract" nature of that class with the abstract keyword.

A class can be declared as an "abstract class", even if it does not contain any abstract methods. This ability is very useful if a class does not need any abstract methods and we want to ban all instances of the class.

Engagement/interface

The keyword "interface" takes the abstract concept to a deeper level. We can think of it as a purely abstract class. It allows the creator to specify the basic form of the class: method name, parameter list and return type, but does not specify the method body. Interfaces also contain data members of basic data types, but they all default to static and final. The interface only provides a form, and does not provide the details of implementation.

The interface describes itself like this: "All classes that implement me should now look like me." . Therefore, all code that uses a specific interface knows what methods that interface can call. This is the whole meaning of the interface. So we often use interfaces to establish "protocols" between classes. Some object-oriented programming languages use a keyword called "protocol", which has the same function as an interface.

To create an interface, use the interface keyword instead of class. Similar to the class, we can add a public keyword before the interface keyword (but only the interface is defined in the file with the same name); Or omit it to create a "friendly" state.

In order to generate a class that conforms to a specific interface (or a set of interfaces), the implements keyword is used. What we want to express is that "the interface looks like that, and here are its specific working details". Besides these, our other job is very similar to inheritance.