Home  >  Article  >  Backend Development  >  Inheritance and composition in C#

Inheritance and composition in C#

WBOY
WBOYforward
2023-09-08 16:21:041305browse

C# 中的继承与组合

Inheritance

Inheritance allows you to specify that a new class should inherit members of an existing class. This existing class is called the base class and the new class is called the derived class. Inheritance implements the IS-A relationship. For example, a mammal is an animal, a dog is a mammal, therefore a dog is an animal, and so on.

For example, the base class Shape has derived classes such as Circle, Square, Rectangle, etc.

Composition

Under composition, if the parent object is deleted, the child object will also loses its status. A combination is a special type of aggregation and gives partial relationships.

For example, a car has an engine. If the car is destroyed, the engine will be destroyed as well.

Example

public class Engine {
   . . .
}
public class Car {
   Engine eng = new Engine();
   .......
}

The above is the detailed content of Inheritance and composition in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete
Previous article:Boxing and unboxing in C#Next article:Boxing and unboxing in C#