Tính kế thừa (kế thừa) là một trong những tính chất quan trọng của lập trình hướng đối tượng (lập trình hướng đối tượng). Tính chất của đề tài này liên quan đến việc định nghĩa một lớp mới (lớp mới) dựa trên lớp cũ đã có (lớp hiện có). Lớp mới được gọi là lớp dẫn xuất (lớp dẫn xuất) hay lớp con (lớp con). Các lớp đã được gọi là lớp cơ sở cũ (lớp cơ sở) hoặc lớp cha (lớp cha) 1. 1. Lớp con và lớp cha trong PythonPython cũng là một ngôn ngữ lập trình hướng đối tượng nên cũng hỗ trợ tính kế thừa. Bất kỳ lớp nào trong Python cũng có thể là một lớp cơ sở. Cú kế thừa trong Python class BaseClass:
# Body of base class
class DerivedClass(BaseClass):
# Body of derived class
DerivedClass sẽ kế thừa các thuộc tính và phương thức từ BaseClass. Việc này chủ yếu là để tái sử dụng mã. Hơn nữa, DerivedClass có thể định nghĩa thêm các thuộc tính và phương thức của chính nó Ví dụ tạo một lớp Cha Personclass Person:
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(self.name + ",", self.age, "years old.")
# create objects of Person class
john = Person("John", 36)
john.info()
marry = Person("Marry", 35)
marry.info()
Ví dụ Lớp con Sinh viên kế thừa Lớp cha Ngườiclass Person:
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(self.name + ",", self.age, "years old.")
# Student inherits from Person
# without add any other properties or methods
class Student(Person):
pass
# create an object of Student class
kane = Student("Kane", 29)
kane.info()
Kết quảKane, 29 years old.
Trong ví dụ trên, Lớp Sinh viên kế thừa Lớp Người mà không bổ sung bất kỳ thuộc tính và phương thức nào cho Lớp Sinh viên riêng. Tức là, Lớp Sinh viên có cùng thuộc tính và phương thức như Lớp Người Có những trường hợp lớp con không định nghĩa được gì như Lớp Sinh viên. Khi tạo một đối tượng của lớp Student thì Python sẽ gọi hàm class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(self.name + ",", self.age, "years old.")
# create objects of Person class
john = Person("John", 36)
john.info()
marry = Person("Marry", 35)
marry.info()
2 của lớp Person mà lớp Student kế thừa được1. 2. Định nghĩa hàm __init__() trong lớp conChúng ta có thể định nghĩa lại hàm khởi tạo class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(self.name + ",", self.age, "years old.")
# create objects of Person class
john = Person("John", 36)
john.info()
marry = Person("Marry", 35)
marry.info()
2 trong lớp con. Lúc này, hàm class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(self.name + ",", self.age, "years old.")
# create objects of Person class
john = Person("John", 36)
john.info()
marry = Person("Marry", 35)
marry.info()
2 trong lớp con sẽ ghi đè (overriding) lên hàm class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(self.name + ",", self.age, "years old.")
# create objects of Person class
john = Person("John", 36)
john.info()
marry = Person("Marry", 35)
marry.info()
2 kế thừa được từ lớp cha________số 8Trong ví dụ trên, chúng ta gọi lại hàm class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(self.name + ",", self.age, "years old.")
# create objects of Person class
john = Person("John", 36)
john.info()
marry = Person("Marry", 35)
marry.info()
2 của lớp cho Person trong lớp Student để đảm bảo tính kế thừa. If not used class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(self.name + ",", self.age, "years old.")
# create objects of Person class
john = Person("John", 36)
john.info()
marry = Person("Marry", 35)
marry.info()
7, they we could used class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(self.name + ",", self.age, "years old.")
# create objects of Person class
john = Person("John", 36)
john.info()
marry = Person("Marry", 35)
marry.info()
8class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(self.name + ",", self.age, "years old.")
# create objects of Person class
john = Person("John", 36)
john.info()
marry = Person("Marry", 35)
marry.info()
2Hàm class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(self.name + ",", self.age, "years old.")
# create objects of Person class
john = Person("John", 36)
john.info()
marry = Person("Marry", 35)
marry.info()
9 giúp lớp con tự động kế thừa các thuộc tính và phương thức từ lớp cha1. 3. Lớp con có thuộc tính và phương thức riêngLớp con thường có các thuộc tính và phương thức riêng của nó class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(self.name + ",", self.age, "years old.")
# create objects of Person class
john = Person("John", 36)
john.info()
marry = Person("Marry", 35)
marry.info()
4Kết quảclass Person:
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(self.name + ",", self.age, "years old.")
# create objects of Person class
john = Person("John", 36)
john.info()
marry = Person("Marry", 35)
marry.info()
5Trong ví dụ trên, Lớp Sinh viên kế thừa từ Lớp Người. Và lớp Học sinh có thêm thuộc tính id và phương thức class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(self.name + ",", self.age, "years old.")
# Student inherits from Person
# without add any other properties or methods
class Student(Person):
pass
# create an object of Student class
kane = Student("Kane", 29)
kane.info()
02. Ghi đè phương thức trong PythonLớp con có thể định nghĩa lại các phương thức được kế thừa từ lớp cha. That was call is method overriding class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(self.name + ",", self.age, "years old.")
# create objects of Person class
john = Person("John", 36)
john.info()
marry = Person("Marry", 35)
marry.info()
7Kết quảclass Person:
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(self.name + ",", self.age, "years old.")
# create objects of Person class
john = Person("John", 36)
john.info()
marry = Person("Marry", 35)
marry.info()
5Trong ví dụ trên, Lớp Sinh viên kế thừa phương thức class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(self.name + ",", self.age, "years old.")
# Student inherits from Person
# without add any other properties or methods
class Student(Person):
pass
# create an object of Student class
kane = Student("Kane", 29)
kane.info()
1 từ Lớp Người. Và lớp Học sinh đã định nghĩa lại phương thức class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(self.name + ",", self.age, "years old.")
# Student inherits from Person
# without add any other properties or methods
class Student(Person):
pass
# create an object of Student class
kane = Student("Kane", 29)
kane.info()
1 cho riêng nó3. Sử dụng hàm isinstance() và issubclass() trong PythonHàm class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(self.name + ",", self.age, "years old.")
# Student inherits from Person
# without add any other properties or methods
class Student(Person):
pass
# create an object of Student class
kane = Student("Kane", 29)
kane.info()
3 giúp kiểm tra một đối tượng có phải là một thực thể của một lớp hay không. Hàm class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(self.name + ",", self.age, "years old.")
# Student inherits from Person
# without add any other properties or methods
class Student(Person):
pass
# create an object of Student class
kane = Student("Kane", 29)
kane.info()
4 giúp kiểm tra một lớp có phải là lớp con của một lớp khác hay không. Hàm class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(self.name + ",", self.age, "years old.")
# Student inherits from Person
# without add any other properties or methods
class Student(Person):
pass
# create an object of Student class
kane = Student("Kane", 29)
kane.info()
5 return data type of a variable in Pythonclass Person:
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(self.name + ",", self.age, "years old.")
# create objects of Person class
john = Person("John", 36)
john.info()
marry = Person("Marry", 35)
marry.info()
0Kết quảclass Person:
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(self.name + ",", self.age, "years old.")
# create objects of Person class
john = Person("John", 36)
john.info()
marry = Person("Marry", 35)
marry.info()
1Chúng ta thấy Lớp Sinh viên kế thừa từ Lớp Người. Do đó, các đối tượng của lớp Student cũng chính là các đối tượng của Person. The other object of the Person is not a Student |