The star of the Rainbow 2022-08-06 16:27:17 阅读数:788
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.
Encapsulation, inheritance and polymorphism are the three major characteristics of object-oriented programming.
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.
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.
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_).
means that the same method of the base class has different functions in different derived classes.
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.
copyright：author[The star of the Rainbow]，Please bring the original link to reprint, thank you. https://en.javamana.com/2022/218/202208061610371558.html