Lớp con kế thừa từ lớp cha trong Python là gì?

Lớp mới được tạo ra được gọi là lớp con (lớp con hoặc lớp dẫn xuất) và lớp hiện có mà lớp con được dẫn xuất từ ​​đó được gọi là lớp cha (lớp cha hoặc lớp cơ sở)


Cú pháp kế thừa Python

Đây là cú pháp kế thừa trong Python,

# define a superclass
class super_class:
    # attributes and method definition

# inheritance
class sub_class(super_class):
    # attributes and method of super_class
    # attributes and method of sub_class

Ở đây, chúng ta kế thừa lớp

class Animal:

    # attribute and method of the parent class
    name = ""
    
    def eat(self):
        print("I can eat")

# inherit from Animal
class Dog(Animal):

    # new method in subclass
    def display(self):
        # access name attribute of superclass using self
        print("My name is ", self.name)

# create an object of the subclass
labrador = Dog()

# access superclass attribute and method 
labrador.name = "Rohu"
labrador.eat()

# call subclass method 
labrador.display()
4 từ lớp
class Animal:

    # attribute and method of the parent class
    name = ""
    
    def eat(self):
        print("I can eat")

# inherit from Animal
class Dog(Animal):

    # new method in subclass
    def display(self):
        # access name attribute of superclass using self
        print("My name is ", self.name)

# create an object of the subclass
labrador = Dog()

# access superclass attribute and method 
labrador.name = "Rohu"
labrador.eat()

# call subclass method 
labrador.display()
5


ví dụ 1. Kế thừa Python

class Animal:

    # attribute and method of the parent class
    name = ""
    
    def eat(self):
        print("I can eat")

# inherit from Animal
class Dog(Animal):

    # new method in subclass
    def display(self):
        # access name attribute of superclass using self
        print("My name is ", self.name)

# create an object of the subclass
labrador = Dog()

# access superclass attribute and method 
labrador.name = "Rohu"
labrador.eat()

# call subclass method 
labrador.display()

đầu ra

I can eat
My name is  Rohu

Trong ví dụ trên, chúng ta đã dẫn xuất một phân lớp Dog từ một siêu lớp Animal. Lưu ý các tuyên bố,

labrador.name = "Rohu"

labrador.eat()

Ở đây, chúng tôi đang sử dụng labrador (đối tượng của Chó) để truy cập tên và

class Animal:

    # attribute and method of the parent class
    name = ""
    
    def eat(self):
        print("I can eat")

# inherit from Animal
class Dog(Animal):

    # new method in subclass
    def display(self):
        # access name attribute of superclass using self
        print("My name is ", self.name)

# create an object of the subclass
labrador = Dog()

# access superclass attribute and method 
labrador.name = "Rohu"
labrador.eat()

# call subclass method 
labrador.display()
6 của lớp Động vật. Điều này là có thể bởi vì lớp con kế thừa tất cả các thuộc tính và phương thức của lớp cha

Ngoài ra, chúng ta đã truy cập thuộc tính name bên trong phương thức của lớp Dog bằng cách sử dụng

class Animal:

    # attribute and method of the parent class
    name = ""
    
    def eat(self):
        print("I can eat")

# inherit from Animal
class Dog(Animal):

    # new method in subclass
    def display(self):
        # access name attribute of superclass using self
        print("My name is ", self.name)

# create an object of the subclass
labrador = Dog()

# access superclass attribute and method 
labrador.name = "Rohu"
labrador.eat()

# call subclass method 
labrador.display()
7


là một mối quan hệ

Trong Python, thừa kế là một mối quan hệ is-a. Nghĩa là, chúng ta chỉ sử dụng tính kế thừa nếu tồn tại mối quan hệ is-a giữa hai lớp. Ví dụ,

  1. Xe hơi là phương tiện
  2. Táo là một loại trái cây
  3. Mèo là một con vật

Ở đây, Xe có thể kế thừa từ Xe, Apple có thể kế thừa từ Trái cây, v.v.


ví dụ 2. Kế thừa trong Python

Hãy xem một ví dụ khác về thừa kế trong Python,

Đa giác là hình khép kín có 3 cạnh trở lên. Giả sử, chúng ta có một lớp tên là

class Animal:

    # attribute and method of the parent class
    name = ""
    
    def eat(self):
        print("I can eat")

# inherit from Animal
class Dog(Animal):

    # new method in subclass
    def display(self):
        # access name attribute of superclass using self
        print("My name is ", self.name)

# create an object of the subclass
labrador = Dog()

# access superclass attribute and method 
labrador.name = "Rohu"
labrador.eat()

# call subclass method 
labrador.display()
8 được định nghĩa như sau,

class Polygon:
    def __init__(self, no_of_sides):
        self.n = no_of_sides
        self.sides = [0 for i in range(no_of_sides)]

    def inputSides(self):
        self.sides = [float(input("Enter side "+str(i+1)+" : ")) for i in range(self.n)]

    def dispSides(self):
        for i in range(self.n):
            print("Side",i+1,"is",self.sides[i])

Lớp này có các thuộc tính dữ liệu để lưu trữ số cạnh

class Animal:

    # attribute and method of the parent class
    name = ""
    
    def eat(self):
        print("I can eat")

# inherit from Animal
class Dog(Animal):

    # new method in subclass
    def display(self):
        # access name attribute of superclass using self
        print("My name is ", self.name)

# create an object of the subclass
labrador = Dog()

# access superclass attribute and method 
labrador.name = "Rohu"
labrador.eat()

# call subclass method 
labrador.display()
9 và độ lớn của mỗi cạnh dưới dạng danh sách có tên là
I can eat
My name is  Rohu
0

  • Phương pháp
    I can eat
    My name is  Rohu
    1 lấy độ lớn của mỗi bên
  • Phương thức
    I can eat
    My name is  Rohu
    2 hiển thị các độ dài cạnh này

Tam giác là đa giác có 3 cạnh. Vì vậy, chúng ta có thể tạo một lớp tên là

I can eat
My name is  Rohu
3 kế thừa từ
class Animal:

    # attribute and method of the parent class
    name = ""
    
    def eat(self):
        print("I can eat")

# inherit from Animal
class Dog(Animal):

    # new method in subclass
    def display(self):
        # access name attribute of superclass using self
        print("My name is ", self.name)

# create an object of the subclass
labrador = Dog()

# access superclass attribute and method 
labrador.name = "Rohu"
labrador.eat()

# call subclass method 
labrador.display()
8. Điều này làm cho tất cả các thuộc tính của lớp
class Animal:

    # attribute and method of the parent class
    name = ""
    
    def eat(self):
        print("I can eat")

# inherit from Animal
class Dog(Animal):

    # new method in subclass
    def display(self):
        # access name attribute of superclass using self
        print("My name is ", self.name)

# create an object of the subclass
labrador = Dog()

# access superclass attribute and method 
labrador.name = "Rohu"
labrador.eat()

# call subclass method 
labrador.display()
8 có sẵn cho lớp Triangle

Chúng tôi không cần phải xác định lại chúng (khả năng sử dụng lại mã).

I can eat
My name is  Rohu
3 có thể được định nghĩa như sau

class Triangle(Polygon):
    def __init__(self):
        Polygon.__init__(self,3)

    def findArea(self):
        a, b, c = self.sides
        # calculate the semi-perimeter
        s = (a + b + c) / 2
        area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
        print('The area of the triangle is %0.2f' %area)

Tuy nhiên, lớp

I can eat
My name is  Rohu
3 có một phương pháp mới
I can eat
My name is  Rohu
8 để tìm và in diện tích tam giác

Bây giờ hãy xem mã làm việc hoàn chỉnh của ví dụ trên bao gồm tạo một đối tượng,

class Polygon:
    # Initializing the number of sides
    def __init__(self, no_of_sides):
        self.n = no_of_sides
        self.sides = [0 for i in range(no_of_sides)]

    def inputSides(self):
        self.sides = [float(input("Enter side "+str(i+1)+" : ")) for i in range(self.n)]

    # method to display the length of each side of the polygon
    def dispSides(self):
        for i in range(self.n):
            print("Side",i+1,"is",self.sides[i])

class Triangle(Polygon):
    # Initializing the number of sides of the triangle to 3 by 
    # calling the __init__ method of the Polygon class
    def __init__(self):
        Polygon.__init__(self,3)

    def findArea(self):
        a, b, c = self.sides

        # calculate the semi-perimeter
        s = (a + b + c) / 2

        # Using Heron's formula to calculate the area of the triangle
        area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
        print('The area of the triangle is %0.2f' %area)

# Creating an instance of the Triangle class
t = Triangle()

# Prompting the user to enter the sides of the triangle
t.inputSides()

# Displaying the sides of the triangle
t.dispSides()

# Calculating and printing the area of the triangle
t.findArea()

đầu ra

Enter side 1 : 3
Enter side 2 : 5
Enter side 3 : 4
Side 1 is 3.0
Side 2 is 5.0
Side 3 is 4.0
The area of the triangle is 6.00

Ở đây, chúng ta có thể thấy rằng mặc dù chúng ta không định nghĩa các phương thức như

I can eat
My name is  Rohu
1 hoặc
I can eat
My name is  Rohu
2 cho lớp
I can eat
My name is  Rohu
3 một cách riêng biệt, nhưng chúng ta vẫn có thể sử dụng chúng

Nếu một thuộc tính không được tìm thấy trong chính lớp đó, việc tìm kiếm sẽ tiếp tục đến lớp cơ sở. Điều này lặp lại một cách đệ quy, nếu chính lớp cơ sở được dẫn xuất từ ​​các lớp khác


Ghi đè phương thức trong Kế thừa Python

Trong ví dụ trước, chúng ta thấy đối tượng của lớp con có thể truy cập phương thức của lớp cha

Tuy nhiên, nếu cùng một phương thức xuất hiện trong cả lớp cha và lớp con thì sao?

Trong trường hợp này, phương thức trong lớp con sẽ ghi đè phương thức trong lớp cha. Khái niệm này được gọi là ghi đè phương thức trong Python

Ví dụ. Ghi đè phương thức

class Animal:

    # attributes and method of the parent class
    name = ""
    
    def eat(self):
        print("I can eat")

# inherit from Animal
class Dog(Animal):

    # override eat() method
    def eat(self):
        print("I like to eat bones")

# create an object of the subclass
labrador = Dog()

# call the eat() method on the labrador object
labrador.eat()

đầu ra

I like to eat bones

Trong ví dụ trên, cùng một phương thức

class Animal:

    # attribute and method of the parent class
    name = ""
    
    def eat(self):
        print("I can eat")

# inherit from Animal
class Dog(Animal):

    # new method in subclass
    def display(self):
        # access name attribute of superclass using self
        print("My name is ", self.name)

# create an object of the subclass
labrador = Dog()

# access superclass attribute and method 
labrador.name = "Rohu"
labrador.eat()

# call subclass method 
labrador.display()
6 có mặt trong cả lớp Chó và lớp Động vật

Bây giờ, khi chúng ta gọi phương thức

class Animal:

    # attribute and method of the parent class
    name = ""
    
    def eat(self):
        print("I can eat")

# inherit from Animal
class Dog(Animal):

    # new method in subclass
    def display(self):
        # access name attribute of superclass using self
        print("My name is ", self.name)

# create an object of the subclass
labrador = Dog()

# access superclass attribute and method 
labrador.name = "Rohu"
labrador.eat()

# call subclass method 
labrador.display()
6 bằng cách sử dụng đối tượng của lớp con Dog, phương thức của lớp Dog được gọi

Điều này là do phương thức

class Animal:

    # attribute and method of the parent class
    name = ""
    
    def eat(self):
        print("I can eat")

# inherit from Animal
class Dog(Animal):

    # new method in subclass
    def display(self):
        # access name attribute of superclass using self
        print("My name is ", self.name)

# create an object of the subclass
labrador = Dog()

# access superclass attribute and method 
labrador.name = "Rohu"
labrador.eat()

# call subclass method 
labrador.display()
6 của lớp con Dog ghi đè phương thức tương tự của lớp cha Animal


Phương thức super() trong Kế thừa Python

Trước đây chúng ta đã thấy rằng cùng một phương thức trong lớp con sẽ ghi đè phương thức trong lớp cha

Tuy nhiên, nếu chúng ta cần truy cập phương thức của lớp cha từ lớp con, chúng ta sử dụng phương thức

labrador.name = "Rohu"

labrador.eat()
5. Ví dụ,

class Animal:

    # attribute and method of the parent class
    name = ""
    
    def eat(self):
        print("I can eat")

# inherit from Animal
class Dog(Animal):

    # new method in subclass
    def display(self):
        # access name attribute of superclass using self
        print("My name is ", self.name)

# create an object of the subclass
labrador = Dog()

# access superclass attribute and method 
labrador.name = "Rohu"
labrador.eat()

# call subclass method 
labrador.display()
0

đầu ra

class Animal:

    # attribute and method of the parent class
    name = ""
    
    def eat(self):
        print("I can eat")

# inherit from Animal
class Dog(Animal):

    # new method in subclass
    def display(self):
        # access name attribute of superclass using self
        print("My name is ", self.name)

# create an object of the subclass
labrador = Dog()

# access superclass attribute and method 
labrador.name = "Rohu"
labrador.eat()

# call subclass method 
labrador.display()
1

Trong ví dụ trên, phương thức

class Animal:

    # attribute and method of the parent class
    name = ""
    
    def eat(self):
        print("I can eat")

# inherit from Animal
class Dog(Animal):

    # new method in subclass
    def display(self):
        # access name attribute of superclass using self
        print("My name is ", self.name)

# create an object of the subclass
labrador = Dog()

# access superclass attribute and method 
labrador.name = "Rohu"
labrador.eat()

# call subclass method 
labrador.display()
6 của lớp con Dog sẽ ghi đè phương thức tương tự của lớp cha Animal

Lớp con kế thừa gì từ lớp cha?

Một lớp con kế thừa tất cả các thành viên (trường, phương thức và lớp lồng nhau) từ lớp cha của nó. Các hàm tạo không phải là thành viên, vì vậy chúng không được kế thừa bởi các lớp con, nhưng hàm tạo của lớp cha có thể được gọi từ lớp con.

Lớp con kế thừa gì từ quizlet Python của lớp cha?

Lớp con kế thừa gì từ lớp cha của nó? . Hãy xem đoạn mã sau, đây là dòng đầu tiên của định nghĩa lớp. all the superclass's attributes. Look at the fo llowing code, which is the first line of a class definition.

Khi một lớp con kế thừa từ lớp cha, chúng ta sử dụng từ khóa nào sau đây?

Để kế thừa từ một lớp, hãy sử dụng từ khóa mở rộng .

Khi một lớp con được kế thừa từ chỉ một lớp cha thì nó được gọi là?

Độc thân. Sử dụng kế thừa duy nhất , một lớp con chỉ có thể kế thừa từ một lớp cha.