- 类的组成
- 类属性
- 实例方法
- 静态方法
- 类方法
-
对象的创建及使用
-
调用类属性,类方法,静态方法
-
动态绑定属性
#类,对象的创建
class Person():
#类属性(类中方法外的变量,被该类所有对象共享)
native_place="zhejiang"
#初始化方法
def __init__(self,name,age):
self.name=name #self.name为实例属性,进行了一个赋值操作,将局部变量name值赋给实例属性
self.age=age
#实例方法
def run(self):
print("running")
#静态方法
@staticmethod
def sm():
print("静态类方法")
#类方法
@classmethod
def cm(cls):
print("类方法")
#对象的创建及使用
per1=Person("per1",18)
print(per1.name)
print(per1.age)
per1.run()#对象名.方法名
Person.run(per1)#类名.方法名(类的对象)-->self
#调用类属性,类方法,静态方法
print(Person.native_place)
Person.cm()
Person.sm()
#动态绑定属性
per2=Person("per2",19)
per2.gender="man"
print(per2.name,per2.age,per2.gender)
#动态绑定方法
def show():
print("调用函数")
per2.show=show
per2.show()