Object-Oriented Programming

The star of the Rainbow 2022-08-06 16:27:17 阅读数:788

object-orientedobjectorientedprogramming

First, the three major characteristics of object-oriented

Overview

The application of object-oriented programming (OOP) technology can facilitate the reuse of code in the follow-up, reduce the total amount of code written, and speed up the development progress; the shortcomings also exist, such as after a certain class is modified, it depends on the class.Code has to be retested, and bugs discovered later in development can affect a lot of code.

In object-oriented programming, a class is the core of its design. It is actually a new data type and a tool for implementing abstract types. A class is a kind of data implemented by abstracting object data types.type.The core idea of ​​OOP is to encapsulate things with common properties and methods under the same class.

Three Features

Encapsulation, inheritance and polymorphism are the three major characteristics of object-oriented programming.

1. Encapsulation

Put common properties and methods under the same class, so that data and operations are integrated, the program structure is more compact, and at the same time, the difficulty of debugging and maintenance caused by data disorder is avoided.
Level 1: Creating a class and an object will create the namespaces of the two respectively. We can only access the names inside by using the class name or object, which is itself a kind of encapsulation.
The second level: Some properties and methods are hidden (defined as private) in the class, only used inside the class and cannot be accessed by the outside, or leave a small number of interfaces (functions) for external access.

2. Inherited

Encapsulate common properties and methods under a parent class, and then multiple subclasses can inherit the properties and methods of the parent class.Of course, you can also add new properties and methods when inheriting.

There are two main ways to realize the concept of inheritance: implementation inheritance and interface inheritance.

  • Implementation inheritance refers to the ability to use the properties and methods of a base class without additional coding;
  • Interface inheritance refers to the ability to use only the names of properties and methods, but the subclass must provide the ability to implement (subclass refactors the father class method);

Inheritance is divided into single inheritance and multiple inheritance (inherited from multiple base classes).If the base class is not specified when inheriting, the python class will inherit the object class by default. Object is the base class of all python classes. It provides the implementation of some common methods (such as _str_).

3. Polymorphism

means that the same method of the base class has different functions in different derived classes.

Second, how to abstract classes

Abstract the general process of a class

When determining which classes you need and the methods and properties contained in them, you can try:

(1) Mark the constructed nouns, verbs, and adjectives.
(2) Find possible classes in nouns.
(3) Find possible ways in the verb.
(4) Find possible attributes in adjectives.
(5) Assign the found methods and properties to each class.

With a sketch of the object-oriented model, consider the relationships between classes and objects (such as inheritance or collaboration) and their responsibilities.To further improve the model, try:

(1) Document (or imagine) a series of use cases, ie, scenarios in which the program is used, and try to ensure that these use cases cover all functionality.
(2) Thoroughly and carefully consider each scenario to ensure that the model includes everything needed.If there is any omission, add it; if there is something wrong, modify it.Keep repeating this process until you are satisfied with the model.

Points to note

  • Put related things together.If a function operates on a global variable, it is best to treat them as properties and methods of a class.
  • Don't get too intimate with each other.A method should only care about the properties of the instance to which it belongs, and let them manage the state of other instances by themselves.
  • Use inheritance with caution, especially multiple inheritance.Inheritance is sometimes useful, but in some cases it can introduce unnecessary complexity.It's hard to use multiple inheritance correctly, and it's even harder to troubleshoot bugs in it.
  • Keep it simple.Keep the method short and compact.In general, you should ensure that most methods can be read and understood within 30 seconds.For the rest of the methods, try to keep them on one page or screen as much as possible.
copyright:author[The star of the Rainbow],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/218/202208061610371558.html