python instance attributes and class attributes, class methods, static methods and instance method

Class and instance properties

  • Class properties : in Python we can attribute classes direct access can be divided into public and private class attributes class attributes (like static member variables in Java), direct access to public property, and private property will have to pass from get and set methods defined access (object to call the get and set methods).
  • Examples of attributes : instance of an object (the object created by the class) owned property can not be accessed directly through the class name. Objects can be added directly to the external properties (not recommended, because object properties should be encapsulated inside the class, or prone to a number of implicit error), this property is also known as instance properties. Generally, the method initializes instance attributes are __init()defined internally by the parameters passed and assigning (similar construction method in Java).
  • It should be noted that the object access class properties will not affect the original class attribute, because when an object is instantiated, the memory allocation will give a space object, so the properties of this class is the class object to access the properties themselves of this space (equivalent to a copy), so to change the class attribute directly through the object can not have an impact on the original class.
class people:
   name = "tom"  # 类属性
   __height = 180

   def __init__(self, age):
       self.age = age  # 实例属性
   def getHeight(self):
       return self.__height
   def setHeight(self,height):
       self.__height = height

print("通过类名访问:" + people.name)
p = people(20)
p.name="max"
p.setHeight(190)
print("通过对象访问:%s %d %d"%(p.name,p.age,p.getHeight()))
print("通过类名访问:" + people.name) #p.name="max"并没有影响到原类属性

result:
Here Insert Picture Description

Class methods, static methods and instance method

  • Class methods : Method class object owned, need decorator @classmethodidentified, the first parameter must be a class object, generally calas the first parameter (of course you can not call cal, it's just a habit and norm), able to access through the instance object and the object (the class name). Class method allows an object instance to modify the value of the class attribute (already mentioned above, an object instance of the class attribute value can not be modified directly)
class people:
   name = "tom"  # 类属性
   def __init__(self, age):
       self.age = age  # 实例属性

   @classmethod
   def setName(cls,name):
       cls.name=name

p=people(20)
p.setName("max")
print(people.name)

result:
Here Insert Picture Description

  • Examples of methods : Method instance of an object owned, can not access the class object (not directly by the class name calling). The first argument must be an instance of an object, generally selfas the first parameter (the same token, you can not call self, it's just a habit and norm), subject to call by example.
class people:
   name = "tom"  # 类属性
   def __init__(self, age):
       self.age = age  # 实例属性
   def showAge(self):
       print("年龄:%d"%(self.age))

p=people(20)
p.showAge()

result:
Here Insert Picture Description

  • Static method : decorator need @staticmethod, no need to define the parameter to be used as a normal function.
class people:
   name = "tom"  # 类属性
   def __init__(self, age):
       self.age = age  # 实例属性

   @staticmethod
   def showAge():
       print("姓名:"+people.name)

people.showAge()
p=people(20)
p.showAge()

result:
Here Insert Picture Description

Published 11 original articles · won praise 16 · views 1826

Guess you like

Origin blog.csdn.net/qq_44204959/article/details/105193797