Đa kế thừa trong hàm tạo python

Trong bài viết trước của chúng ta, khi chúng ta đang nói về thừa kế, một

Features of the mother class
Features of the father class
4 đang kế thừa các thuộc tính và chức năng của một
Features of the mother class
Features of the father class
4 khác. Vậy đa thừa kế là gì?

Giống như một đứa trẻ ngoài đời có những đặc điểm của cả bố và mẹ. Hãy bắt tay vào viết mã ngay lập tức, bạn sẽ hiểu rõ hơn nhiều

class Mother:
    def print_Mother[self]:
        print["Features of the mother class"]
 
class Father:
    def print_Father[self]:
        print["Features of the father class"]
 
class Child[Mother, Father]:
    pass
 
object_d = Child[]
object_d.print_Mother[]
object_d.print_Father[]

Bây giờ chúng ta hãy xem đầu ra;

Features of the mother class
Features of the father class

Như bạn có thể thấy, chúng tôi đã lấy và sử dụng các chức năng của nhiều lớp

Nhưng có một điểm rất quan trọng mà chúng ta cần chú ý là trong multifunction, chúng ta chỉ có thể sử dụng một

Features of the mother class
Features of the father class
4, một thuộc tính hoặc một hàm trùng tên thôi, ví dụ ngay thôi

class Mother:
    def eye_colour[self]:
        print["Features of the mother class"]

class Father:
    def eye_colour[self]:
        print["Features of the father class"]


class Child[Mother, Father]:
    pass

object_d = Child[]
object_d.eye_colour[]

Cả lớp

Features of the mother class
Features of the father class
9 và lớp
class Mother:
    def eye_colour[self]:
        print["Features of the mother class"]

class Father:
    def eye_colour[self]:
        print["Features of the father class"]


class Child[Mother, Father]:
    pass

object_d = Child[]
object_d.eye_colour[]
0 đều có thuộc tính
class Mother:
    def eye_colour[self]:
        print["Features of the mother class"]

class Father:
    def eye_colour[self]:
        print["Features of the father class"]


class Child[Mother, Father]:
    pass

object_d = Child[]
object_d.eye_colour[]
1

Vì vậy, hãy xem lớp

class Mother:
    def eye_colour[self]:
        print["Features of the mother class"]

class Father:
    def eye_colour[self]:
        print["Features of the father class"]


class Child[Mother, Father]:
    pass

object_d = Child[]
object_d.eye_colour[]
2 sẽ kế thừa từ lớp nào

Features of the mother class
Features of the father class
2

Đúng như chúng tôi đoán, cô ấy mang đặc điểm của lớp

class Mother:
    def eye_colour[self]:
        print["Features of the mother class"]

class Father:
    def eye_colour[self]:
        print["Features of the father class"]


class Child[Mother, Father]:
    pass

object_d = Child[]
object_d.eye_colour[]
3. Bởi vì trước tiên, lớp
Features of the mother class
Features of the father class
9 được kế thừa các thuộc tính của nó. Bây giờ hãy thay đổi nó và lần này kế thừa lớp
class Mother:
    def eye_colour[self]:
        print["Features of the mother class"]

class Father:
    def eye_colour[self]:
        print["Features of the father class"]


class Child[Mother, Father]:
    pass

object_d = Child[]
object_d.eye_colour[]
0 trước

Features of the mother class
Features of the father class
6

đầu tiên, chúng tôi kế thừa lớp

class Mother:
    def eye_colour[self]:
        print["Features of the mother class"]

class Father:
    def eye_colour[self]:
        print["Features of the father class"]


class Child[Mother, Father]:
    pass

object_d = Child[]
object_d.eye_colour[]
0, hãy xem đầu ra của chúng tôi

Features of the mother class
Features of the father class
8

Bây giờ bạn sẽ nói rằng tại sao tôi phải tạo một hàm có cùng tên, tôi sẽ tạo một hàm có tên khác. Nhưng hãy nhớ rằng, bạn có thể không đơn độc trong một dự án. Bạn có thể gặp phải những vấn đề như vậy khi làm việc với nhiều người. Hãy nói về những việc cần làm nếu hàm xây dựng

class Mother:
    def eye_colour[self]:
        print["Features of the mother class"]

class Father:
    def eye_colour[self]:
        print["Features of the father class"]


class Child[Mother, Father]:
    pass

object_d = Child[]
object_d.eye_colour[]
7 tồn tại trong cả hai lớp. Hãy thể hiện nó ngay bây giờ;

Features of the mother class
Features of the father class
0

Như bạn có thể thấy khi chúng tôi chạy mã của mình

Features of the mother class
Features of the father class
1

Chúng tôi nhận được kết quả như vậy Vì vậy, vì chúng tôi kế thừa lớp

class Mother:
    def eye_colour[self]:
        print["Features of the mother class"]

class Father:
    def eye_colour[self]:
        print["Features of the father class"]


class Child[Mother, Father]:
    pass

object_d = Child[]
object_d.eye_colour[]
0 trước nên thuộc tính của lớp
class Mother:
    def eye_colour[self]:
        print["Features of the mother class"]

class Father:
    def eye_colour[self]:
        print["Features of the father class"]


class Child[Mother, Father]:
    pass

object_d = Child[]
object_d.eye_colour[]
0 được ưu tiên. Chà, bây giờ bạn sẽ nói rằng chúng ta không thể tự thay đổi thứ tự ưu tiên này khi thích hợp?

Hàm xây dựng trong đa kế thừa

Features of the mother class
Features of the father class
4

Hãy nhìn vào đầu ra;

Features of the mother class
Features of the father class
5

Ở đây chúng ta đã ưu tiên hàm

class Mother:
    def eye_colour[self]:
        print["Features of the mother class"]

class Father:
    def eye_colour[self]:
        print["Features of the father class"]


class Child[Mother, Father]:
    pass

object_d = Child[]
object_d.eye_colour[]
7 của lớp
Features of the mother class
Features of the father class
9. Các bạn để ý một chút sẽ thấy câu này, chúng ta chỉ nhận được hàm
class Mother:
    def eye_colour[self]:
        print["Features of the mother class"]

class Father:
    def eye_colour[self]:
        print["Features of the father class"]


class Child[Mother, Father]:
    pass

object_d = Child[]
object_d.eye_colour[]
7 chứ không phải tất cả các thuộc tính và hàm của lớp
Features of the mother class
Features of the father class
9. Làm thế nào chúng ta có thể nhận được thông tin từ đây?

Hãy cho thấy nó với một ví dụ

Features of the mother class
Features of the father class
0

đầu ra

Features of the mother class
Features of the father class
1

Như bạn có thể thấy, đứa trẻ đã nhận được đặc điểm màu mắt từ Mẹ và đặc điểm chiều cao từ Cha. Lý do là vì chúng ta viết lớp

class Mother:
    def eye_colour[self]:
        print["Features of the mother class"]

class Father:
    def eye_colour[self]:
        print["Features of the father class"]


class Child[Mother, Father]:
    pass

object_d = Child[]
object_d.eye_colour[]
0 trước nên nó kế thừa hàm
Features of the mother class
Features of the father class
28 của lớp
class Mother:
    def eye_colour[self]:
        print["Features of the mother class"]

class Father:
    def eye_colour[self]:
        print["Features of the father class"]


class Child[Mother, Father]:
    pass

object_d = Child[]
object_d.eye_colour[]
0. Nhưng vì chúng ta đã chỉ định hàm
class Mother:
    def eye_colour[self]:
        print["Features of the mother class"]

class Father:
    def eye_colour[self]:
        print["Features of the father class"]


class Child[Mother, Father]:
    pass

object_d = Child[]
object_d.eye_colour[]
7 của lớp
Features of the mother class
Features of the father class
9 là ưu tiên, nên nó kế thừa hàm
class Mother:
    def eye_colour[self]:
        print["Features of the mother class"]

class Father:
    def eye_colour[self]:
        print["Features of the father class"]


class Child[Mother, Father]:
    pass

object_d = Child[]
object_d.eye_colour[]
7 của lớp Mother là ưu tiên

Theo cách tương tự, nếu chúng ta ưu tiên hàm

Features of the mother class
Features of the father class
28 của lớp
Features of the mother class
Features of the father class
9, thì hàm
Features of the mother class
Features of the father class
28 của lớp
Features of the mother class
Features of the father class
9 sẽ được ưu tiên

Features of the mother class
Features of the father class
2

Khi chúng ta nhìn vào đầu ra, bạn sẽ thấy rằng nó kế thừa cả hai thuộc tính từ lớp

Features of the mother class
Features of the father class
9

Features of the mother class
Features of the father class
3

Như bạn có thể thấy, mặc dù chúng ta đã viết lớp

class Mother:
    def eye_colour[self]:
        print["Features of the mother class"]

class Father:
    def eye_colour[self]:
        print["Features of the father class"]


class Child[Mother, Father]:
    pass

object_d = Child[]
object_d.eye_colour[]
0 trước nhưng nó lại lấy các thuộc tính của lớp
Features of the mother class
Features of the father class
9 vì chúng ta đã ưu tiên cho các thuộc tính của lớp
Features of the mother class
Features of the father class
9

Chúng ta có thể sử dụng hàm tạo trong thừa kế trong Python không?

Ví dụ về thừa kế . Hàm tạo của lớp con luôn được gọi đến hàm tạo của lớp cha để khởi tạo giá trị cho các thuộc tính trong lớp cha, sau đó nó bắt đầu gán giá trị cho các thuộc tính của nó. constructor of class used to create an object [instance], and assign the value for the attributes. Constructor of subclasses always called to a constructor of parent class to initialize value for the attributes in the parent class, then it start assign value for its attributes.

Constructor nào được gọi trong đa kế thừa?

Constructor tự động được gọi khi đối tượng được tạo. Đa thừa kế. Đa kế thừa là một tính năng của C++ trong đó một lớp có thể xuất phát từ một số [hai hoặc nhiều] lớp cơ sở. Các hàm tạo của các lớp kế thừa được gọi theo cùng thứ tự mà chúng được kế thừa .

Làm thế nào hàm tạo được thực thi trong nhiều kế thừa?

Trình xây dựng. Trong đa kế thừa, hàm tạo của lớp cơ sở và hàm tạo của lớp dẫn xuất được tự động thực thi khi một đối tượng của lớp dẫn xuất được tạo . Hàm tạo của lớp cơ sở được thực thi trước, sau đó mới thực hiện hàm tạo của lớp dẫn xuất.

Bạn có thể có nhiều hàm tạo trong Python không?

Cung cấp nhiều hàm tạo với @classmethod trong Python. Một kỹ thuật hiệu quả để cung cấp nhiều hàm tạo trong Python là sử dụng @classmethod . Trình trang trí này cho phép bạn biến một phương thức thông thường thành một phương thức lớp. Không giống như các phương thức thông thường, các phương thức lớp không lấy thể hiện hiện tại, self, làm đối số.

Chủ Đề