Note: Data is a constant, using full capitalization naming rules
10.12 Category Attributes
Definition and Characteristics of Attributes of Category 10.12.1
class Car(object): age = 18 def __init__(self,mark,color,price,speed): self.mark = mark self.color = color self.price = price self.speed = speed Class attributes and instance attributes Class attributes: The attributes owned by class objects are shared by class objects and instance objects. There is only one copy in memory. If it is a public class attribute, it can be accessed by class objects and instance objects. Class attributes: defined inside a class, outside a method, shared by all objects, occupying one share of memory
Access to Attributes of Class 10.12.2
class Car(object): age = 18 def __init__(self,mark,color,price,speed): self.mark = mark self.color = color self.price = price self.speed = speed car1 = Car(mark,color,price,speed) print('The first method: access through class objects') print(Car.age) print('The second way is through: instance object access') print(car1.age) print('##########################################################################') //Note: 1,Our Class Attribute Classification: Common Class Attributes, Private Class Attributes, Protecting Class Attributes 2,Private class attributes are not accessible outside the class, including instance objects or class objects. 3,Inside a class, instance methods can be accessed through class objects and instance objects.
Modification of 10.12.3 Category Attributes
class Person(object): '''Establish person class,Attributes have name, age, gender, creation method''' country = 'China' @classmethod def changeCountry(cls,newcountry): cls.country = newcountry print('At this time, the nationality after the amendment is:',cls.country) def __init__(self,name,age,gender): self.name = name self.age = age self.gender = gender @staticmethod def shoeStyle(): print('many') print('Class attributes:',Person.country) zhang = Person('wang',18,'man') zhang.country = 'Singapore'#Create new dynamic attributes, instance attributes Person.country = 'South Africa' print('Class attributes for instance calls:',zhang.country) print('Class attributes:',Person.country) Person.changeCountry('Australia') print(Person.country) ming = Person('Xiao Ming',18,'boy') ming.changeCountry('North America') ming.shoeStyle() Person.shoeStyle() //Note: 1,Class attributes are modified by class objects 2,You can't modify it by being an instance object, otherwise you just add a dynamic attribute.( init If there is a field in it, it will be overwritten)
Category 10.13 Methods
Category 10.13.1 Attributes
class Person(object): '''Establish person class,Attributes have name, age, gender, creation method''' country = 'China' @classmethod def changeCountry(cls,newcountry): cls.country = newcountry print('At this time, the nationality after the amendment is:',cls.country) def __init__(self,name,age,gender): self.name = name self.age = age self.gender = gender @staticmethod def shoeStyle(): print('many') print('Class attributes:',Person.country) zhang = Person('wang',18,'man') zhang.country = 'Singapore' Person.country = 'South Africa' print('Class attributes for instance calls:',zhang.country) print('Class attributes:',Person.country) Person.changeCountry('Australia') print(Person.country) ming = Person('Xiao Ming',18,'boy') ming.changeCountry('North America') ming.shoeStyle() Person.shoeStyle() //Note: 1,Format of Class Method @classmethod def Method name( cls): pass 2,Class properties can be modified 3,It can be accessed either by class objects or by instance objects.