Home > Backend Development > C#.Net Tutorial > Inheritance and composition in C#

Inheritance and composition in C#

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-09-08 16:21:04
forward
1435 people have browsed it

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();
   .......
}
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template