C # Object-Oriented - Introduction

  A, C # provide full support for object-oriented programming (Object Oriented Programming); the type of the class description of the object, and the object is a specific instance of a class, the creation of objects is also referred instantiated (Instantiation); commonly use the new operator to create an object:

new MyType();

  Wherein ※ () actually represents is an example of class calls the default constructor to construct class;

  ※ C # also provides a light-weight version of the class, called structure, when the need to create a large number of objects without wishing to take up too much memory, you can use the structure;

  ※ Generally, a class or the behavior of more complex data structures frequent changes in value after the object is created, a structure simpler or fewer acts modify the data structure after the object is created value;

  1. Definitions (Define) a named class or struct MyType, then MyType is the name of the type of declaration (Declare) MyType type of variable myObj, initialization (Initialize) myObj variable to the newly created object or other and the same type MyType the variable is not empty, then the variable myObj points to an object of type MyType:

MyType myObj = new MyType();

  Two, three characteristics of object-oriented programming: encapsulation, inheritance, polymorphism, wherein the three classes complete support feature, structure, only supports encapsulation, inheritance and polymorphism does not support:

  1. Packaging, is packaged into the objective things abstract class, the class may only allow trusted data and behavior of a class or object operating on untrusted hide; package is one object-oriented features, objects and the class the main characteristics of the concept; Briefly, a class is a logical entity that encapsulates data and the operation data behavior, and the behavior of certain data may be private and can not be accessed outside world, in this way, internal data objects behavior and provide different levels of protection to prevent accidental changes part of the program irrelevant or incorrect use of the private portion of the object;

  2. inheritance, can use all the features of existing classes, and rewrite the ability to expand on these functions in the case of the original class do not need to; through inheritance to create a class called subclass or derived class, inherited class or a base class is called the parent class; inheritance process is a process from general to specific; to inheritance, can be implemented by inheritance and composition (composition); implementation of the concept of inheritance in two ways: directly refers to inheritance ability of the base class without additional coding; interface inheritance is the name of the only method, but subclasses must provide the ability to achieve;

  3. polymorphism, is to allow the assignment technique derived object to its base class variable, after the assignment, the base class variables can operate in different ways according to the type of the currently assigned to its derived class object; simply, is allows the assignment of a pointer to the derived class type base type pointer;
  three, five basic object-oriented design principles: the SOLID
  1. single responsibility principle (SRP, single responsibility principle): a class should have one and only one to change the grounds, which means that a class should have only one job;
  2. open closed principle (OCP, open-close principle) : the object should be open for extension, closed for modification, which means that a class should not need to modify the class itself but easy expansion;
  3. Richter substitution principle (LSP, Liskov Substitution principle): when x is in the object type T Q (x) holds, if S is T derived class, the type of the object S when Q y (y) should be established, i.e., to call the base class also applies to derived class, a base class for each instance should be replaced by a derived class instance thereof;
  4. Interface separation principles (ISP, I nterface Segregation Principle): The client should not be forced to achieve a its interface do not have access, or that the client should not be forced to rely on a method that they are not used;
  5. Dependency Inversion Principle (DIP, Dependency Inversion Principle): Object must rely on the abstract rather than concrete realization, it represents a high-level modules should not depend on low-level modules, which should depend on abstractions;

 


If you feel that you have read this article to help, please click the "recommend" button, your recognition is the greatest power of my writing!

Author: Minotauros
Source: https://www.cnblogs.com/minotauros/

This article belongs to the author and blog Park total, welcome to reprint, but without the author's consent declared by this section must be retained, and given the original connection in the apparent position of the article page, otherwise the right to pursue legal responsibilities.

Guess you like

Origin www.cnblogs.com/minotauros/p/11432009.html