Learn Python's basic knowledge of classes from the old

  • 2020-04-02 14:14:41
  • OfStack

In the beginning, please read the following several dry terms very patiently. This is not in the style of this tutorial, but please understand that you must read the dry things in the future. These dry explanations are from wikipedia.

1. Problem space

Problem space is the state of knowledge that the problem solver has reached about a problem. It is formed by the problem solver using the information contained in the problem and the information stored.

A problem is generally defined in the following three aspects:
  The & # 8226; Initial state - incomplete information or unsatisfactory condition at the outset;
  The & # 8226; Target state - the information or state you wish to obtain;
  The & # 8226; Action - the steps you may take to move from the initial state to the target state.
 
Together, these three parts define the problem space.

2, objects,

Object is a term in object Oriented language, which not only refers to a specific thing in the Namespace of the objective world, but also represents the basic element in the solution space of the software system.

In a software system, objects have unique identifiers, including Properties and Methods. Properties are the information that needs to be remembered, and Methods are the services that objects can provide. In Object Oriented software, an Object is an Instance of a Class.

3. Object orientation

Object-oriented programming (OOP) is a programming paradigm and a method of program development. An object is an instance of a class. It takes the object as the basic unit of the program, encapsulates the program and data in order to improve the reusability, flexibility and extensibility of the software.

Object-oriented programming can be thought of as the idea of having separate, calling objects in a program, as opposed to the traditional idea of thinking of a program as a collection of functions, or simply as a set of instructions to a computer. Each object in object-oriented programming should be able to accept data, process it, and communicate it to other objects, so they can all be considered a small "machine," or object.

It has been proven that object-oriented programming promotes the flexibility and maintainability of programs and is widely used in large-scale project design. In addition, proponents claim that object-oriented programming is easier to learn than previous approaches because it makes it easier for people to design and maintain programs, making them easier to analyze, design, and understand. Opponents deny this in some areas.

When we talk about object orientation, it's not just a method of programming. It is more of a way of program development. In this regard, we must learn more about object-oriented system analysis and Object Oriented Design (OOD).

Here's another quote from the history of OOP in wikipedia.


The prototype of object-oriented programming, as early as in 1960 Years of Simula As you can see from the language, the programming field was facing a crisis: how can software be well maintained when the software and hardware environment is becoming more and more complex? Object-oriented programming solves this problem in part by emphasizing repeatability. 20 century 70 s Smalltalk Languages are classically object-oriented -- so much so 30 Today, the language is still considered the basis of object-oriented languages.
The earliest origins of the concept of objects and instances in computer science can be traced back to the Massachusetts institute of technology PDP-1 System. This system is probably the earliest volume-based architecture ( capability based architecture ). In addition 1963 years Ivan Sutherland the Sketchpad The same idea is behind the application. Objects were first used as programming entities 1960 S by Simula 67 Language introduces thought. Simula The language is ollie - Designed by John dahl and kristin nejart for the simulation environment at the computer center in Oslo, Norway. (they are said to have designed the language to simulate ships and are interested in how properties of different ships interact. They classify different ships into different classes, and each object, based on its class, can define its own properties and behavior. This approach is the earliest conceptual representation of analytical procedures. In an analytic program, we map real-world objects to abstract objects, which is called "simulation." Simula Not only was the concept of "classes" introduced, but the idea of instances was also applied -- perhaps the earliest application of these concepts. 20 century 70 S xerox PARC Institute invented Smalltalk The language defines the concept of object-oriented programming as the widespread use of objects and messages in basic operations. Smalltalk The founder of Simula 67 The main idea of the impact, however Smalltalk The objects in are completely dynamic -- they can be created, modified, and destroyed, as well Simula The static objects in. In addition, Smalltalk It also introduces the idea of inheritance, thus transcending the programming model of non-instance creation and non-inheritance Simula . In addition, Simula 67 The idea is also applied in many different languages, such as Lisp , Pascal . Object-oriented programming in 80 The era became a dominant ideology, largely thanks to it C++ - C An expanded version of the language. In the graphical user interface ( GUI ) object-oriented programming has adapted well to the rising tide. GUI And object - oriented programming is closely related in Mac OS X You can see that. Mac OS X Is made up of Objective-C The language is written, the language is a copy Smalltalk the C Language expansion edition. The idea of object-oriented programming also makes event-handling programming more widely used (although the concept is not confined to object-oriented programming). One way to say it is, GUI Has greatly promoted the development of object-oriented programming. Niklaus wert and his colleagues at the federal institute of technology in Zurich studied abstract data and modular programming. Modula-2 All of these are included, and Oberon A special object-oriented approach is included - unlike Smalltalk with C++ . Object-oriented features were added to the popular languages of the time: Ada , BASIC , Lisp , Fortran , Pascal And all that. Because these languages did not originally have an object-oriented design, this mashup often leads to compatibility and maintainability problems. In contrast, "pure" object-oriented languages lack some of the features that programmers need to survive. In this context, the development of new languages has become a priority. As a pioneer, Eiffel It successfully solved these problems and became a popular language at that time. In the last few years, Java Language became the most widely used language except that it was associated with C and C++ Grammatical approximation. Java Its portability is an indelible part of its success because it has attracted the input of a large number of programmers. In the recent development of computer languages, a number of languages have emerged that support both object-oriented and procedural programming. The best of them are Python , Ruby And so on. Just as procedural programming has advanced the techniques of structured programming, modern object-oriented programming methods have enabled the use of design patterns, design by contract, and modeling languages (e.g UML ) technology has also improved.

By the time you read this sentence, I'll assume that you've got a vague idea of object orientation. So, what does a class have to do with OOP?

Wikipedia defines classes this way:


In object-oriented programming, classes ( class ) is a construction of an object-oriented computer programming language. It is a blueprint for creating objects, describing the common properties and methods of the objects created.
 
A more rigorous definition of a class is a cohesive package made up of some particular metadata. It describes the behavior of objects that are called instances of that class. Classes have interfaces and structures. Interfaces describe how methods interoperate with classes and their instances, while structures describe how data in an instance is divided into properties. A class is the most specific type of object associated with a layer. Classes can also have runtime representations (meta-objects) that provide runtime support for manipulating the metadata associated with the class.

Programming languages that support classes vary somewhat in their support for the various features associated with classes. Most support different forms of class inheritance. Many languages also support features that provide encapsulation, such as access modifiers. Class provides a means to implement the three most important features of object-oriented programming (encapsulation, inheritance, polymorphism).

At this point, the reader may realize that OOP programming requires classes. You can say that, but not strictly. However, the opposite cannot be said. This is not to say that just because a class is used, it must be OOP.

Writing class

To understand the class, you need to have some abstract thinking. Because the Class itself defines an abstract feature of something. For example, define a class:


class Human:        # This is the way to define a class, which is usually named with capitalized words or word mosaics
    pass

  Okay, so let's start here and write a class, but instead of python, we'll use pseudo-code, which is, of course, a far cry from python. As follows:


class Human:
    The limbs
    character
    hobby
    learning ()

  An Object is an instance of a class. We've just defined a class named Human that defines all of the Human in the world, but this is an abstract Human, not a specific person. To a specific person, the characteristics of his limbs, his character, his hobbies, etc., are all specific, and these things are called attributes here.

The following is to find a specific person: wang erasako, the above class instantiation.
King's two pockmarks = Human()
Two pockmarks. Limbs = long
Wang erpockmarked. Hobby = to see MM

  In this case, wang erasako is an instance of the class Human. The value of a concrete object property is called its "state". (the system allocates memory space to objects, but not to classes. This is easy to understand. Classes are abstract.

At this point, the officer is probably on the class has a vague understanding of it?

Given the class, it seems a little far from our intuition. So take your time. This is a short lecture, I hope you can understand.


Related articles: