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[]
1Ví 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 chaNgoà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[]
3là 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ụ,
- Xe hơi là phương tiện
- Táo là một loại trái cây
- 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
Ví 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ậtBâ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 AnimalPhươ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[]
5Trong 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