Encapsulation, inheritance and polymorphism in C#

1 Introduction

In object-oriented programming, encapsulation, inheritance and polymorphism are three important concepts. They are fundamental features in the C# language for designing and implementing code with high cohesion and low coupling. This article will introduce in detail the relevant knowledge of encapsulation, inheritance and polymorphism in C#.


Insert image description here



2. Encapsulation

Encapsulation is a mechanism that combines data and methods into a single unit for the purposes of information hiding and access control. In C#, encapsulation is achieved through the use of classes and access modifiers.

2.1 Class

Classes are the basic unit of encapsulation in C#. A class is an abstract data type that defines a set of properties and methods. Properties represent the state of a class, while methods represent the behavior of a class. By using classes, we can combine related data and methods to form an entity with specific functionality.

Here is an example class definition:

public class Person
{
    
    
    private string name;
    private int age;

    public string Name
    {
    
    
        get {
    
     return name; }
        set {
    
     name = value; }
    }

    public int Age
    {
    
    
        get {
    
     return age; }
        set {
    
     age = value; }
    }
}

In the above example, the Person class contains two private fields name and age, and the corresponding public properties Name and Age. Private fields can only be accessed within the class, while public properties can be accessed and modified by other classes. This achieves the encapsulation of data.

2.2 Access modifiers

In C#, access modifiers are used to control access to class members. Commonly used access modifiers are public, private, protected and internal.

  • public: means public and can be accessed from anywhere.
  • private: means private and can only be accessed within the class.
  • protected: means protected and can be accessed within the class and derived classes.
  • internal: Indicates that it can only be accessed within the current assembly.

By using appropriate access modifiers, you can achieve reasonable encapsulation of class members and improve the security and maintainability of your code.


3. Inheritance

Inheritance is a mechanism for extending an existing class by defining a new class. Through inheritance, a class can obtain the properties and methods of the parent class and redefine or extend them. In C#, inheritance is implemented using the keywords class and base.

Here is an example class inheritance:

public class Student : Person
{
    
    
    private string school;

    public string School
    {
    
    
        get {
    
     return school; }
        set {
    
     school = value; }
    }

    public void Study()
    {
    
    
        Console.WriteLine("I am studying at " + school);
    }
}

In the above example, the Student class inherits from the Person class and adds a school attribute and a learning method. Through inheritance, the Student class can use the properties and methods in the Person class, and can also extend new functions.


4. Polymorphism

Polymorphism is a feature that allows the same method to produce different results on different objects. In C#, polymorphism is implemented through virtual methods, abstract classes, and interfaces.
Insert image description here

4.1 Virtual methods

A method declared asvirtual in a base class can be overridden in a derived class. When an overridden method is called, the method of the corresponding derived class is actually executed.

Here's an example:

public class Shape
{
    
    
    public virtual void Draw()
    {
    
    
        Console.WriteLine("Drawing a shape");
    }
}

public class Circle : Shape
{
    
    
    public override void Draw()
    {
    
    
        Console.WriteLine("Drawing a circle");
    }
}

public class Rectangle : Shape
{
    
    
    public override void Draw()
    {
    
    
        Console.WriteLine("Drawing a rectangle");
    }
}

In the above example, the Draw method in the Shape class is declared asvirtual, and the Circle class and Rectangle class override this method respectively. When the Draw method is called, the method of the corresponding derived class is actually executed.

4.2 Abstract class

An abstract class is a class that cannot be instantiated, can only be inherited, and can contain abstract members and concrete members. Abstract members must be implemented in derived classes.

Here's an example:

public abstract class Shape
{
    
    
    public abstract void Draw();
}

public class Circle : Shape
{
    
    
    public override void Draw()
    {
    
    
        Console.WriteLine("Drawing a circle");
    }
}

public class Rectangle : Shape
{
    
    
    public override void Draw()
    {
    
    
        Console.WriteLine("Drawing a rectangle");
    }
}

In the above example, the Shape class is an abstract class, and the Draw method is an abstract member. The Circle class and Rectangle class must implement the Draw method, otherwise the compilation will report an error.

4.3 Interface

An interface is a type that defines a set of members and does not contain a specific implementation. A class can implement multiple interfaces and provide specific implementations of corresponding members.

Here's an example:

public interface IDrawable
{
    
    
    void Draw();
}

public class Circle : IDrawable
{
    
    
    public void Draw()
    {
    
    
        Console.WriteLine("Drawing a circle");
    }
}

public class Rectangle : IDrawable
{
    
    
    public void Draw()
    {
    
    
        Console.WriteLine("Drawing a rectangle");
    }
}

In the above example, the IDrawable interface defines the Draw method, and the Circle class and Rectangle class implement the interface and provide specific implementations.


5. Summary

Encapsulation realizes the encapsulation of data and methods through classes and access modifiers; inheritance realizes the extension and reuse of code through base classes and derived classes; polymorphism realizes the characteristics of the same method producing different results on different objects through virtual methods, abstract classes and interfaces. . These features allow us to write more flexible and extensible code. I hope this article will help you understand encapsulation, inheritance and polymorphism in C#.

Guess you like

Origin blog.csdn.net/qq_22120623/article/details/135033420