C # OO (elementary idea ).,

Source: Internet
Author: User

C # OO (elementary idea ).,

Inheritance, polymorphism, encapsulation

In C #, In order to reasonably describe the laws of nature, object-oriented programming introduces the concept of inheritance, which is one of the most important concepts in object-oriented programming, defines how to create a new class based on the existing class.

Inheritance:A subclass derived from a class has all the public attributes and methods of the class.

The parent class is a subclass that inherits the parent class.

Syntax-subclass:Parent class

Subclass and parent class

The inherited class is called a parent class or a base class. A class that inherits other classes is called a subclass or a derived class. A subclass not only has its own unique members, but also has members of the parent class.

Is Application

The inheritance relationship between the two categories must conform to the is a relationship (for example, a small truck is a truck, a truck is a car, and a small truck is a car)

Rys replacement principle:Child classes can replace parent classes, but child classes cannot replace Child class objects.

Only when the child class can replace the parent class, the software functions are not affected, and the parent class can be reused. The Child class can also add new behaviors based on the parent class.

Is And AsUse of Operators

The Is operator Is used to check whether the object Is compatible with the specified type.

The As operator is mainly used for type conversion between two objects.

Inherited features

Inheritance is passed (Child classes can be derived from child classes) and a single child class (One Child class cannot inherit multiple parent classes at the same time)

BaseKeyword andProtectedModifier

Base: indicates the parent class, used to call Members in the parent class.

Protected: modifies the parent class members. It only allows subclass calls and internal access to the class, and does not allow access from other non-subclass classes.

Modifier differences:

Public: any class can be accessed.

Private: no class can be accessed.

Protected: It can only be accessed by the subclass of the class itself, but cannot be accessed by any other class.

Subclass constructor:

Any subclass inherits the common no-argument constructor of the parent class. Other constructor of the parent class will not be inherited. Only constructors with the same signature can be declared in the subclass and called with the base display.

The constructor of the Child class will certainly call the constructor of the parent class. First, execute the constructor of the Child class and execute the constructor of the Child class.

Implicitly call the constructor in the parent class:

Subclass (if not specified) calls a default non-argument constructor in the parent class.

Show down constructors in the parent class

When specified by the subclass, use the base keyword to call a constructor of the parent class to initialize the attributes. Then, complete the initialization of the attributes specific to the subclass in the constructor of the subclass. If the subclass does not specify which constructor is manually created in the parent class, the system will call the self-created constructor without parameters in the parent class. If no constructor is created, an error will be reported.

Public SE (string name); = Public SE (string name): base ();

Note: parameters can only be passed when base is used to call the parent class constructor.

Polymorphism

Polymorphism:Two or more objects of different classes make different responses to the same message (method call.

Polymorphism is based on inheritance. Without inheritance, there is no polymorphism.

Implementation of Polymorphism

Take the parent class type as a parameter: It can accept its subclass type. During running, the program automatically determines which subclass the actual parameter belongs to and calls the subclass method to realize polymorphism.

We implement polymorphism Based on inheritance through virtual methods and abstract methods (by rewriting the parent class method, we can complete our own method ).

Virtual method:Rewrite the implementation method. Use the keyword Virtual to define a Virtual method (in the parent class), and Override this Virtual method (in the subclass) with Override modifier ).

Syntax: access modifier virtual return value type method name ()

{

// Method body

}

 

Use virtual methods to achieve polymorphism:

1. Subclass override the virtual method of the parent class

Two methods: 1. Create a parent class variable and use a subclass object to instantiate the parent class variable.

2. The parent class type is used as a parameter, and its subclass object is passed in as a parameter.

2. During the runtime, the method to be executed is determined based on the actually created object

Abstract method: an unimplemented method. These methods can only be defined. You can declare an abstract method (in the parent class) by adding the keyword abstract when defining the method ), override is used to Override this abstract method ).

Syntax: access modifier sbstract return value type method name ();

Note: There is no method body in the abstract method.

Application of abstract methods:Abstract methods must be defined in non-Abstract subclasses. abstract methods must be defined in abstract classes.

 

Abstract class:Classes modified with sbstract are all abstract classes. Abstract classes are abstract concepts. abstract classes provide abstract methods that must inherit their subclasses for implementation. These abstract methods are used to constrain the behavior of subclasses. classes with abstract methods must be abstract classes, abstract classes do not have to have abstract methods.

Abstract classes cannot be instantiated, nor are they sealed or static.

 

Abstract METHODS and virtual methods both implement polymorphism by overwriting the parent class method.

Note: rewrite the syntax of the parent class: Method Name and parameter list. The return value is the same.

(Method Overloading is completed in a class with the same method name. The number of parameters is different from the parameter type and is irrelevant to the return value)

Differences between the two:

Abstract method:

Modify with abstract keywords,

No method body,

Must be overwritten by override,

It can only be written in an abstract class.

 

Virtual method:

Modify the method body with the virtaul keyword

Can be overwritten by override,

Or override rewrite.

Object-oriented Thinking

Encapsulation: ensures the integrity and security of the object's data.

Inheritance: establishes relationships between classes to achieve code reuse and facilitate system expansion.

Polymorphism: different implementation methods can be implemented for the same method call.

Object-oriented advanced applications 

Design Simulation

In software engineering, object dependencies are called coupling, and the relationship between each element in a module is called cohesion. In software engineering, high cohesion and low coupling are favored.

Design Pattern concept:Specific solutions to specific problems that have been tested in practice.

The design pattern is like the ancient plan. The design pattern is people in the software

Summary of some experiences in development.

Object-oriented design pattern is the basis for reusable Object-Oriented Software

Simple factory design model:Create a simple factory and encapsulate the changes. Add modifications to the code execution within the class and return the child Class Object of the parent class by defining a parent class, the subclass of the instance in this method body is created based on the parameters passed by the method.

The simple factory design mode reduces the coupling between classes and greatly reduces the modification of the customer program, greatly enhancing the program expansion performance (so that the customer program does not need to be changed ), however, simple factories also have some limitations.

Singleton design mode:The method that generates the instance is in the class, and its instance is returned through a static method. (Set the constructor to private in the class)

Private constructor:A special constructor usually contains only static members. If only a private constructor in a class does not have a public constructor, other classes cannot access the instances of this class.

Features of the singleton design mode:

1. private constructor.

2. Save the unique static private variable.

3. Obtain the static method of the unique instance.

Application Singleton design mode:The Singleton design mode can be used when only one instance of the class exists and can be accessed globally.

Simple factory model application:The simple factory mode is used in a customer program. You do not need to know which subclass the created object belongs to. The objects to be created need to be changed. Therefore, these changes are encapsulated in the factory.

The design of the software is based on requirements, so the appropriate design mode should be selected as appropriate.

Related Article

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.