Hướng dẫn python multiple inheritance access parent variable - biến cha mẹ truy cập đa kế thừa python

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 ...

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: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: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 self._haircolor.

Ngoài ra, bạn thực sự nên sử dụng super 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

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?

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.

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

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.

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

class Child[Mother]:
    def __init__[self]: 
        super[Child, self].__init__[]
    def print_haircolor[self]:
        print self._haircolor
3
AttributeError: type object 'Mother' has no attribute '_haircolor'
6
class Child[Mother]:
    def __init__[self]: 
        super[Child, self].__init__[]
    def print_haircolor[self]:
        print self._haircolor
5
class Child[Mother]:
    def __init__[self]: 
        super[Child, self].__init__[]
    def print_haircolor[self]:
        print self._haircolor
6
class Child[Mother]:
    def __init__[self]: 
        super[Child, self].__init__[]
    def print_haircolor[self]:
        print self._haircolor
7
class Child[Mother]:
    def __init__[self]: 
        super[Child, self].__init__[]
    def print_haircolor[self]:
        print self._haircolor
8
class Child[Mother]:
    def __init__[self]: 
        super[Child, self].__init__[]
    def print_haircolor[self]:
        print self._haircolor
9

COE
COE
SHIVAM
SACHIN
3425
3624
COE
0
AttributeError: type object 'Mother' has no attribute '_haircolor'
6
class Child[Mother]:
    def __init__[self]: 
        super[Child, self].__init__[]
    def print_haircolor[self]:
        print self._haircolor
5
COE
COE
SHIVAM
SACHIN
3425
3624
COE
3
class Child[Mother]:
    def __init__[self]: 
        super[Child, self].__init__[]
    def print_haircolor[self]:
        print self._haircolor
7
COE
COE
SHIVAM
SACHIN
3425
3624
COE
5
COE
COE
SHIVAM
SACHIN
3425
3624
COE
6

COE
COE
SHIVAM
SACHIN
3425
3624
COE
7
COE
COE
SHIVAM
SACHIN
3425
3624
COE
8

COE
COE
SHIVAM
SACHIN
3425
3624
COE
7
Rahul
886012
30000000
0

COE
COE
SHIVAM
SACHIN
3425
3624
COE
7
Rahul
886012
30000000
2

COE
COE
SHIVAM
SACHIN
3425
3624
COE
7
Rahul
886012
30000000
4

COE
COE
SHIVAM
SACHIN
3425
3624
COE
7
Rahul
886012
30000000
6

COE
COE
SHIVAM
SACHIN
3425
3624
COE
7
Rahul
886012
30000000
8

COE
COE
SHIVAM
SACHIN
3425
3624
COE
7
SINGLA ELECTRONICS
DELL Inspiron 15
Redmi Note 5
0

Output:

COE
COE
SHIVAM
SACHIN
3425
3624
COE

Lưu ý: Để biết thêm thông tin, hãy tham khảo các lớp và đối tượng Python. For more information, refer to Python Classes and Objects.

Truy cập các chức năng của lớp cha

Khi một lớp kế thừa từ một lớp khác, nó kế thừa các thuộc tính và phương thức của một lớp khác. Một lớp kế thừa từ một lớp khác được gọi là lớp con và lớp mà lớp con được thừa hưởng được gọi là lớp cha. Nhưng bạn đã bao giờ tự hỏi làm thế nào để truy cập các phương pháp lớp cha mẹ? Đ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:

AttributeError: type object 'Mother' has no attribute '_haircolor'
2
SINGLA ELECTRONICS
DELL Inspiron 15
Redmi Note 5
2
SINGLA ELECTRONICS
DELL Inspiron 15
Redmi Note 5
3
SINGLA ELECTRONICS
DELL Inspiron 15
Redmi Note 5
4

print self._haircolor
3
AttributeError: type object 'Mother' has no attribute '_haircolor'
9
print self._haircolor
0
print self._haircolor
1
SINGLA ELECTRONICS
DELL Inspiron 15
Redmi Note 5
9

This is in Outer class that is Vehicle
This is in Inner class that is Car
This is in multilevel InnerInner class that is Maruti
Just Print It!
0
print self._haircolor
1
print self._haircolor
5
AttributeError: type object 'Mother' has no attribute '_haircolor'
6
print self._haircolor
7

This is in Outer class that is Vehicle
This is in Inner class that is Car
This is in multilevel InnerInner class that is Maruti
Just Print It!
0
print self._haircolor
1
This is in Outer class that is Vehicle
This is in Inner class that is Car
This is in multilevel InnerInner class that is Maruti
Just Print It!
77__16

print self._haircolor
3
AttributeError: type object 'Mother' has no attribute '_haircolor'
9 self._haircolor2
print self._haircolor
1self._haircolor4

This is in Outer class that is Vehicle
This is in Inner class that is Car
This is in multilevel InnerInner class that is Maruti
Just Print It!
0
COE
COE
SHIVAM
SACHIN
3425
3624
COE
7self._haircolor7
print self._haircolor
1self._haircolor9

This is in Outer class that is Vehicle
This is in Inner class that is Car
This is in multilevel InnerInner class that is Maruti
Just Print It!
0
COE
COE
SHIVAM
SACHIN
3425
3624
COE
7self._haircolor7
print self._haircolor
1super4

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

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'
01

This is in Outer class that is Vehicle
This is in Inner class that is Car
This is in multilevel InnerInner class that is Maruti
Just Print It!
0
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
04
AttributeError: type object 'Mother' has no attribute '_haircolor'
6
AttributeError: type object 'Mother' has no attribute '_haircolor'
06

This is in Outer class that is Vehicle
This is in Inner class that is Car
This is in multilevel InnerInner class that is Maruti
Just Print It!
0
AttributeError: type object 'Mother' has no attribute '_haircolor'
08
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
10

print self._haircolor
3
AttributeError: type object 'Mother' has no attribute '_haircolor'
9
AttributeError: type object 'Mother' has no attribute '_haircolor'
13
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
15

AttributeError: type object 'Mother' has no attribute '_haircolor'
16
COE
COE
SHIVAM
SACHIN
3425
3624
COE
7self._haircolor7
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
20

class Child[Mother]:
    def __init__[self]: 
        super[Child, self].__init__[]
    def print_haircolor[self]:
        print self._haircolor
3
AttributeError: type object 'Mother' has no attribute '_haircolor'
6
AttributeError: type object 'Mother' has no attribute '_haircolor'
23
AttributeError: type object 'Mother' has no attribute '_haircolor'
24
class Child[Mother]:
    def __init__[self]: 
        super[Child, self].__init__[]
    def print_haircolor[self]:
        print self._haircolor
7
AttributeError: type object 'Mother' has no attribute '_haircolor'
26
class Child[Mother]:
    def __init__[self]: 
        super[Child, self].__init__[]
    def print_haircolor[self]:
        print self._haircolor
7
AttributeError: type object 'Mother' has no attribute '_haircolor'
28
AttributeError: type object 'Mother' has no attribute '_haircolor'
29

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

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

Output:

Rahul
886012
30000000

Lưu ý: Để biết thêm thông tin, hãy tham khảo kế thừa trong Python. For more information, refer to Inheritance in Python.

Truy cập phương thức lớp cha từ lớp bên trong

Một lớp bên trong hoặc lớp lồng nhau là một lớp được xác định bên trong cơ thể của một lớp khác. Nếu một đối tượng được tạo bằng một lớp, đối tượng bên trong lớp gốc có thể được sử dụng. Một lớp có thể có một hoặc nhiều hơn một lớp bên trong.

Các loại lớp bên trong:

  • Nhiều lớp bên trong
  • Lớp nội địa đa cấp

Nhiều lớp bên trong: Một lớp chứa nhiều hơn một lớp bên trong. A class containing more than one inner class.

Example:

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

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
COE
COE
SHIVAM
SACHIN
3425
3624
COE
7self._haircolor7
AttributeError: type object 'Mother' has no attribute '_haircolor'
42
COE
COE
SHIVAM
SACHIN
3425
3624
COE
6

print self._haircolor
3
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
46
AttributeError: type object 'Mother' has no attribute '_haircolor'
6
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
49

print self._haircolor
3
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
52
AttributeError: type object 'Mother' has no attribute '_haircolor'
6
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
55

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

AttributeError: type object 'Mother' has no attribute '_haircolor'
16
COE
COE
SHIVAM
SACHIN
3425
3624
COE
7self._haircolor7
AttributeError: type object 'Mother' has no attribute '_haircolor'
67
COE
COE
SHIVAM
SACHIN
3425
3624
COE
6

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

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

AttributeError: type object 'Mother' has no attribute '_haircolor'
16
COE
COE
SHIVAM
SACHIN
3425
3624
COE
7self._haircolor7
AttributeError: type object 'Mother' has no attribute '_haircolor'
80
COE
COE
SHIVAM
SACHIN
3425
3624
COE
6

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'
85

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

Output:

SINGLA ELECTRONICS
DELL Inspiron 15
Redmi Note 5

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
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
COE
COE
SHIVAM
SACHIN
3425
3624
COE
7self._haircolor7
AttributeError: type object 'Mother' has no attribute '_haircolor'
42
COE
COE
SHIVAM
SACHIN
3425
3624
COE
6

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
COE
COE
SHIVAM
SACHIN
3425
3624
COE
7self._haircolor7
print self._haircolor
14
COE
COE
SHIVAM
SACHIN
3425
3624
COE
6

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'
2
AttributeError: type object 'Mother' has no attribute '_haircolor'
88

AttributeError: type object 'Mother' has no attribute '_haircolor'
16
COE
COE
SHIVAM
SACHIN
3425
3624
COE
7self._haircolor7
print self._haircolor
38
COE
COE
SHIVAM
SACHIN
3425
3624
COE
6

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

This is in Outer class that is Vehicle
This is in Inner class that is Car
This is in multilevel InnerInner class that is Maruti
Just Print It!
0
COE
COE
SHIVAM
SACHIN
3425
3624
COE
7
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
COE
COE
SHIVAM
SACHIN
3425
3624
COE
6

Output:

This is in Outer class that is Vehicle
This is in Inner class that is Car
This is in multilevel InnerInner class that is Maruti
Just Print It!

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

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?

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?

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?

Trẻ em hoặc các lớp con là các lớp sẽ kế thừa từ lớp cha.Điều đó có nghĩa là mỗi lớp trẻ sẽ có thể sử dụng các phương thức và biến của lớp cha.. That means that each child class will be able to make use of the methods and variables of the parent class.

Bài Viết Liên Quan

Chủ Đề