Python3面向对象编程实例

/*认真研究完这个实例,理解python面向对象编程的方法*/
class
Person: """人员信息""" # 姓名(共有属性) name = '' # 年龄(共有属性) age = 0 #定义构造函数 def __init__(self, name='', age=0): self.name = name self.age = age # 重载专有方法: __str__ def __str__(self): return "这里重载了 __str__ 专有方法, " + str({'name': self.name, 'age': self.age}) def set_age(self, age): self.age = age class Account: """账户信息""" # 账户余额(私有属性) __balance = 0 # 所有账户总额 __total_balance = 0 # 获取账户余额 # self 必须是方法的第一个参数 def balance(self): return self.__balance '''一共有三种方式定义类的方法 第一种:不加修饰,绑定的是当前的实例 增加账户余额''' def balance_add(self, cost): # self 访问的是本实例 self.__balance += cost # self.__class__ 可以访问类 self.__class__.__total_balance += cost ''' 第二种:用@classmethod来修饰,绑定的是类 类方法(用@classmethod标识,第一个参数为 cls) ''' @classmethod def total_balance(cls): return cls.__total_balance ''' 第二种:用@staticmethod修饰,不绑定 静态方法(用 @staticmethod 标识,不需要类参数或实例参数) ''' @staticmethod def exchange(a, b): return b, a class Teacher(Person, Account): """教师""" # 班级名称,受保护的属性 _class_name = '' def __init__(self, name): # 第一种重载父类__init__()构造方法 # super(子类,self).__init__(参数1,参数2,....) super(Teacher, self).__init__(name) @classmethod def set_class_name(cls,class_name): cls._class_name = class_name def get_info(self): # 以字典的形式返回个人信息 return { 'name': self.name, # 此处访问的是父类Person的属性值 'age': self.age, 'class_name': self._class_name, 'balance': self.balance(), # 此处调用的是子类重载过的方法 } # 方法重载 def balance(self): # Account.__balance 为私有属性,子类无法访问,所以父类提供方法进行访问 return Account.balance(self) * 1.1 class Student(Person, Account): """学生""" _teacher_name = '' def __init__(self, name, age=18): # 第二种重载父类__init__()构造方法 # 父类名称.__init__(self,参数1,参数2,...) Person.__init__(self, name, age) @classmethod def set_teacher_name(cls,teacher_name): cls._teacher_name = teacher_name def get_info(self): # 以字典的形式返回个人信息 return { 'name': self.name, # 此处访问的是父类Person的属性值 'age': self.age, 'teacher_name': self._teacher_name, 'balance': self.balance(), } # 教师 John john = Teacher('John') john.set_class_name('CS2018') john.balance_add(20) john.set_age(36) # 子类的实例可以直接调用父类的方法 print("John's info:", john.get_info()) # 学生 Mary mary = Student('Mary', 18) mary.balance_add(18) mary.set_teacher_name('john') print("Mary's info:", mary.get_info()) # 学生 Fake fake = Student('Fake') fake.balance_add(30) print("Fake's info", fake.get_info()) # 三种不同的方式调用静态方法 print("john.exchange('a', 'b'):", john.exchange('a', 'b')) print('Teacher.exchange(1, 2)', Teacher.exchange(1, 2)) print('Account.exchange(10, 20):', Account.exchange(10, 20)) # 类方法、类属性 print('Account.total_balance():', Account.total_balance()) print('Teacher.total_balance():', Teacher.total_balance()) print('Student.total_balance():', Student.total_balance()) # 重载专有方法 print(john) print(mary) print(fake)

输出:

John's info: {'name': 'John', 'age': 36, 'balance': 22.0, 'class_name': 'CS2018'}
Mary's info: {'teacher_name': 'john', 'name': 'Mary', 'age': 18, 'balance': 18}
Fake's info {'teacher_name': 'john', 'name': 'Fake', 'age': 18, 'balance': 30}
john.exchange('a', 'b'): ('b', 'a')
Teacher.exchange(1, 2) (2, 1)
Account.exchange(10, 20): (20, 10)
Account.total_balance(): 0
Teacher.total_balance(): 20
Student.total_balance(): 48
这里重载了 __str__ 专有方法, {'name': 'John', 'age': 36}
这里重载了 __str__ 专有方法, {'name': 'Mary', 'age': 18}
这里重载了 __str__ 专有方法, {'name': 'Fake', 'age': 18}

原文地址:https://www.cnblogs.com/qiang-wei/p/9451315.html