Object-oriented programming

  • 1.1 Overview of Object Orientation
  • 1.2 Three basic features of object orientation
  • 1.3 Classes and Objects
    • 1.3.1 defining class
  • 1.4 encapsulation
  • 1.5 inheritance
  • 1.6 polymorphism

1.1 Overview of Object Orientation

Everything is an object, too abstract for a zero-based person. So far in my narrowest sense, in Python, something as simple as the number 1, or as long as it has a name, like a variable you’re creating, like a = 1, you can see that it has content (properties, methods) by dir(1),dir(a)

1.2 Three basic features of object orientation

Encapsulation, inheritance and polymorphism, as long as the basic course will talk about, I believe you have heard spit

1.3 Classes and Objects

1.3.1 Defining a Class Creates a class like this

Class Name of the class ([parent class]): body of the classCopy the code

Classes have properties, methods and member variables

Methods have instance methods, class methods, and static methods but I don’t know how many times I’ve looked at them, but I’m still a little confused. Instance methods are easy to understand. Class methods are a lot like static methods, and the authors don’t say when to use them. The emphasis is on these lines:

Class methods, methods that belong to classes, methods that don’t belong to instances static methods, don’t bind to instances and classes, don’t specify self and CLS but that seems beside the point. It doesn’t matter if you don’t understand it now. Constructor, also called magic method, initialization. Such as __init__, STR, and two underscores. For the moment, that means giving an instance an initialized property

1.4 encapsulation

Private methods and private variables, which are functions or variables that are inside a class, are preceded by two underscores, so they cannot be accessed outside the class and can only be called inside the class

1.5 inheritance

To give you a direct example:

class God(): name = 'Dog' class Dog(God): Age = 999 p = Dog() print(p.name) => Dog ========result======== DogCopy the code

1.6 polymorphism

That is, when a subclass has the same method name as its parent class, it uses its own method.

This article is from the SDK community: www.sdk.cn