C # Getting Started share (vi)--encapsulation, inheritance and polymorphism

Source: Internet
Author: User

C # is a purely object-oriented programming language, and the use of object-oriented thinking is very important for writing a large project. Like other object-oriented languages, C # is primarily implemented through encapsulation (encapsulating), Inheritance (inheritance), and polymorphism (polymorphism).

(i) Encapsulation

The customer wants to access the state of the object without using the method, but the designer of the class wants to hide the state of the class in the class member so that the client can access the object's state indirectly through the method. Attributes satisfy these two purposes: to encapsulate data through attributes (properties), to provide customers with simple "member-variable" interfaces, and to provide designers with the data-hiding necessary for the "use of methods to achieve" OO design.

From the previous blog about get and set controllers we can define some private members that store properties, such as:

You can add modifiers to get and set, such as Protected,internal,private, but when you use modifiers, both the get and set access methods are present and can only be decorated with one of them. And the modifier's access level is lower than the access level of the property or indexer. The internal keyword is an access modifier for a type and a member of a type. Only internal types or members can be accessed in files of the same assembly.

Sometimes you need to use the static constructor method to initialize, but you do not want the value to be changed when using the keyword readonly.


(ii) Succession

In object-oriented programming, there are two distinct types of inheritance: implementation inheritance and interface inheritance.

Implementation inheritance: Represents a type that derives from a base type that owns all member fields and functions of that base type. In implementing inheritance, the derived type takes the implementation code of each function of the base type, unless the implementation code that overrides a function is specified in the definition of the derived type. This type of inheritance is useful when you need to add functionality to an existing type, or if many of the related types share a significant set of common functionality.

Interface Inheritance: Indicates that a type inherits only the signature of a function and does not inherit any implementation code. This type of inheritance is best used when you need to specify that the type has some of the available attributes.

Some languages, such as C + +, support so-called "multiple inheritance," Where a class derives from more than one class. C # does not support multiple implementation inheritance. C #, in turn, allows a type to derive from multiple interfaces: multiple interface inheritance. This means that C # can derive from another class and any number of interfaces. More precisely, because System.Object is a common base class, each C # class (except for the object class) has a base class and can have any number of base interfaces.

To implement inheritance:

If you want to declare a class that derives from another class, you can use the following syntax (the base class puts the colon after it):

This syntax is very similar to the syntax in C + + and Java, but C + + programmers are accustomed to using the concept of public and private inheritance, and note that private inheritance is not supported, so there is no publicly or private qualifier on the base class name. Supporting private inheritance can only greatly increase the complexity of the language.

If the class (or struct) is also derived from the interface, the base class and interface in the list are separated by commas:



If you declare a base class function as virtual, you can override the function in any derived class:


Abstract classes and abstract functions:

C # allows classes and functions to be declared abstract. Abstract classes cannot be instantiated, and abstract functions cannot be implemented directly, and must be overridden in non-abstract derived classes. Obviously, the abstract function itself is also virtual (although there is no need to provide the virtual keyword, in fact, if the keyword is provided, it produces a syntax.) If a class contains abstract functions, the class is also abstract and must be known as abstract:

(c) polymorphic

Inheritance has two aspects of the most powerful, on the one hand can make the code has a strong reusability, on the other hand is polymorphic.

Polymorphism is: Allows a pointer of a subclass type to be assigned to a pointer of the parent class type. That is, the same operation works on different objects, and can have different interpretations, resulting in different execution results. At run time, you can invoke methods in the derived class by pointing to a pointer to the base class.

Before understanding polymorphism, we must first understand the object-oriented Richter substitution principle and the open closure principle.

Richter Replacement principle (Liskov Substitution Principle):

A derived class (subclass) object can be used to replace its base class (superclass) object. The popular point of understanding is that "subclass is the parent class", when you need a parent class type Object can give a subclass type of object, when a subclass type object is required to give a parent class type object is not possible!

Open closure principle (open Closed Principle):

Package changes, reduce coupling, software entities should be extensible, and non-modifiable. In other words, the extension is open, and the modification is closed. Therefore, the open closure principle is mainly embodied in two aspects: open to expansion, meaning that there are new requirements or changes, the existing code can be extended to adapt to the new situation. Enclosing a modification means that once the class is designed, it can do its work independently, rather than making any modifications to the class.

Let's take a look at how to achieve polymorphism with virtual methods:

Create the base class bird as follows, adding a virtual method eat ():


The subclass Magpie is created as follows, inheriting the parent class bird, overriding the virtual method eat () in the parent class bird:


Create a subclass eagle as follows, inheriting the parent class bird, overriding the virtual method eat () in the parent class bird:


To this, a base class, two subclasses have been created, and then we see in the main function how polymorphism is manifested:


This will show all the corresponding content.

In addition to the virtual method to achieve polymorphism, there are two other methods, namely the use of abstraction or interface to achieve polymorphism, here is not introduced, we can refer to the relevant data to learn the two ways to achieve polymorphism.

About C # Object-oriented programming we'll introduce you to this, and the next blog will introduce C # strings and regular expressions.

C # Getting Started share (vi)--encapsulation, inheritance and polymorphism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.