def __init__[bản thân, máy tính, ram, bộ nhớ]
# Đẳng cấp Di động kế thừa Máy tính
def __init__[bản thân, máy tính, ram, bộ lưu trữ, kiểu máy]
siêu[]. __init__[máy tính, ram, lưu trữ]
Apple = Mobile['Apple', 2, 64, 'iPhone X']
print['Điện thoại di động là. ', Quả táo. máy vi tính]
print['RAM la. ', Quả táo. đập]
print['Bộ nhớ là. ', Quả táo. kho]
print['Mô hình là. ', Quả táo. mô hình]
Ví dụ sau minh họa cách lớp dẫn xuất có thể gọi lớp cơ sở bằng cách sử dụng phương thức super[]
Thí dụ
Sao chépclass person:
def __init__[self, firstname, lastname]:
self.firstname = firstname
self.lastname = lastname
def fullname[self]:
print[firstname, ' ', lastname]
class student[person]:
def __init__[self, firstname, lastname, grade]:
self.grade = grade
super[].__init__[firstname, lastname] # calling base constructor
def display_details[]:
super[].fullname[] # calling base class method
print['Grade ', self.grade]
std = student['James', 'Bond', '10']
std.display_details[]
đầu ra
James Bond
Grade 10
Trong ví dụ trên, super[].__init__[firstname, lastname]
trong phương thức init của lớp student
gọi phương thức init của lớp người cơ sở và truyền tham số. Theo cách tương tự, super[].fullname[]
gọi phương thức person.fullname[]
. super[].fullname[]
có thể được gọi là
class person:
def __init__[self, firstname, lastname]:
self.firstname = firstname
self.lastname = lastname
def fullname[self]:
print[firstname, ' ', lastname]
class student[person]:
def __init__[self, firstname, lastname, grade]:
self.grade = grade
super[].__init__[firstname, lastname] # calling base constructor
def display_details[]:
super[].fullname[] # calling base class method
print['Grade ', self.grade]
std = student['James', 'Bond', '10']
std.display_details[]
0 Một lớp là một kế hoạch chi tiết hoặc nguyên mẫu do người dùng định nghĩa mà từ đó các đối tượng được tạo ra. Các lớp cung cấp phương tiện kết hợp dữ liệu và chức năng với nhau. Tạo một lớp mới sẽ tạo ra một loại đối tượng mới, cho phép tạo các thể hiện mới của loại đó
Thí dụ
# Python program to demonstrate
# classes
class
Inside Child class Inside Parent class0_______3_______1
Inside Child class Inside Parent class3_______3_______4
Inside Child class Inside Parent class3_______3_______6
Inside Child class Inside Parent class7
Inside Child class Inside Parent class8
Inside Child class Inside Parent class9
Inside Parent Inside Child0_______3_______8
Inside Parent Inside Child2
Inside Parent Inside Child3
Inside Parent Inside Child4
Inside Parent Inside Child0_______3_______8
Inside Parent Inside Child7
Inside Parent Inside Child3
Inside Parent Inside Child9
Inside Parent Inside Child0_______3_______8
Inside Parent Inside Child2
Inside Parent Inside Child3
Inside Parent Inside Child4
Inside Parent Inside Child5
Inside Child class Inside Parent class3_______12_______7
Inside Child class Inside Parent class3_______3_______6
HEY !!!!!! GfG I am initialised[Class GEG3] HEY !!!!!! GfG I am initialised[Class GEG2] HEY !!!!!! GfG I am initialised[Class GEG1] Printing from class GFG3: 10 Printing from class GFG2: 11 Printing from class GFG1: 120
HEY !!!!!! GfG I am initialised[Class GEG3] HEY !!!!!! GfG I am initialised[Class GEG2] HEY !!!!!! GfG I am initialised[Class GEG1] Printing from class GFG3: 10 Printing from class GFG2: 11 Printing from class GFG1: 121_______3_______8
HEY !!!!!! GfG I am initialised[Class GEG3] HEY !!!!!! GfG I am initialised[Class GEG2] HEY !!!!!! GfG I am initialised[Class GEG1] Printing from class GFG3: 10 Printing from class GFG2: 11 Printing from class GFG1: 123
Inside Parent Inside Child0_______32_______0
HEY !!!!!! GfG I am initialised[Class GEG3] HEY !!!!!! GfG I am initialised[Class GEG2] HEY !!!!!! GfG I am initialised[Class GEG1] Printing from class GFG3: 10 Printing from class GFG2: 11 Printing from class GFG1: 121
Inside Child class Inside Parent class8
HEY !!!!!! GfG I am initialised[Class GEG3] HEY !!!!!! GfG I am initialised[Class GEG2] HEY !!!!!! GfG I am initialised[Class GEG1] Printing from class GFG3: 10 Printing from class GFG2: 11 Printing from class GFG1: 128
Inside Child class Inside Parent class8
# Python program to demonstrate
0Inside Child class Inside Parent class8
# Python program to demonstrate
2
# Python program to demonstrate
4
# Python program to demonstrate
5
# Python program to demonstrate
6
Inside Parent Inside Child3
Inside Child class Inside Parent class0
HEY !!!!!! GfG I am initialised[Class GEG3] HEY !!!!!! GfG I am initialised[Class GEG2] HEY !!!!!! GfG I am initialised[Class GEG1] Printing from class GFG3: 10 Printing from class GFG2: 11 Printing from class GFG1: 121
# classes
0# classes
1# classes
2# classes
1# classes
0# classes
5# classes
6
HEY !!!!!! GfG I am initialised[Class GEG3] HEY !!!!!! GfG I am initialised[Class GEG2] HEY !!!!!! GfG I am initialised[Class GEG1] Printing from class GFG3: 10 Printing from class GFG2: 11 Printing from class GFG1: 120
# classes
8đầu ra
Geeks for Geeks
Gọi Phương thức lớp cha
Để hiểu về khái niệm lớp cha, bạn phải biết về Tính kế thừa trong Python. Nói một cách đơn giản hơn, kế thừa là khái niệm theo đó một lớp [thường được gọi là lớp con hoặc lớp con] kế thừa các thuộc tính từ một lớp khác [thường được gọi là Lớp cha hoặc lớp cha]
Nhưng bạn đã bao giờ thắc mắc về việc gọi các hàm được định nghĩa bên trong lớp cha với sự trợ giúp của lớp con chưa? . Bạn chỉ cần tạo một đối tượng của lớp con và gọi hàm của lớp cha bằng toán tử # classes
9
Thí dụ
0
1
class
4
Inside Child class Inside Parent class3_______81_______7
Inside Child class Inside Parent class3_______3_______6
0Inside Child class Inside Parent class8
HEY !!!!!! GfG I am initialised[Class GEG3] HEY !!!!!! GfG I am initialised[Class GEG2] HEY !!!!!! GfG I am initialised[Class GEG1] Printing from class GFG3: 10 Printing from class GFG2: 11 Printing from class GFG1: 123
Inside Parent Inside Child0_______32_______0
HEY !!!!!! GfG I am initialised[Class GEG3] HEY !!!!!! GfG I am initialised[Class GEG2] HEY !!!!!! GfG I am initialised[Class GEG1] Printing from class GFG3: 10 Printing from class GFG2: 11 Printing from class GFG1: 121
6# classes
5
9
class
class
1
class
2
Inside Child class Inside Parent class3_______83_______4
Inside Child class Inside Parent class3_______3_______6
class
7Inside Child class Inside Parent class8_______32_______3
Inside Parent Inside Child0_______32_______0
HEY !!!!!! GfG I am initialised[Class GEG3] HEY !!!!!! GfG I am initialised[Class GEG2] HEY !!!!!! GfG I am initialised[Class GEG1] Printing from class GFG3: 10 Printing from class GFG2: 11 Printing from class GFG1: 121
Inside Child class Inside Parent class03
# classes
5
Inside Child class Inside Parent class06
Inside Child class Inside Parent class07
Inside Parent Inside Child3
Inside Child class Inside Parent class09
Inside Child class Inside Parent class10
Inside Child class Inside Parent class12
Inside Child class Inside Parent class13
đầu ra
Inside Child class Inside Parent class
Gọi phương thức của lớp Parent sau khi ghi đè phương thức
Ghi đè phương thức là một khả năng của bất kỳ ngôn ngữ lập trình hướng đối tượng nào cho phép một lớp con hoặc lớp con cung cấp một triển khai cụ thể của một phương thức đã được cung cấp bởi một trong các lớp cha hoặc lớp cha của nó
Các phương thức của lớp cha cũng có thể được gọi trong các phương thức được ghi đè. Điều này thường có thể đạt được bằng hai cách