“Py3 oop”的版本间的差异
跳到导航
跳到搜索
(→类的) |
|||
第184行: | 第184行: | ||
</pre> | </pre> | ||
− | == | + | == super()关键字调用父类方法 == |
<pre> | <pre> | ||
+ | # 创建子类学生Student | ||
+ | class Student(SchoolMember): | ||
+ | def __init__(self, name, age, score): | ||
+ | super().__init__(name, age) | ||
+ | self.score = score | ||
+ | |||
+ | def tell(self): | ||
+ | super().tell() # 等同于 SchoolMember.tell(self) | ||
+ | print('score: {}'.format(self.score)) | ||
</pre> | </pre> | ||
− | |||
== 类 == | == 类 == |
2024年10月24日 (四) 04:30的版本
目录
2024
类(Class)与对象(Object)
class Student: def __init__(self,name,score): self.name = name self.score = score def show(self): print("Name: {}. Score: {}".format(self.name,self.score)) student1 = Student("evan",100) print(student1) student1.show() # ➜ tmp py3 clas.py # <__main__.Student object at 0x7f007c9e9890> # Name: evan. Score: 100
类变量(class variables)与实例变量(instance variables)
class Student: # number属于类变量,定义在方法外,不属于具体实例 number =0 def __init__(self,name,score): self.name = name self.score = score # number = number + 1 ## number属于类变量,定义在方法外,不属于具体实例 Student.number = Student.number + 1 def show(self): print("Name: {}. Score: {}".format(self.name,self.score)) student1 = Student("evan",100) print(student1) student1.show() print(Student.number) print(student1.__class__.number) # ➜ tmp py3 clas.py # <__main__.Student object at 0x7fb907ed9890> # Name: evan. Score: 100 # 1 # 1
类方法(Class method)
class Student: # number属于类变量,定义在方法外,不属于具体实例 number =0 def __init__(self,name,score): self.name = name self.score = score # number = number + 1 ## number属于类变量,定义在方法外,不属于具体实例 Student.number = Student.number + 1 def show(self): print("Name: {}. Score: {}".format(self.name,self.score)) # 定义类方法,打印学生的数量 属于类的方法不使用self参数, 而使用参数cls,代表类本身。另外习惯上对类方法我们会加上@classmethod的修饰符做说明 @classmethod def total(cls): print("Total: {0}".format(cls.number)) student1 = Student("evan",100) Student.total() # Total: 1
类的私有属性(private attribute)和私有方法(private method)
class Student: # number属于类变量,定义在方法外,不属于具体实例 number =0 def __init__(self,name,score): self.name = name self.__score = score # number = number + 1 ## number属于类变量,定义在方法外,不属于具体实例 # Student.number = Student.number + 1 def show(self): print("Name: {}. Score: {}".format(self.name,self.__score)) # 定义类方法,打印学生的数量 属于类的方法不使用self参数, 而使用参数cls,代表类本身。另外习惯上对类方法我们会加上@classmethod的修饰符做说明 # @classmethod # def total(cls): # print("Total: {0}".format(cls.number)) student1 = Student("evan",100) student1.show() # Name: evan. Score: 100 student1.__score ## 打印出错,该属性不能从外部访问 class Student: # number属于类变量,定义在方法外,不属于具体实例 number =0 def __init__(self,name,score): self.name = name self.__score = score # 利用property装饰器把函数伪装成属性 @property def score(self): print("Name: {}. Score: {}".format(self.name,self.__score)) # 定义类方法,打印学生的数量 属于类的方法不使用self参数, 而使用参数cls,代表类本身。另外习惯上对类方法我们会加上@classmethod的修饰符做说明 # @classmethod # def total(cls): # print("Total: {0}".format(cls.number)) student1 = Student("evan",100) #注意: 一旦给函数加上一个装饰器@property,调用函数的时候不用加括号就可以直接调用函数了 student1.score # Name: evan. Score: 100
类的继承(Inheritance)
class SchoolMember: def __init__(self, name, age): self.name = name self.age = age def tell(self): print("Name: {}, Age: {}".format(self.name, self.age),end="") class Teacher(SchoolMember): def __init__(self,name,age,salary): SchoolMember.__init__(self,name,age)# 利用父类进行初始化 self.salary = salary def tell(self): SchoolMember.tell(self) print("Salary: {}".format(self.salary)) class Student(SchoolMember): def __init__(self,name,age,score): SchoolMember.__init__(self,name,age) self.__score = score def tell(self): SchoolMember.tell(self) print(", Score: {}".format(self.__score)) teacher1 = Teacher("Alice",25,30000) student1 = Student("evan",30,100) teacher1.tell() student1.tell() # ➜ tmp py3 clas.py # Name: Alice, Age: 25Salary: 30000 # Name: evan, Age: 30, Score: 100 # ➜ tmp
super()关键字调用父类方法
# 创建子类学生Student class Student(SchoolMember): def __init__(self, name, age, score): super().__init__(name, age) self.score = score def tell(self): super().tell() # 等同于 SchoolMember.tell(self) print('score: {}'.format(self.score))
类
类对象
#!/usr/bin/python3 class MyClass: """一个简单的类实例""" i = 12345 def f(self): return 'hello world' # 实例化类 x = MyClass() # 访问类的属性和方法 print("MyClass 类的属性 i 为:", x.i) print("MyClass 类的方法 f 输出为:", x.f()) # 以上创建了一个新的类实例并将该对象赋给局部变量 x,x 为空的对象。 执行以上程序输出结果为: MyClass 类的属性 i 为: 12345 MyClass 类的方法 f 输出为: hello world
see also
__init__() 的特殊方法(构造方法)
类有一个名为 __init__() 的特殊方法(构造方法),该方法在类实例化时会自动调用 #!/usr/bin/python3 class Complex: def __init__(self, realpart, imagpart): self.r = realpart self.i = imagpart x = Complex(3.0, -4.5) print(x.r, x.i) # 输出结果:3.0 -4.5