Kế thừa đơn với ví dụ 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 đang 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[]
0 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[]
1

Thí dụ. 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[]
2 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[]
3

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.

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

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

________số 8

đầ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[]
2 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[]
2 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[]
2 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

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. 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[]
4

đầ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[]
5

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[]
2 của lớp con Dog sẽ ghi đè phương thức tương tự của lớp cha Animal

Kế thừa duy nhất với ví dụ là gì?

Ví dụ về kế thừa đơn . Trong ví dụ dưới đây, lớp Dog kế thừa lớp Animal, vì vậy có một sự kế thừa duy nhất. When a class inherits another class, it is known as a single inheritance. In the example given below, Dog class inherits the Animal class, so there is the single inheritance.

Kế thừa đơn giản trong python là gì?

Tính kế thừa cho phép chúng ta định nghĩa một lớp kế thừa tất cả các phương thức và thuộc tính từ một lớp khác . Lớp cha là lớp được kế thừa từ đó, còn được gọi là lớp cơ sở. Lớp con là lớp kế thừa từ lớp khác, còn gọi là lớp dẫn xuất.

Kế thừa một cấp có nghĩa là gì trong python?

Trong kế thừa đơn python, một lớp dẫn xuất chỉ được dẫn xuất từ ​​một lớp cha duy nhất và cho phép lớp đó lấy các hành vi và thuộc tính từ một lớp cơ sở duy nhất. This enables code reusability of a parent class, and adding new features to a class makes code more readable, elegant and less redundant.

Sự kế thừa trong ví dụ thực tế về python là gì?

Kế thừa trong Python . Giả sử tồn tại một lớp “Trái cây”, và bạn lấy từ nó để tạo một lớp mới có tên là “Apple”. Mối quan hệ đơn giản giữa hai lớp nói rằng “Apple” là “Fruit”.

Chủ Đề