Gọi siêu phương thức trong python

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ép

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()

đầ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 class
0_______3_______1

 

Inside Child class
Inside Parent class
3_______3_______4

Inside Child class
Inside Parent class
3_______3_______6
Inside Child class
Inside Parent class
7
Inside Child class
Inside Parent class
8
Inside Child class
Inside Parent class
9

Inside Parent
Inside Child
0_______3_______8
Inside Parent
Inside Child
2
Inside Parent
Inside Child
3
Inside Parent
Inside Child
4

Inside Parent
Inside Child
0_______3_______8
Inside Parent
Inside Child
7
Inside Parent
Inside Child
3
Inside Parent
Inside Child
9

Inside Parent
Inside Child
0_______3_______8
Inside Parent
Inside Child
2
Inside Parent
Inside Child
3
Inside Parent
Inside Child
4

Inside Parent
Inside Child
5

Inside Child class
Inside Parent class
3_______12_______7

Inside Child class
Inside Parent class
3_______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: 12
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: 12
1_______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: 12
3

Inside Parent
Inside Child
0_______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: 12
1
Inside Child class
Inside Parent class
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: 12
8
Inside Child class
Inside Parent class
8# Python program to demonstrate0
Inside Child class
Inside Parent class
8# Python program to demonstrate2

 

# Python program to demonstrate4

# Python program to demonstrate5

# Python program to demonstrate6

Inside Parent
Inside Child
3
Inside Child class
Inside Parent class
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: 12
1# classes0# classes1# classes2# classes1# classes0# classes5

# classes6

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: 12
0# classes8

đầ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ử # classes9

Thí dụ




 0

 1

 

class  4

 

Inside Child class
Inside Parent class
3_______81_______7

Inside Child class
Inside Parent class
3_______3_______6  0
Inside Child class
Inside Parent class
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: 12
3

Inside Parent
Inside Child
0_______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: 12
1 6# classes5

 

 9

class class1

class2

Inside Child class
Inside Parent class
3_______83_______4

Inside Child class
Inside Parent class
3_______3_______6 class7
Inside Child class
Inside Parent class
8_______32_______3

Inside Parent
Inside Child
0_______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: 12
1
Inside Child class
Inside Parent class
03# classes5

 

Inside Child class
Inside Parent class
06

Inside Child class
Inside Parent class
07
Inside Parent
Inside Child
3
Inside Child class
Inside Parent class
09

Inside Child class
Inside Parent class
10

 

Inside Child class
Inside Parent class
12

Inside Child class
Inside Parent class
13

đầ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

Phương thức super()() gọi là gì?

Phương thức super() trong Python . Điều này rất hữu ích để truy cập các phương thức kế thừa đã bị ghi đè trong một lớp. Hay đơn giản hơn, nó dùng để gọi hàm tạo, i. e. phương thức __init__() của siêu lớp. The super method returns a proxy object that delegates method calls to a parent or sibling class of type. This is useful for accessing inherited methods that have been overridden in a class. Or simply, it is used to call the constructor, i.e. the __init__() method of the superclass.

Super() hoạt động như thế nào trong Python?

Hàm super() trong Python trả về các đối tượng được biểu diễn trong lớp của cha mẹ và rất hữu ích trong các kế thừa đa cấp và đa cấp để tìm lớp nào mà lớp con sẽ mở rộng trước

super() __ init __ trong Python là gì?

“__init__” là một phương thức dành riêng trong các lớp python . Nó được biết đến như một hàm tạo trong thuật ngữ Hướng đối tượng. Phương thức này khi được gọi sẽ cho phép lớp khởi tạo các thuộc tính của lớp. Python super() Hàm super() cho phép chúng ta tránh sử dụng tên lớp cơ sở một cách rõ ràng.

Python có tự động gọi super không?

Nếu có siêu lớp [init] mà bạn muốn gọi, bạn phải gọi chúng một cách rõ ràng bằng super(). __init__() đệ quy. Nếu không có [init] được xác định dọc theo đường dẫn cây, sẽ không có gì được gọi. Đây chỉ là một chức năng thông thường được tự động gọi một lần khi xây dựng .