19_python foundation - object oriented - class structure, class attributes and class methods, static methods

target

  • Three characteristics of object oriented
  • Class properties and instance properties
  • Class methods and static methods

1, Three characteristics of object oriented

  • encapsulation
    • The operation of writing properties and methods into a class is encapsulation
    • Encapsulation can add private permissions to properties and methods
  • Inheritance (realize code reuse, and the same code does not need to be written repeatedly)
    • The subclass inherits all the properties and methods of the parent class by default
    • Subclasses can override parent class properties and methods
  • polymorphic
    • Different subclass objects call the same parent method to produce different execution results
    • Different objects are passed in to produce different results

2, Class structure

2.1 terminology - Examples

  1. Using face object development, the first step is to design classes
  2. Create an object using the class name (). There are two steps to create an object:
      1. Allocate space for objects in memory
      1. Call initialization method__ init__ Initialize for object

3. After the object is created, there is a real object instance in memory

Therefore, usually:

  1. The created object is called an instance of a class
  2. The act of creating an object is called instantiation
  3. The properties of an object are called instance properties
  4. The method called by an object is called an instance method

During program execution:

  1. Objects have their own instance properties
  2. To call an object method, you can use self
    • Access your own properties
    • Call your own method

conclusion

  • Each object has its own independent memory space to store its own different properties
  • There is only one method of multiple objects in memory. When calling a method, you need to pass the reference of the object to the method

2.2 class is a special object

Everything in Python is an object:

  • class A: the defined class belongs to class object
  • obj = A() is an instance object
  • When the program runs, the class will also be loaded into memory

  • In Python, a class is a special object -- a class object

  • When the program runs, there is only one class object in memory. Many object instances can be created with one class

  • In addition to encapsulating the properties and methods of instances, class objects can also have their own properties and methods

    • Class properties
    • Class method
  • You can access the properties of the class or call the methods of the class through the class name

3, Class properties and instance properties

3.1 class attribute (two access methods)

  • A class attribute is an attribute defined in a class object
  • Usually used to record features related to this class
  • Class properties are not used to record the characteristics of specific objects

3.1.1 setting and accessing class properties

1) Set class properties

  • Class attribute is the attribute owned by the class object, which is shared by all instance objects of the class.
attribute = value

2) Access class properties

  • Class properties can be accessed using class objects or instance objects.

    • Class name.Class properties
      
    • object.Class properties (not recommended)
      

be careful

  • If the object. Class attribute = value assignment statement is used, only one attribute will be added to the object without affecting the value of the class attribute
# 1. Define class and define class attributes
class Dog(object):
    tooth = 10   # Set class properties

# 2. Create object
wangcai = Dog()
xiaohei = Dog()

# 3. Access class properties (two methods): class and object
print(Dog.tooth)  # 10
print(wangcai.tooth)  # 10
print(xiaohei.tooth)  # 10

Advantages of class properties

  • Class properties are defined when a recorded item of data is always consistent.
  • Instance properties require each object to open up a separate memory space for recording data, while class properties are shared by the whole class, occupying only one memory, which saves more memory space.

3.1.2 modify class attribute (a modification method)

Class properties can only be modified through class objects, not instance objects. If class properties are modified through instance objects, it means that an instance property is created.

  • Modifying class properties outside a class
Class name.Class properties = value     # Modify class properties
class Dog(object):
    tooth = 10

wangcai = Dog()
xiaohei = Dog()

# Modify class properties
Dog.tooth = 12
print(Dog.tooth)  # 12
print(wangcai.tooth)  # 12
print(xiaohei.tooth)  # 12

# You cannot modify properties through objects. If you do, you will actually create an instance property
wangcai.tooth = 20
print(Dog.tooth)  # 12
print(wangcai.tooth)  # 20
print(xiaohei.tooth)  # 12

3.2 instance properties

Instance attribute: describes the characteristics of things.

class Dog(object):
    def __init__(self):
        self.age = 5

    def info_print(self):
        print(self.age)


wangcai = Dog()
print(wangcai.age)  # 5
# print(Dog.age)  # Error: instance property cannot be accessed through class
wangcai.info_print()  # 5

4, Class methods and static methods

4.1 class method (two access modes)

4.1.1 class method syntax

  • A class method is a method defined for a class object
    • Within a class method, you can directly access class properties or call other class methods

The syntax is as follows

@classmethod
def Class method name(cls):
    pass
  • Class methods need to be identified by the modifier @ classmethod to tell the interpreter that this is a class method
  • The first parameter of a class method should be cls
    • The cls in the method is the reference of which class is called
    • This parameter is similar to the first parameter of the instance method, which is self
    • You can also use other names when prompted, but you are used to cls

4.1.2 characteristics of class method

  • The decorator @ classmethod needs to be used to identify it as a class method. For a class method, the first parameter must be a class object. Generally, cls is used as the first parameter.

  • Inside the method

    • You can access the properties of a class through cls
    • You can also call other class methods through cls

4.1.3 usage scenarios of class methods

  • When class objects need to be used in methods (such as accessing private class properties, etc.), class methods are defined

  • Class methods are generally used in conjunction with class properties

  • Call class methods (two types)

    • Class name.Class method()   # No cls parameters need to be passed
      
    • Object name.Class method()
      
# 1. Define class: private class attribute. Class method obtains this private class attribute
class Dog(object):
    __tooth = 10  # Define private class properties

    # Define class methods
    @classmethod
    def get_tooth(cls):  # cls refers to the Dog class
        return cls.__tooth

# 2. Create objects and call class methods
wangcai = Dog()
result = wangcai.get_tooth()
print(result)  # 10

result = Dog.get_tooth()
print(result)  # 10 

4.2 static method (two access modes)

4.2.1 static method syntax

  • During development, if you need to encapsulate a method in a class, this method:
    • There is no need to access instance properties or call instance methods
    • There is no need to access class properties or call class methods
  • At this time, you can encapsulate this method into a static method

The syntax is as follows

@staticmethod
def Static method name():
    pass
  • The static method needs to be identified by the modifier @ staticmethod to tell the interpreter that this is a static method
  • Call static methods by class name

4.2.2 characteristics of static method

  • It needs to be decorated through the decorator @ staticmethod. Static methods do not need to pass class objects or instance objects (formal parameters do not have self/cls).

  • Static methods can also be accessed through instance objects and class objects.

    • Class name.Static method name()
      
    • Object name.Static method name()
      

4.2.3 usage scenario of static method

  • Static methods are defined when neither instance objects (such as instance objects and instance properties) nor class objects (such as class properties, class methods, creating instances, etc.) need to be used in methods
  • Canceling unnecessary parameter passing is helpful to reduce unnecessary memory occupation and performance consumption
# 1. Define classes and static methods
class Dog(object):
    @staticmethod
    def info_print():
        print('This is a static method')


# 2. Create object
wangcai = Dog()

# 3. Calling static methods: classes and objects
wangcai.info_print()   # This is a static method
Dog.info_print()       # This is a static method

5, Summary

  • Three characteristics of object oriented
    • encapsulation
    • inherit
    • polymorphic
  • Class properties
    • Attributes belonging to class objects, which are common to all objects
  • Instance properties
  • Class method
@classmethod
def xx():
  code
  • Static method
@staticmethod
def xx():
  code

Keywords: Python C# Pycharm Visual Studio Code

Added by dardsemail on Sun, 12 Sep 2021 05:39:33 +0300