- python 中的基本数据类型一次仅指一种数据。
如果您可以声明一种本身包含多个数据类型并且可以在任何函数的帮助下使用它们的数据类型,那会怎样?Python 类class为您提供了这个机会。
“name”和“age”是“Person”类的两个成员变量。每次我们声明这个类的一个对象时,它都会包含这两个变量作为它的成员。这部分是可选的,因为它们可以由构造函数初始化。
Python 类构造函数是创建类的新对象时要执行的第一段代码
构造函数可用于将值放入成员变量中。您还可以在构造函数中打印消息以确认对象是否已创建。
一旦我们了解了 python 继承,我们将了解构造函数的更大作用。构造函数方法以 def __init__ 开头。之后,第一个参数必须是“self”,因为它传递对类本身实例的引用。您还可以添加其他参数,如示例中所示的方式。'person_name' 和 'person_age' 是要创建新对象时发送的两个参数。
在python中创建对象的方式非常简单。首先,您输入新对象的名称,后跟赋值运算符和带参数的类名称(在构造函数中定义)。请记住,参数的数量和类型应与构造函数中接收到的参数兼容。
创建对象后,可以调用成员方法并访问成员属性(前提是它们是可访问的)。
- # definition of the superclass starts here
- class Person:
- # initializing the variables
- name = ""
- age = 0
- #defining constructor
- def __init__(self, person_name, person_age):
- self.name = person_name
- self.age = person_age
- # defining class methods
- def show_name(self):
- print(self.name)
- def show_age(self):
- print(self.age)
- # end of superclass definition
- #definition of subclass starts here
- class Student(Person): # Person is the superclass and Student is the subclass
- studentId = ""
- def __init__(self, student_name, student_age, student_id):
- # Calling the superclass constructor and sending values of attributes.
# Python super() 函数允许我们隐式地引用超类
- # 等同于下行 super().__init__(student_name, student_age)
- Person.__init__(self, student_name, student_age)
- self.student_id = student_id
- def get_id(self):
- return self.student_id #returns the value of student id
- #end of subclass definition
- # Create an object of the superclass
- person1 = Person("Richard", 23)
- # call member methods of the objects
- person1.show_age()
- # Create an object of the subclass
- student1 = Student("Max", 22, "102")
- print(student1.get_id())
- student1.show_name()
在多级继承的情况下,它会引用哪个类?好吧,Python super() 将始终引用直接超类。
从输出中我们可以清楚地看到,首先__init__()调用了类 C的函数,然后是类 B,然后是类 A。调用sub_method()函数也发生了类似的事情。
- class A:
- def __init__(self):
- print('Initializing: class A')
- def sub_method(self, b):
- print('Printing from class A:', b)
- class B(A):
- def __init__(self):
- print('Initializing: class B')
- super().__init__()
- def sub_method(self, b):
- print('Printing from class B:', b)
- super().sub_method(b + 1)
- class C(B):
- def __init__(self):
- print('Initializing: class C')
- super().__init__()
- def sub_method(self, b):
- print('Printing from class C:', b)
- super().sub_method(b + 1)
- if __name__ == '__main__':
- c = C()
- c.sub_method(1)
- # Initializing: class C
- # Initializing: class B
- # Initializing: class A
- # Printing from class C: 1
- # Printing from class B: 2
- # Printing from class A: 3
使用python多重继承解决冲突
- #definition of the class starts here
- class Person:
- #defining constructor
- def __init__(self, personName, personAge):
- self.name = personName
- self.age = personAge
- #defining class methods
- def showName(self):
- print(self.name)
- def showAge(self):
- print(self.age)
- #end of class definition
- # defining another class
- class Student: # Person is the
- def __init__(self, studentId):
- self.studentId = studentId
- def getId(self):
- return self.studentId
- class Resident(Person, Student): # extends both Person and Student class
- def __init__(self, name, age, id):
- Person.__init__(self, name, age)
- Student.__init__(self, id)
- # Create an object of the subclass
- resident1 = Resident('John', 30, '102')
- resident1.showName()
- print(resident1.getId())
- # John
- # 102
- # 使用python多重继承解决冲突
- class A:
- def __init__(self):
- self.name = 'John'
- self.age = 23
- def getName(self):
- return self.name
- class B:
- def __init__(self):
- self.name = 'Richard'
- self.id = '32'
- def getName(self):
- return self.name
- class C(A, B):
- def __init__(self):
- A.__init__(self)
- B.__init__(self)
- def getName(self):
- return self.name
- C1 = C()
- print(C1.getName())
- # 打印出来的名字是“Richard”而不是“John”。
- # 让我们试着了解这里发生了什么。在 C 的构造函数中,调用的第一个构造函数是 A 的构造函数。因此,C 中 name 的值变为与 A 中 name 的值相同。
- # 但之后,当调用 B 的构造函数时,name 的值C 中的名称被 B 中的 name 值覆盖。
- # 因此,C 的 name 属性在打印时保留值 'Richard'。即使我们将 C 类声明为:Class C(B, A)
- # 层次结构完全取决于子类中 __init__() 调用的顺序。为了完美地处理它,有一个名为 MRO(方法解析顺序)的协议
- class A:
- def __init__(self):
- super().__init__()
- self.name = 'John'
- self.age = 23
- def getName(self):
- return self.name
- class B:
- def __init__(self):
- super().__init__()
- self.name = 'Richard'
- self.id = '32'
- def getName(self):
- return self.name
- class C(A, B):
- def __init__(self):
- super().__init__()
- def getName(self):
- return self.name
- C1 = C()
- print(C1.getName())
- # class C(A, B):打印出来的名字是“John”。
- # MRO 以从左到右的方式深度工作。__init__ 方法中的 super() 表示下一个层次结构中的类。
- # 首先C的super()表示A。然后A的构造函数中的super寻找它的超类。如果没有找到,则执行其余代码并返回。
- # 所以这里调用构造函数的顺序是:C -> A -> B
- # 如果我们调用print(C.__mro__),那么我们就可以看到MRO的traceroute了。
- # (<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>)
- # 一旦调用了 A 的构造函数并访问了属性“name”,它就不会访问 B 中的属性“name”。为了使用 python 多重继承,必须更好地理解 MRO。
- # class C(B, A):打印出来的名字是“Richard”。调用print(C.__mro__)
- # (<class '__main__.C'>, <class '__main__.B'>, <class '__main__.A'>, <class 'object'>)
评论
发表评论