OOP basic knowledge review

OOP : Object-Oriented Programming, as a professional mover, if we hear colleagues say that OOP we still don’t know why, then it’s really a slap in the face.

In particular, this article is just an outline or some personal understanding of some specific content in the process of reviewing the basics of OOP.

【OOP】

Object (example): Everything in time can be called an object, a specific puppy, a big tree, and the actual object we can see or perceive.

Class: On the basis of the object, according to some attributes of the object (name, age, elevation, blood type), behavior (calling, running, singing, jumping) to extract it and use the code in programming to achieve, how do we live The actual objects in the code are transformed into our code, which is operated through these properties, methods, etc. Then we refine these attributes to form a class. We represent the operation of real objects in real life through the operation of the class. (For the refined class, "new" can be understood as operating a specific object)

class:

  • Keyword: Class
  • Field: Private
  • Property: Provide a window for internal and external communication
  • Method: Some actions specific to the subject (eating, screaming, going up the stairs, hitting, etc.)
  • Access restrictions: public (public), private (private), only subclasses (protected) are allowed

Package:

    Advantages: For certain types of things/actions that will be done, the period does not need to be affected by other objects. (To be precise, such as: refining methods, dividing objects into classes, these are all encapsulation)

  • Low coupling
  • Facilitate internal modification
  • Provides a very convenient interface to the outside world

Inheritance: Inheritance is on the basis of classes. Different classes can also be refined into a class according to certain rules. These rules are formed into a class, and the inheritance relationship is changed from small to large. Good manipulation of our objects. For example, the attribute extraction of the dog black and the dragon can extract a "dog" category, the cat Xiaobai and Xiaohuang can extract a "cat" category, and the "dog" category and the "cat" category we refine again In the "mammal category", we put the attributes of the mammals of cats and dogs into the mammal category, so that there is no need to write these additional attributes when creating the cats and dogs. (The essence of inheritance is to inherit a small class from a large class)

  • High coupling (change a class may affect other inherited classes)
  • The subclass can have all the non-private properties/methods of the parent class
  • Subclasses can additionally extend their own unique attributes/methods
  • Subclasses can extend the methods of the parent class (virtual methods, overridden)

Polymorphism: different objects do the same action (method) but the effect is not the same ( dog issue calls Wang , the cat emits sounds meow ).

Virtual method: When most used for inheritance, the subclass extends the content of the parent class (extending the parent class does not refer to the specific properties/methods of the subclass)

Abstract class: A class that is more refined in the process of class encapsulation. If you cannot instantiate a specific object, then you must consider creating this class as an abstract class

  • Cannot be instantiated
  • Method has no entity (no actual content)

Interface: Used to solve the problem that multiple different objects need to operate the same method, but these multiple objects are not convenient to divide into one class or implement this method through class inheritance (high coupling), use the interface

  • The class inherits the interface and must implement the methods in the interface

 

Guess you like

Origin blog.csdn.net/qq_39541254/article/details/102511970