Hướng dẫn python multiple parent classes - python nhiều lớp cha

Tôi đang cố gắng truy cập một biến thành viên phụ huynh từ một lớp mở rộng. Nhưng chạy mã sau ...

Show

Nội dung chính ShowShow

  • Làm thế nào để bạn truy cập các biến thể hiện của lớp cha trong Python?
  • Làm thế nào để bạn gọi một biến cha mẹ trong Python?
  • Lớp cha có thể truy cập các biến con?
  • Lớp phụ huynh có thể thừa hưởng từ một con trăn lớp trẻ em không?

class Mother(object):
    def __init__(self):
        self._haircolor = "Brown"

class Child(Mother):
    def __init__(self): 
        Mother.__init__(self)   
    def print_haircolor(self):
        print Mother._haircolor

c = Child()
c.print_haircolor()

Nhận được lỗi này:

AttributeError: type object 'Mother' has no attribute '_haircolor'

Tôi đang làm gì sai?

Hỏi ngày 8 tháng 4 năm 2012 lúc 17:12Apr 8, 2012 at 17:12Apr 8, 2012 at 17:12

Bạn đang trộn lẫn các thuộc tính lớp và thể hiện.

print self._haircolor

Đã trả lời ngày 8 tháng 4 năm 2012 lúc 17:15Apr 8, 2012 at 17:15Apr 8, 2012 at 17:15

0

Bạn muốn thuộc tính thể hiện, không phải thuộc tính lớp, vì vậy bạn nên sử dụng

print self._haircolor
29.

Ngoài ra, bạn thực sự nên sử dụng

print self._haircolor
30 trong

AttributeError: type object 'Mother' has no attribute '_haircolor'
0 trong trường hợp bạn quyết định thay đổi quyền thừa kế của mình thành
AttributeError: type object 'Mother' has no attribute '_haircolor'
1 hoặc một cái gì đó.
class Child(Mother):
    def __init__(self): 
        super(Child, self).__init__()
    def print_haircolor(self):
        print self._haircolor

Đã trả lời ngày 8 tháng 4 năm 2012 lúc 17:17Apr 8, 2012 at 17:17Apr 8, 2012 at 17:17

MvchrmvchrmVChrmVChr

48.7K11 Huy hiệu vàng106 Huy hiệu bạc101 Huy hiệu đồng11 gold badges106 silver badges101 bronze badges11 gold badges106 silver badges101 bronze badges

2

Truy cập các chức năng lớp cha Điều này thực sự đơn giản, bạn chỉ cần gọi hàm tạo của lớp cha trong hàm tạo của lớp con và sau đó đối tượng của lớp con có thể truy cập các phương thức và thuộc tính của lớp cha.

Example:

Làm thế nào để bạn gọi một biến cha mẹ trong Python?

Lớp cha có thể truy cập các biến con?

Lớp cha có thể truy cập các biến con?

Lớp phụ huynh có thể thừa hưởng từ một con trăn lớp trẻ em không?

Lớp phụ huynh có thể thừa hưởng từ một con trăn lớp trẻ em không?

Nhận được lỗi này:

Output:

AttributeError: type object 'Mother' has no attribute '_haircolor'
3

Tôi đang làm gì sai? For more information, refer to Python Classes and Objects.

Hỏi ngày 8 tháng 4 năm 2012 lúc 17:12Apr 8, 2012 at 17:12

Bạn đang trộn lẫn các thuộc tính lớp và thể hiện.

Example:

Đã trả lời ngày 8 tháng 4 năm 2012 lúc 17:15Apr 8, 2012 at 17:15

Output:

print self._haircolor
3

Bạn muốn thuộc tính thể hiện, không phải thuộc tính lớp, vì vậy bạn nên sử dụng

print self._haircolor
29. For more information, refer to Inheritance in Python.

Ngoài ra, bạn thực sự nên sử dụng
print self._haircolor
30 trong

AttributeError: type object 'Mother' has no attribute '_haircolor'
0 trong trường hợp bạn quyết định thay đổi quyền thừa kế của mình thành
AttributeError: type object 'Mother' has no attribute '_haircolor'
1 hoặc một cái gì đó.
class Child(Mother):
    def __init__(self): 
        super(Child, self).__init__()
    def print_haircolor(self):
        print self._haircolor

Đã trả lời ngày 8 tháng 4 năm 2012 lúc 17:17Apr 8, 2012 at 17:17

  • MvchrmvchrmVChr
  • 48.7K11 Huy hiệu vàng106 Huy hiệu bạc101 Huy hiệu đồng11 gold badges106 silver badges101 bronze badges

Truy cập các chức năng lớp cha Điều này thực sự đơn giản, bạn chỉ cần gọi hàm tạo của lớp cha trong hàm tạo của lớp con và sau đó đối tượng của lớp con có thể truy cập các phương thức và thuộc tính của lớp cha. A class containing more than one inner class.

Example:

Hàm Super () trong Python bằng cách sử dụng hàm Super (), chúng ta có thể truy cập các thành viên lớp cha từ lớp con. Hàm Super () là một hàm tích hợp, rất hữu ích để gọi hàm tạo, phương thức và biến siêu lớp rõ ràng từ lớp con.

Output:

AttributeError: type object 'Mother' has no attribute '_haircolor'
3

AttributeError: type object 'Mother' has no attribute '_haircolor'
5082
AttributeError: type object 'Mother' has no attribute '_haircolor'
6
AttributeError: type object 'Mother' has no attribute '_haircolor'
84 In multilevel inner classes, the inner class contains another class which is inner classes to the previous one.82
AttributeError: type object 'Mother' has no attribute '_haircolor'
6
AttributeError: type object 'Mother' has no attribute '_haircolor'
84
In multilevel inner classes, the inner class contains another class which is inner classes to the previous one.

Example:

Lớp bên trong đa cấp: Trong các lớp bên trong đa cấp, lớp bên trong chứa một lớp khác là các lớp bên trong của lớp trước.

AttributeError: type object 'Mother' has no attribute '_haircolor'
4
AttributeError: type object 'Mother' has no attribute '_haircolor'
9
print self._haircolor
0
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
15
print self._haircolor
3
AttributeError: type object 'Mother' has no attribute '_haircolor'
37
print self._haircolor
297
AttributeError: type object 'Mother' has no attribute '_haircolor'
42
AttributeError: type object 'Mother' has no attribute '_haircolor'
36
AttributeError: type object 'Mother' has no attribute '_haircolor'
4
AttributeError: type object 'Mother' has no attribute '_haircolor'
2
AttributeError: type object 'Mother' has no attribute '_haircolor'
58
print self._haircolor
3
AttributeError: type object 'Mother' has no attribute '_haircolor'
9
AttributeError: type object 'Mother' has no attribute '_haircolor'
61
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
15
print self._haircolor
3
AttributeError: type object 'Mother' has no attribute '_haircolor'
37
print self._haircolor
297
print self._haircolor
14
AttributeError: type object 'Mother' has no attribute '_haircolor'
36
AttributeError: type object 'Mother' has no attribute '_haircolor'
4
AttributeError: type object 'Mother' has no attribute '_haircolor'
2
AttributeError: type object 'Mother' has no attribute '_haircolor'
71
AttributeError: type object 'Mother' has no attribute '_haircolor'
82
AttributeError: type object 'Mother' has no attribute '_haircolor'
6
AttributeError: type object 'Mother' has no attribute '_haircolor'
84

Lớp bên trong đa cấp: Trong các lớp bên trong đa cấp, lớp bên trong chứa một lớp khác là các lớp bên trong của lớp trước.

AttributeError: type object 'Mother' has no attribute '_haircolor'
4
AttributeError: type object 'Mother' has no attribute '_haircolor'
9
print self._haircolor
0
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
15
print self._haircolor
3
AttributeError: type object 'Mother' has no attribute '_haircolor'
37
print self._haircolor
297
AttributeError: type object 'Mother' has no attribute '_haircolor'
42
AttributeError: type object 'Mother' has no attribute '_haircolor'
36
AttributeError: type object 'Mother' has no attribute '_haircolor'
4
AttributeError: type object 'Mother' has no attribute '_haircolor'
2
AttributeError: type object 'Mother' has no attribute '_haircolor'
58
print self._haircolor
3
AttributeError: type object 'Mother' has no attribute '_haircolor'
9
AttributeError: type object 'Mother' has no attribute '_haircolor'
61
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
15
print self._haircolor
3
AttributeError: type object 'Mother' has no attribute '_haircolor'
37
print self._haircolor
297
print self._haircolor
14
AttributeError: type object 'Mother' has no attribute '_haircolor'
36
AttributeError: type object 'Mother' has no attribute '_haircolor'
4
AttributeError: type object 'Mother' has no attribute '_haircolor'
2
AttributeError: type object 'Mother' has no attribute '_haircolor'
71
AttributeError: type object 'Mother' has no attribute '_haircolor'
82
AttributeError: type object 'Mother' has no attribute '_haircolor'
6
AttributeError: type object 'Mother' has no attribute '_haircolor'
84

Output:

AttributeError: type object 'Mother' has no attribute '_haircolor'
2
AttributeError: type object 'Mother' has no attribute '_haircolor'
88
AttributeError: type object 'Mother' has no attribute '_haircolor'
16
AttributeError: type object 'Mother' has no attribute '_haircolor'
37
print self._haircolor
297
AttributeError: type object 'Mother' has no attribute '_haircolor'
8438
AttributeError: type object 'Mother' has no attribute '_haircolor'
36
print self._haircolor
3
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
96
AttributeError: type object 'Mother' has no attribute '_haircolor'
6
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
99
print self._haircolor
3
print self._haircolor
1
print self._haircolor
0216
print self._haircolor
1
print self._haircolor
05
AttributeError: type object 'Mother' has no attribute '_haircolor'
4
AttributeError: type object 'Mother' has no attribute '_haircolor'
9
print self._haircolor
08
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
15
AttributeError: type object 'Mother' has no attribute '_haircolor'
4
AttributeError: type object 'Mother' has no attribute '_haircolor'
2
print self._haircolor
18
AttributeError: type object 'Mother' has no attribute '_haircolor'
40
AttributeError: type object 'Mother' has no attribute '_haircolor'
37
print self._haircolor
61
print self._haircolor
3
AttributeError: type object 'Mother' has no attribute '_haircolor'
9
print self._haircolor
0
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
15
print self._haircolor
65
AttributeError: type object 'Mother' has no attribute '_haircolor'
16
print self._haircolor
1
print self._haircolor
0216
print self._haircolor
1
print self._haircolor
29
print self._haircolor
69
print self._haircolor
3
AttributeError: type object 'Mother' has no attribute '_haircolor'
9
print self._haircolor
08
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
15
print self._haircolor
73
print self._haircolor
74
AttributeError: type object 'Mother' has no attribute '_haircolor'
36

Làm thế nào để bạn truy cập các biến thể hiện của lớp cha trong Python?

AttributeError: type object 'Mother' has no attribute '_haircolor'
4Làm thế nào để bạn truy cập các biến thể hiện của lớp cha trong Python?call the constructor of parent class inside the constructor of child class and then the object of a child class can access the methods and attributes of the parent class.

Truy cập các chức năng lớp cha Điều này thực sự đơn giản, bạn chỉ cần gọi hàm tạo của lớp cha trong hàm tạo của lớp con và sau đó đối tượng của lớp con có thể truy cập các phương thức và thuộc tính của lớp cha.call the constructor of parent class inside the constructor of child class and then the object of a child class can access the methods and attributes of the parent class.

Làm thế nào để bạn gọi một biến cha mẹ trong Python? in python By using the super() function, we can access the parent class members from the child class. The super() function is a built-in function, which is useful to call the superclass constructor, methods, and variables explicitly from the child class.

Hàm Super () trong Python bằng cách sử dụng hàm Super (), chúng ta có thể truy cập các thành viên lớp cha từ lớp con.Hàm Super () là một hàm tích hợp, rất hữu ích để gọi hàm tạo, phương thức và biến siêu lớp rõ ràng từ lớp con. in python By using the super() function, we can access the parent class members from the child class. The super() function is a built-in function, which is useful to call the superclass constructor, methods, and variables explicitly from the child class.

Lớp cha có thể truy cập các biến con?you can't directly access parent class instance variables. For example, if the parent had a height instance variable, child class method definitions wouldn't be able to access this directly.

Khía cạnh bất thường duy nhất là, trong các định nghĩa phương thức lớp con, bạn không thể truy cập trực tiếp các biến thể hiện của lớp phụ huynh.Ví dụ: nếu cha mẹ có biến thể hiện chiều cao, các định nghĩa phương thức lớp con sẽ không thể truy cập trực tiếp điều này.you can't directly access parent class instance variables. For example, if the parent had a height instance variable, child class method definitions wouldn't be able to access this directly.

Lớp phụ huynh có thể thừa hưởng từ một con trăn lớp trẻ em không?. That means that each child class will be able to make use of the methods and variables of the parent class.