- __str__ method
If in development, want to use the print output object variable, the ability to print customized content, you can use the built-in method of __str__
Note: __ str__ method must return a string
class Cat (): DEF the __init__ (Self, name): the self.name = name Print ( ' cat come% s ' % the self.name) DEF __str__ (Self): return ' % s love fish ' % Self. name # create objects, pass in the parameter A = Cat ( ' Tom ' ) Print (A)
result:
Tom the cat came
Tom love fish
- Private property --- package
Sometimes a property or method of the object that we want to be used only inside the object and do not want to be accessed externally
Defined way
When you define a property or method to increase before the property name or method name two underscores __, in fact, during development, private property is not static, so give private property can provide external operating methods
By custom get, set methods provide access to private property
class the Person (): DEF __init__ (Self, name, Age): self.name = name . Self __age = Age # definition of private property get method to acquire private property DEF getAge (Self): return Self. __age # definition of private the method set reassignment property, private property reset DEF the setAge (Self, Age): . Self __age = Age PERSON1 = the Person ( ' Tom ' ,. 19 ) person1.setAge ( 20 is ) Print (person1.name, person1.getAge ())
result
Tom 20
You can also use the property offers private label attribute access
- Class properties, class methods, static methods
Object class is created: the class object: instance of an object class name
Class is the class object properties owned property, which is shared by all the class of the object instance of the object, there is only one copy in memory. For the class of public property, can be accessed through the class object and instance objects outside the class
class people (): name = ' Tom ' __age =. 19 P = people Print (p.name) # instance object Print (people.name) # class object # error, can not be accessed outside the class private class attributes # Print (P Age .__) # Print (people .__ Age)
result
tom
tom
If the modified class properties outside the class, have to go through the class object is then modified, if a reference to an object by way of example, will produce an instance property of the same name, this modified embodiment is modified instance attribute, and the attribute will not affect the class If you reference the name of the property by way of example object instance attributes will force shield the class attribute that references an instance property, unless the property delete instance
Class Methods
Method variables have the class object, decorator @classmethod need to identify which type of process for a class method, the first parameter must be a class object, generally cls as the first parameter (other names may be used as the first argument), it is possible to access the object and the class object by way of example
#类方法 class people(): country = 'country' @classmethod def getCountry(cls): return cls.country p = people() print(p.getCountry()) print(people.getCountry())
result
country
country
Static method
Need to be modified by modifying an @staticmethod, the static method does not require custom parameter
#静态方法 class people(): China = 'China' @staticmethod def getCountry(): return people.China p = people() print(p.getCountry()) print(people.getCountry())
result
China
China