Summary of the basic concepts of object-oriented programming

Time:2020-4-6

1、 Classes and instances

Object is a self-contained entity, marked by a group of recognizable characteristics and behavior classes. Object-oriented programming is to write classes for objects, which is an abstract collection with the same properties and functions.

Be careful:

  1. Class names should be capitalized, and multiple words should be capitalized;
  2. Public modifiers are required for public methods

Instance is a real object. Instantiation is the process of creating an object. Use the new keyword to create it.

2、 Construction method

The constructor is also called the constructor. In fact, it initializes the class. The constructor has the same name as the class, no return value, no void, and is called at new time.

All classes have constructor. If you don’t code, the system will generate empty constructor by default. If you have defined constructor, the default constructor will be invalid

Example:

3、 Method overload

The overload of methods provides the ability to create multiple methods with the same name, but these methods need to use different legal entity parameter types.

Note: when the method is overloaded, the two methods must have the same method name, but the parameter name and the number of parameters must be different.

The advantage of overloading: new functions can be added without changing the original method.

4、 Properties and modifiers

Property is a method or a pair of methods, but when calling his code, he is a field, that is, the property is suitable for the way of using method call.

The field is the data that the storage class needs to meet the needs of more than 70 years, and the field is the variable related to the class.

Example:

The difference between public and private:

They are all modifiers, and public means that the class members it modifies can be accessed by any other class. Private only allows members in the same class to access. Other classes, including their subclasses, cannot access it.

The meaning of the get and set methods of the property:

The properties are: two methods get and set. The get accessor returns the same data type as the declared property, which means that the value or reference of the internal field can be obtained when calling; the set accessor does not display the setting parameter, but it has an implicit parameter, which is indicated by the keyword value. Its function is to assign a value to the internal field or reference when calling the property.

Five, encapsulation

Each object contains all the information it needs to operate. This feature is called encapsulation, so objects do not have to rely on other objects to complete their own operations.

Benefits of encapsulation:

  1. Good packaging to reduce coupling
  2. The internal implementation of a class can be freely modified
  3. Class has a clear external interface

Six, inheritance

The inheritance of objects represents a kind of is-a relationship. If two objects, a and B, can be described as B is a, then B can inherit a. the inheritor can be understood as the specialization of the inheritee, because he has not only the characteristics of the inheritee, but also his own unique personality. Inheritance defines how classes are related and shared. Inheritance works by defining the parent class and subclass, or base class and derived class. The class inherits all the characteristics of the parent class. The subclass not only inherits all the characteristics of the parent class, but also defines new characteristics by itself.

There are three points for attention in learning to inherit:

If the child class inherits the parent class:

  1. The subclass has the properties and functions of the non private parent class
  2. The subclass has its own properties and functions, that is, the subclass can extend the properties and functions that the parent does not have;
  3. Subclasses can also implement the functions of their parents in their own way (method rewriting)

Protected means that a subclass can have full access to the parent class when inheriting

The members that a subclass inherits from its parent are methods, fields, properties, events, indexers. But for the construction method, it cannot be inherited and can only be called. For the member calling the parent class, the base keyword can be used.

If you don’t need inheritance, if you want to modify the function, you have to modify it in all the repeated methods. The more code, the greater the possibility of error. The advantage of inheritance is that inheritance makes all the common parts of the subclass in the parent class, so that the code can be shared, which avoids duplication. In addition, inheritance makes it easier to modify or extend the extension 。

Disadvantages of inheritance: the parent class changes, and the child class has to change.

Inheritance breaks the wrapper, and the implementation details of the parent class are exposed to the child class

Seven. Polymorphism

Polymorphism means that different objects can perform the same action, but through their own code classes

  1. Children appear as parents
  2. Subclasses work in their own way
  3. When a subclass appears as a parent, its specific properties and methods cannot be used

Virtual methods and rewriting

In order for the instance of the subclass to completely replace the class member from the parent class, the parent class must declare the member as false. This is achieved by adding the virtual keyword before changing the return value type of the member. The subclass can use the override keyword to implement the parent class instead of its own, which is the override of the method.

The principle of polymorphism is that when a method is called, no matter whether the object is converted to its parent class or not, only the method implementation at the end of the inheritance chain will be called, that is to say, the virtual method is dynamically bound and called according to its runtime type rather than compile time type.

Eight, reconstruction

C allows classes and methods to be declared abstract, that is, abstract methods

Be careful:

  1. Abstract class cannot be instantiated
  2. Abstract methods are methods that must be overridden by subclasses
  3. If an abstract method is included in a class, the class must be defined as an abstract class, whether or not other general methods are included
  4. We should consider that abstract classes have as much common code as possible and as little data as possible.

Abstract class usually represents an abstract concept. It provides a trigger point for inheritance. When designing a new abstract class, it must be used for inheritance. Therefore, in a hierarchical structure formed by inheritance relationship, leaf nodes should be concrete classes and branch nodes should be abstract classes.

Nine, interface

Keyword interface

An interface is a collection of implicit public methods and attributes to encapsulate specific functions. Once an interface is implemented, a class can support all the attributes and members specified by the interface. Declaring an interface is exactly the same as declaring an abstract class, but it is not allowed to provide an execution mode that any member of the interface cannot. To implement an interface class, all the methods and attributes of the interface must be implemented 。

A class can support multiple interfaces, and multiple classes can support the same interface. The name of the interface should be preceded by the capital letter “I”

Be careful:

The interface is declared with interface instead of class. I should be added before the interface name. The methods or properties of the interface cannot have modifiers or methods without method bodies
The difference between abstract classes and interfaces:

  1. Abstract class can give the realization of some members, but the interface does not contain the realization of member body. Abstract members of abstract class can be partially implemented by subclass, and the members of interface must be fully implemented by implementation class. A class can only inherit one abstract class, but can realize multiple excuses
  2. Class is the abstraction of object, abstract class is the abstraction of class, and interface is the abstraction of behavior
  3. If the behavior spans objects of different classes, the interface can be used; for some similar class objects, the inheritance abstract class can be used.

Reference self talk data structure

Object oriented design principles:

  1. The single responsibility principle (SRP for short): for a class, it is better to do only one thing, and only one causes its changes
  2. The open close principle (OCP): it is open for extension and closed for change
  3. The Liskov Substitution Principle (LSP): a subclass must be able to replace its base class
  4. The dependency inversion principle (DIP): depends on abstraction
  5. The Interface Segregation Principle (ISP for short): use multiple small specialized interfaces instead of one large total interface.

summary

The above is the whole content of this article. I hope that the content of this article has some reference learning value for your study or work. Thank you for your support for developepaer. If you want to know more about it, please check the relevant links below

Recommended Today

This open source AI project can “tattoo with one click”. AI: do I want to lose face?

From: the heart of machine Sometimes, we need to cover some people’s tattoos to avoid being imitated. Sometimes people just simply wonder what some big stars would look like without tattoos. Vijish madhavan, a machine learning researcher from India, recently developed an open source machine learning tool skinddeep to meet our needs. Netizens also use […]