How to create a class:
class 类名: pass
eg
class Bar: # 定义一个类 def foo(self, arg): # self 永远是调用方法的对象,即中间人 print(self, self.name, self.age, arg)obj = Bar() # 使用 Bar 类,创建一个对象 objprint(obj)obj.name = ‘klvchen‘obj.age = 28obj.foo(666)运行结果:<__main__.Bar object at 0x000001AB792D23C8><__main__.Bar object at 0x000001AB792D23C8> klvchen 28 666
Create a method
构造方法: __init__(self, arg) obj = 类(参数)普通方法:obj = 类(参数) obj.普通方法名()
Calling methods in a class
class Foo: def bar(self): print(‘bar‘)obj = Foo() # 方法一,常用obj.bar()obj = Foo() # 方法二Foo.bar(obj)运行结果:barbar
Fields in the class
The classes are divided into normal fields and static fields:
Normal field: Persisted object, execute only through object access
Static fields: persisted in the class, execution can be accessed through the object, or accessed through the class
eg
class Province: country = ‘China‘ # 静态字段 def __init__(self, name): self.name = name # 普通字段print(Province.country)运行结果:China
Three methods in the class:
1. Common method: Persisted in the class, called by the object, self is the object
2. Static method: Guaranteed to exist in class, called directly by class
3. Class method: Persisted in the class, called directly by the class, the CLS is the current class
Application Scenarios:
If the object needs to maintain some value, when performing a function, you need to use the value in the object, select普通方法
You do not need to select a value in any object静态方法
Note: The class saves only one copy in memory, which saves system memory
eg
class Foo: def bar(self): print(‘bar‘) @staticmethod # 静态方法 def sta(): print(‘static method‘) @staticmethod # 静态方法,带参数 def sta2(n): print(n)Foo.sta()Foo.sta2(666)运行结果:static method666
class Foo: @classmethod def classmod(cls): # 类方法, cls 是当前类 Foo print(cls) print(‘classmethod‘)Foo.classmod()运行结果:<class ‘__main__.Foo‘>classmethod
One of the three main features of object-oriented: encapsulation
eg
class Person: def __init__(self, name, age): self.name = name self.age = age def show(self): print(‘%s %d‘ %(self.name, self.age))klvchen = Person(‘陈‘, 28) # 生成对象时会自动执行 __init__() 方法klvchen.show()运行结果:陈 28
Two main characteristics of object-oriented: inheritance
To extract a method common to multiple classes into a parent class, the subclass inherits only the parent class without having to implement each method one at a A.
class Father: passclass Son(Father): pass
eg
class F: def f1(self): print(‘F.f1‘) def f2(self): print(‘F.f2‘)class S(F): def s1(self): print(‘S.s1‘) def f2(self): super(S, self).f2() # 执行父类的 f2 方法 F.f2(self) # 执行父类的 f2 方法obj = S()obj.f1()obj.s1()obj.f2()运行结果:F.f1S.s1F.f2F.f2
Python supports multiple inheritance with the following rules:
1. Left priority
2. One way to do the last
3. At the same root, the root is finally executed
eg
class F1: def f(self): print(‘F1.f‘)class F2: def f(self): print(‘F2.f‘)class S(F1, F2): passobj = S()obj.f()运行结果:F1.f
Three main characteristics of object-oriented: polymorphism
Python native support polymorphism
When you add a @property, you can create an illusion that the field is exactly the same when you access the property
class Foo: def bar(self): print(‘bar‘) @property def per(self): print(‘123‘) return 1 @per.setter def per(self, val): print(val) @per.deleter def per(self): print(888)obj = Foo()r = obj.per#print(r)obj.per = 666del obj.per运行结果:123
class Foo: def f1(self): return 123 def f2(self, val): print(val) def f3(self): print(‘del‘) per = property(fget=f1, fset=f2, fdel=f3)obj = Foo()ret = obj.perprint(ret)obj.per = 6666del obj.per运行结果:1236666del
Call the __call__ method directly after the object Plus ()
class Foo: def __init__(self): print(‘init‘) def __call__(self, *args, **kwargs): print(‘call‘)obj = Foo()obj()运行结果:initcall
__dict__ gets all the members of the object, returned as a dictionary
class Foo: def __init__(self, name, age): self.name = name self.age = ageobj = Foo(‘klvchen‘, 28)d = obj.__dict__print(d)运行结果:{‘name‘: ‘klvchen‘, ‘age‘: 28}
Introduction to Python Classes