Hướng dẫn what is a sub class in python? - một lớp con trong python là gì?

Cho đến bây giờ, bạn đã học về các lớp học và đối tượng. Trong chương này, bạn sẽ tìm hiểu một khái niệm mới về lớp con- lớp con theo định hướng đối tượng.

Lớp con Python


Để hiểu một lớp con là gì, hãy để nhìn vào một ví dụ.

Giả sử chúng ta xác định một hình chữ nhật với một số chiều dài và chiều rộng. Bây giờ, một hình vuông cũng là một hình chữ nhật nhưng có cùng chiều dài và chiều rộng.

Từ đó, bạn phải có cảm giác rằng một hình vuông là một lớp con của hình chữ nhật.

Hãy cùng nghĩ về một số ví dụ khác về các lớp con. Một lớp ‘cuốn sách có thể có‘ Sciencebook, và ‘Mathbook, là các lớp con của nó. Một lớp khác ‘Động vật có thể có‘ Chó, ‘Cat, và‘ Dê, là các lớp con của nó.

Các lớp có phân lớp đã được thực hiện được gọi là một siêu lớp. Các tên khác của siêu lớp là lớp cơ sở hoặc lớp cha và các tên khác của lớp con là lớp có nguồn gốc hoặc lớp con. Trong ví dụ hình chữ nhật của chúng tôi, hình chữ nhật là siêu lớp và hình vuông là lớp con của nó. Quá trình tạo một lớp con của một lớp được gọi là kế thừa.superclass. Other names of superclass are base class or parent class, and other names of subclass are derived class or child class. In our Rectangle example, Rectangle is the superclass and Square is its subclass. The process of creating a subclass of a class is called inheritance.

Tất cả các thuộc tính và phương pháp của siêu lớp cũng được kế thừa bởi lớp con của nó. Điều này có nghĩa là một đối tượng của một lớp con có thể truy cập tất cả các thuộc tính và phương thức của siêu lớp. Hơn nữa, lớp con cũng có thể có các thuộc tính hoặc phương thức riêng ngoài các thuộc tính được kế thừa. & NBSP;

Hướng dẫn what is a sub class in python? - một lớp con trong python là gì?

Chúng ta hãy học cách tạo một lớp con trong Python.

# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
emp = Employee()  # creating object of subclass
 
emp.display1()
emp.display2()

Đầu ra

Đây là siêu lớp Đây là lớp con
This is subclass

Chúng tôi đã tạo hai lớp Person

# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
p = Person()  # creating object of superclass
 
p.display1()
p.display2()
0 với một phương thức trong mỗi lớp.

# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
p = Person()  # creating object of superclass
 
p.display1()
p.display2()
1 → nó có nghĩa là lớp
# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
p = Person()  # creating object of superclass
 
p.display1()
p.display2()
0 được kế thừa từ lớp Person. Nói một cách đơn giản,
# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
p = Person()  # creating object of superclass
 
p.display1()
p.display2()
0 là một lớp con của Person. (Một nhân viên là một người)

# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
p = Person()  # creating object of superclass
 
p.display1()
p.display2()
0 là một lớp con của Person, nó kế thừa phương thức
# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
p = Person()  # creating object of superclass
 
p.display1()
p.display2()
8 của siêu lớp. & NBSP;

# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
p = Person()  # creating object of superclass
 
p.display1()
p.display2()
9 → Chúng tôi đã tạo một đối tượng
# superclass
class Person():
    def __init__(self, per_name, per_age):
        self.name = per_name
        self.age = per_age
		
    def display1(self):
        print("In superclass method: name:", self.name, "age:", self.age)
 
# subclass		
class Employee(Person):
    def display2(self):
        print("In subclass method: name:", self.name, "age:", self.age)
		
emp = Employee("John", 20)  # creating object of superclass
 
emp.display1()
emp.display2()
print("Outside both methods: name:", emp.name, "age:", emp.age)
0 của lớp
# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
p = Person()  # creating object of superclass
 
p.display1()
p.display2()
0.

# superclass
class Person():
    def __init__(self, per_name, per_age):
        self.name = per_name
        self.age = per_age
		
    def display1(self):
        print("In superclass method: name:", self.name, "age:", self.age)
 
# subclass		
class Employee(Person):
    def display2(self):
        print("In subclass method: name:", self.name, "age:", self.age)
		
emp = Employee("John", 20)  # creating object of superclass
 
emp.display1()
emp.display2()
print("Outside both methods: name:", emp.name, "age:", emp.age)
2 → Một đối tượng của lớp con có thể truy cập bất kỳ thành viên nào (thuộc tính hoặc phương thức) của siêu lớp, do đó
# superclass
class Person():
    def __init__(self, per_name, per_age):
        self.name = per_name
        self.age = per_age
		
    def display1(self):
        print("In superclass method: name:", self.name, "age:", self.age)
 
# subclass		
class Employee(Person):
    def display2(self):
        print("In subclass method: name:", self.name, "age:", self.age)
		
emp = Employee("John", 20)  # creating object of superclass
 
emp.display1()
emp.display2()
print("Outside both methods: name:", emp.name, "age:", emp.age)
0 có thể gọi phương thức
# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
p = Person()  # creating object of superclass
 
p.display1()
p.display2()
8 của lớp cha ____.

Vì vậy, đây là một triển khai đơn giản của một lớp con. Hãy để thử một cái gì đó nhiều hơn.

Chúng tôi biết rằng một đối tượng của lớp con có thể truy cập các thuộc tính hoặc phương thức của lớp cha. Nhưng điều ngược lại không đúng, tức là, một đối tượng của lớp cha có thể truy cập các thuộc tính hoặc phương thức của lớp con.

Hãy cùng xem những gì xảy ra khi một đối tượng của lớp Person cố gắng truy cập vào phương pháp của lớp con

# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
p = Person()  # creating object of superclass
 
p.display1()
p.display2()
0 của nó.

# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
p = Person()  # creating object of superclass
 
p.display1()
p.display2()

Đầu ra

Đây là siêu lớp Đây là lớp con

Chúng tôi đã tạo hai lớp Person

# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
p = Person()  # creating object of superclass
 
p.display1()
p.display2()
0 với một phương thức trong mỗi lớp.

# superclass
class Person():
    def __init__(self, per_name, per_age):
        self.name = per_name
        self.age = per_age
		
    def display1(self):
        print("In superclass method: name:", self.name, "age:", self.age)
 
# subclass		
class Employee(Person):
    def display2(self):
        print("In subclass method: name:", self.name, "age:", self.age)
		
emp = Employee("John", 20)  # creating object of superclass
 
emp.display1()
emp.display2()
print("Outside both methods: name:", emp.name, "age:", emp.age)

Đầu ra

Đây là siêu lớp Đây là lớp con
In subclass method: name: John age: 20
Outside both methods: name: John age: 20

Chúng tôi đã tạo hai lớp Person

# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
p = Person()  # creating object of superclass
 
p.display1()
p.display2()
0 với một phương thức trong mỗi lớp.

# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
p = Person()  # creating object of superclass
 
p.display1()
p.display2()
1 → nó có nghĩa là lớp
# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
p = Person()  # creating object of superclass
 
p.display1()
p.display2()
0 được kế thừa từ lớp Person. Nói một cách đơn giản,
# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
p = Person()  # creating object of superclass
 
p.display1()
p.display2()
0 là một lớp con của Person. (Một nhân viên là một người)

# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
p = Person()  # creating object of superclass
 
p.display1()
p.display2()
0 là một lớp con của Person, nó kế thừa phương thức
# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
p = Person()  # creating object of superclass
 
p.display1()
p.display2()
8 của siêu lớp. & NBSP;

# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
p = Person()  # creating object of superclass
 
p.display1()
p.display2()
9 → Chúng tôi đã tạo một đối tượng
# superclass
class Person():
    def __init__(self, per_name, per_age):
        self.name = per_name
        self.age = per_age
		
    def display1(self):
        print("In superclass method: name:", self.name, "age:", self.age)
 
# subclass		
class Employee(Person):
    def display2(self):
        print("In subclass method: name:", self.name, "age:", self.age)
		
emp = Employee("John", 20)  # creating object of superclass
 
emp.display1()
emp.display2()
print("Outside both methods: name:", emp.name, "age:", emp.age)
0 của lớp
# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
p = Person()  # creating object of superclass
 
p.display1()
p.display2()
0.

# superclass
class Person():
    def __init__(self, per_name, per_age):
        self.name = per_name
        self.age = per_age
		
    def display1(self):
        print("In superclass method: name:", self.name, "age:", self.age)
 
# subclass		
class Employee(Person):
    def display2(self):
        print("In subclass method: name:", self.name, "age:", self.age)
		
emp = Employee("John", 20)  # creating object of superclass
 
emp.display1()
emp.display2()
print("Outside both methods: name:", emp.name, "age:", emp.age)
2 → Một đối tượng của lớp con có thể truy cập bất kỳ thành viên nào (thuộc tính hoặc phương thức) của siêu lớp, do đó
# superclass
class Person():
    def __init__(self, per_name, per_age):
        self.name = per_name
        self.age = per_age
		
    def display1(self):
        print("In superclass method: name:", self.name, "age:", self.age)
 
# subclass		
class Employee(Person):
    def display2(self):
        print("In subclass method: name:", self.name, "age:", self.age)
		
emp = Employee("John", 20)  # creating object of superclass
 
emp.display1()
emp.display2()
print("Outside both methods: name:", emp.name, "age:", emp.age)
0 có thể gọi phương thức
# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
p = Person()  # creating object of superclass
 
p.display1()
p.display2()
8 của lớp cha ____.

Vì vậy, đây là một triển khai đơn giản của một lớp con. Hãy để thử một cái gì đó nhiều hơn.

Chúng tôi biết rằng một đối tượng của lớp con có thể truy cập các thuộc tính hoặc phương thức của lớp cha. Nhưng điều ngược lại không đúng, tức là, một đối tượng của lớp cha có thể truy cập các thuộc tính hoặc phương thức của lớp con.

Hãy cùng xem những gì xảy ra khi một đối tượng của lớp Person cố gắng truy cập vào phương pháp của lớp con

# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
p = Person()  # creating object of superclass
 
p.display1()
p.display2()
0 của nó.

Chúng tôi đã tạo một đối tượng

# superclass
class Person():
    def __init__(self, per_name, per_age):
        self.name = per_name
        self.age = per_age
		
    def display1(self):
        print("In superclass method: name:", self.name, "age:", self.age)
 
# subclass		
class Employee(Person):
    def display2(self):
        print("In subclass method: name:", self.name, "age:", self.age)
		
emp = Employee("John", 20)  # creating object of superclass
 
emp.display1()
emp.display2()
print("Outside both methods: name:", emp.name, "age:", emp.age)
8 của lớp cha Person. Đối tượng này được gọi là phương thức
# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
p = Person()  # creating object of superclass
 
p.display1()
p.display2()
8 thành công in một tin nhắn. Nhưng khi nó cố gắng gọi phương thức
# superclass
class Person():
    def __init__(self, per_name, per_age):
        self.name = per_name
        self.age = per_age
 
# subclass		
class Employee(Person):
    def __init__(self, emp_name, emp_age, emp_salary):
        self.salary = emp_salary
        Person.__init__(self, emp_name, emp_age)
		
emp = Employee("John", 20, 8000)  # creating object of superclass
 
print("name:", emp.name, "age:", emp.age, "salary:", emp.salary)
1 của lớp con của nó, chúng tôi đã gặp lỗi như mong đợi.

Nhìn vào ví dụ tiếp theo.

Trong Siêu lớp Phương pháp: Tên: John Age: 20 trong Phương pháp phân lớp: Tên: John Age: 20 Bên ngoài cả hai Phương pháp: Tên: John Age: 20

# superclass
class Person():
    def __init__(self, per_name, per_age):
        self.name = per_name
        self.age = per_age
 
# subclass		
class Employee(Person):
    def __init__(self, emp_name, emp_age, emp_salary):
        self.salary = emp_salary
        Person.__init__(self, emp_name, emp_age)
		
emp = Employee("John", 20, 8000)  # creating object of superclass
 
print("name:", emp.name, "age:", emp.age, "salary:", emp.salary)

Đầu ra

Tên: John Age: 20 Mức lương: 8000

Chúng tôi đã tạo một đối tượng

# superclass
class Person():
    def __init__(self, per_name, per_age):
        self.name = per_name
        self.age = per_age
		
    def display1(self):
        print("In superclass method: name:", self.name, "age:", self.age)
 
# subclass		
class Employee(Person):
    def display2(self):
        print("In subclass method: name:", self.name, "age:", self.age)
		
emp = Employee("John", 20)  # creating object of superclass
 
emp.display1()
emp.display2()
print("Outside both methods: name:", emp.name, "age:", emp.age)
0 của lớp con
# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
p = Person()  # creating object of superclass
 
p.display1()
p.display2()
0 bằng cách chuyển các giá trị của John John, 20 và 8000 cho các tham số
# superclass
class Person():
    pass

# subclass		
class Employee(Person):
    pass

per = Person()  # creating object of superclass
emp = Employee()  # creating object of subclass

print(isinstance(per, Person))
print(isinstance(per, Employee))
print(isinstance(emp, Person))
print(isinstance(emp, Employee))
8,
# superclass
class Person():
    pass

# subclass		
class Employee(Person):
    pass

per = Person()  # creating object of superclass
emp = Employee()  # creating object of subclass

print(isinstance(per, Person))
print(isinstance(per, Employee))
print(isinstance(emp, Person))
print(isinstance(emp, Employee))
9 và
# superclass
class Person():
    pass

# subclass		
class Employee(Person):
    pass

print(issubclass(Person, Employee))
print(issubclass(Employee, Person))
0 của hàm tạo của lớp con (vì lớp con có một hàm tạo của chính nó). Trong hàm tạo này, giá trị của
# superclass
class Person():
    pass

# subclass		
class Employee(Person):
    pass

print(issubclass(Person, Employee))
print(issubclass(Employee, Person))
0 được gán cho thuộc tính
# superclass
class Person():
    pass

# subclass		
class Employee(Person):
    pass

print(issubclass(Person, Employee))
print(issubclass(Employee, Person))
2 của đối tượng
# superclass
class Person():
    def __init__(self, per_name, per_age):
        self.name = per_name
        self.age = per_age
		
    def display1(self):
        print("In superclass method: name:", self.name, "age:", self.age)
 
# subclass		
class Employee(Person):
    def display2(self):
        print("In subclass method: name:", self.name, "age:", self.age)
		
emp = Employee("John", 20)  # creating object of superclass
 
emp.display1()
emp.display2()
print("Outside both methods: name:", emp.name, "age:", emp.age)
0 và hàm tạo của lớp cha được gọi bằng cách truyền
# superclass
class Person():
    pass

# subclass		
class Employee(Person):
    pass

per = Person()  # creating object of superclass
emp = Employee()  # creating object of subclass

print(isinstance(per, Person))
print(isinstance(per, Employee))
print(isinstance(emp, Person))
print(isinstance(emp, Employee))
8 và
# superclass
class Person():
    pass

# subclass		
class Employee(Person):
    pass

per = Person()  # creating object of superclass
emp = Employee()  # creating object of subclass

print(isinstance(per, Person))
print(isinstance(per, Employee))
print(isinstance(emp, Person))
print(isinstance(emp, Employee))
9 làm đối số. Trong hàm tạo của lớp cha, các thuộc tính
# superclass
class Person():
    def __init__(self, per_name, per_age):
        self.name = per_name
        self.age = per_age
 
# subclass		
class Employee(Person):
    def __init__(self, emp_name, emp_age, emp_salary):
        self.salary = emp_salary
        Person.__init__(self, emp_name, emp_age)
		
emp = Employee("John", 20, 8000)  # creating object of superclass
 
print("name:", emp.name, "age:", emp.age, "salary:", emp.salary)
5 và
# superclass
class Person():
    def __init__(self, per_name, per_age):
        self.name = per_name
        self.age = per_age
 
# subclass		
class Employee(Person):
    def __init__(self, emp_name, emp_age, emp_salary):
        self.salary = emp_salary
        Person.__init__(self, emp_name, emp_age)
		
emp = Employee("John", 20, 8000)  # creating object of superclass
 
print("name:", emp.name, "age:", emp.age, "salary:", emp.salary)
6 trở thành John John và 20 cho đối tượng
# superclass
class Person():
    def __init__(self, per_name, per_age):
        self.name = per_name
        self.age = per_age
		
    def display1(self):
        print("In superclass method: name:", self.name, "age:", self.age)
 
# subclass		
class Employee(Person):
    def display2(self):
        print("In subclass method: name:", self.name, "age:", self.age)
		
emp = Employee("John", 20)  # creating object of superclass
 
emp.display1()
emp.display2()
print("Outside both methods: name:", emp.name, "age:", emp.age)
0.

Lưu ý rằng hàm tạo của lớp cha Person được gọi từ hàm tạo của lớp con bằng cách viết

class Rectangle():
    def __init__(self,leng,br):
        self.length = leng
        self.breadth = br
    '''while calling a method in a class python
    automatically passes an instance( object ) of it.
    so we have to pass sef in area i.e. area(self)'''
    def area(self):
        '''length and breadth are not globally defined.
        So, we have to access them as self.length'''
        return self.length*self.breadth
class Square(Rectangle):
    def __init__(self,side):
        Rectangle.__init__(self,side,side)
        self.side = side
s = Square(4)
print(s.length)
print(s.breadth)
print(s.side)
print(s.area())# It appears as nothing is passed but python will pass an instance of class.
0.

Hãy cùng xem một ví dụ nữa.

# superclass
class Person():
    def __init__(self, per_name, per_age):
        self.name = per_name
        self.age = per_age
	
    def display1(self):
        print("name:", self.name)
        print("age:", self.age)

# subclass		
class Employee(Person):
    def __init__(self, emp_name, emp_age, emp_salary):
        self.salary = emp_salary
        Person.__init__(self, emp_name, emp_age)
	
    def display2(self):
        print("salary:", self.salary)
        Person.display1(self)
		
emp = Employee("John", 20, 8000)  # creating object of superclass

emp.display2()

Đầu ra

Mức lương: 8000 Tên: John Age: 20
name: John
age: 20

Bạn phải hiểu chương trình này. Chúng tôi đang gọi hàm tạo của lớp cha Person bên trong hàm tạo của lớp con bằng cách viết

class Rectangle():
    def __init__(self,leng,br):
        self.length = leng
        self.breadth = br
    '''while calling a method in a class python
    automatically passes an instance( object ) of it.
    so we have to pass sef in area i.e. area(self)'''
    def area(self):
        '''length and breadth are not globally defined.
        So, we have to access them as self.length'''
        return self.length*self.breadth
class Square(Rectangle):
    def __init__(self,side):
        Rectangle.__init__(self,side,side)
        self.side = side
s = Square(4)
print(s.length)
print(s.breadth)
print(s.side)
print(s.area())# It appears as nothing is passed but python will pass an instance of class.
0 và gọi phương thức
# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
p = Person()  # creating object of superclass
 
p.display1()
p.display2()
8 của lớp cha trong phương thức
# superclass
class Person():
    def __init__(self, per_name, per_age):
        self.name = per_name
        self.age = per_age
 
# subclass		
class Employee(Person):
    def __init__(self, emp_name, emp_age, emp_salary):
        self.salary = emp_salary
        Person.__init__(self, emp_name, emp_age)
		
emp = Employee("John", 20, 8000)  # creating object of superclass
 
print("name:", emp.name, "age:", emp.age, "salary:", emp.salary)
1 của lớp con bằng cách viết
class Rectangle():
    def __init__(self,leng,br):
        self.length = leng
        self.breadth = br
    '''while calling a method in a class python
    automatically passes an instance( object ) of it.
    so we have to pass sef in area i.e. area(self)'''
    def area(self):
        '''length and breadth are not globally defined.
        So, we have to access them as self.length'''
        return self.length*self.breadth
class Square(Rectangle):
    def __init__(self,side):
        Rectangle.__init__(self,side,side)
        self.side = side
s = Square(4)
print(s.length)
print(s.breadth)
print(s.side)
print(s.area())# It appears as nothing is passed but python will pass an instance of class.
5.

Hàm python siêu ()


Trong ví dụ trước, thay vì

class Rectangle():
    def __init__(self,leng,br):
        self.length = leng
        self.breadth = br
    '''while calling a method in a class python
    automatically passes an instance( object ) of it.
    so we have to pass sef in area i.e. area(self)'''
    def area(self):
        '''length and breadth are not globally defined.
        So, we have to access them as self.length'''
        return self.length*self.breadth
class Square(Rectangle):
    def __init__(self,side):
        Rectangle.__init__(self,side,side)
        self.side = side
s = Square(4)
print(s.length)
print(s.breadth)
print(s.side)
print(s.area())# It appears as nothing is passed but python will pass an instance of class.
6, chúng ta có thể sử dụng hàm
class Rectangle():
    def __init__(self,leng,br):
        self.length = leng
        self.breadth = br
    '''while calling a method in a class python
    automatically passes an instance( object ) of it.
    so we have to pass sef in area i.e. area(self)'''
    def area(self):
        '''length and breadth are not globally defined.
        So, we have to access them as self.length'''
        return self.length*self.breadth
class Square(Rectangle):
    def __init__(self,side):
        Rectangle.__init__(self,side,side)
        self.side = side
s = Square(4)
print(s.length)
print(s.breadth)
print(s.side)
print(s.area())# It appears as nothing is passed but python will pass an instance of class.
7 để gọi hàm tạo và phương thức của lớp cha trong lớp con. & NBSP;

Hàm

class Rectangle():
    def __init__(self,leng,br):
        self.length = leng
        self.breadth = br
    '''while calling a method in a class python
    automatically passes an instance( object ) of it.
    so we have to pass sef in area i.e. area(self)'''
    def area(self):
        '''length and breadth are not globally defined.
        So, we have to access them as self.length'''
        return self.length*self.breadth
class Square(Rectangle):
    def __init__(self,side):
        Rectangle.__init__(self,side,side)
        self.side = side
s = Square(4)
print(s.length)
print(s.breadth)
print(s.side)
print(s.area())# It appears as nothing is passed but python will pass an instance of class.
7 trả về một đối tượng lớp cha và có thể được sử dụng để truy cập các thuộc tính hoặc phương thức của lớp cha trong lớp con.

Hãy để viết lại ví dụ cuối cùng bằng cách sử dụng

class Rectangle():
    def __init__(self,leng,br):
        self.length = leng
        self.breadth = br
    '''while calling a method in a class python
    automatically passes an instance( object ) of it.
    so we have to pass sef in area i.e. area(self)'''
    def area(self):
        '''length and breadth are not globally defined.
        So, we have to access them as self.length'''
        return self.length*self.breadth
class Square(Rectangle):
    def __init__(self,side):
        Rectangle.__init__(self,side,side)
        self.side = side
s = Square(4)
print(s.length)
print(s.breadth)
print(s.side)
print(s.area())# It appears as nothing is passed but python will pass an instance of class.
7.

# superclass
class Person():
    def __init__(self, per_name, per_age):
        self.name = per_name
        self.age = per_age
	
    def display1(self):
        print("name:", self.name)
        print("age:", self.age)

# subclass		
class Employee(Person):
    def __init__(self, emp_name, emp_age, emp_salary):
        self.salary = emp_salary
        super().__init__(emp_name, emp_age)
	
    def display2(self):
        print("salary:", self.salary)
        super().display1()
		
emp = Employee("John", 20, 8000)  # creating object of subclass

emp.display2()

Đầu ra

Trong ví dụ này, chúng tôi đã thay thế

class Rectangle():
    def __init__(self,leng,br):
        self.length = leng
        self.breadth = br
    '''while calling a method in a class python
    automatically passes an instance( object ) of it.
    so we have to pass sef in area i.e. area(self)'''
    def area(self):
        '''length and breadth are not globally defined.
        So, we have to access them as self.length'''
        return self.length*self.breadth
class Square(Rectangle):
    def __init__(self,side):
        Rectangle.__init__(self,side,side)
        self.side = side
s = Square(4)
print(s.length)
print(s.breadth)
print(s.side)
print(s.area())# It appears as nothing is passed but python will pass an instance of class.
0 bằng Person1 và
class Rectangle():
    def __init__(self,leng,br):
        self.length = leng
        self.breadth = br
    '''while calling a method in a class python
    automatically passes an instance( object ) of it.
    so we have to pass sef in area i.e. area(self)'''
    def area(self):
        '''length and breadth are not globally defined.
        So, we have to access them as self.length'''
        return self.length*self.breadth
class Square(Rectangle):
    def __init__(self,side):
        Rectangle.__init__(self,side,side)
        self.side = side
s = Square(4)
print(s.length)
print(s.breadth)
print(s.side)
print(s.area())# It appears as nothing is passed but python will pass an instance of class.
5 bằng Person3 bên trong lớp con.

Khi gọi một phương thức bằng cách sử dụng

class Rectangle():
    def __init__(self,leng,br):
        self.length = leng
        self.breadth = br
    '''while calling a method in a class python
    automatically passes an instance( object ) of it.
    so we have to pass sef in area i.e. area(self)'''
    def area(self):
        '''length and breadth are not globally defined.
        So, we have to access them as self.length'''
        return self.length*self.breadth
class Square(Rectangle):
    def __init__(self,side):
        Rectangle.__init__(self,side,side)
        self.side = side
s = Square(4)
print(s.length)
print(s.breadth)
print(s.side)
print(s.area())# It appears as nothing is passed but python will pass an instance of class.
7, không cần phải tự mình là đối số đầu tiên.

Vì vậy, đó là tất cả những gì bạn cần biết về các lớp con. Sau chương này, thực hành các câu hỏi về chủ đề này để có một sự nắm giữ tốt về nó.

Có hai hàm được xây dựng trong Python, cụ thể là Person5 và Person6, có thể được sử dụng để kiểm tra lớp của một đối tượng hoặc lớp con của một lớp. & NBSP;

Python isinstance ()


Hàm này được sử dụng để kiểm tra xem một đối tượng là một thể hiện của một lớp cụ thể. Nói cách khác, nó kiểm tra xem một đối tượng thuộc về một lớp cụ thể.

# superclass
class Person():
    pass

# subclass		
class Employee(Person):
    pass

per = Person()  # creating object of superclass
emp = Employee()  # creating object of subclass

print(isinstance(per, Person))
print(isinstance(per, Employee))
print(isinstance(emp, Person))
print(isinstance(emp, Employee))

Đầu ra

Ở đây, Person7 là một đối tượng của siêu lớp Person

# superclass
class Person():
    def __init__(self, per_name, per_age):
        self.name = per_name
        self.age = per_age
		
    def display1(self):
        print("In superclass method: name:", self.name, "age:", self.age)
 
# subclass		
class Employee(Person):
    def display2(self):
        print("In subclass method: name:", self.name, "age:", self.age)
		
emp = Employee("John", 20)  # creating object of superclass
 
emp.display1()
emp.display2()
print("Outside both methods: name:", emp.name, "age:", emp.age)
0 là một đối tượng của lớp con
# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
p = Person()  # creating object of superclass
 
p.display1()
p.display2()
0. Do đó, mỗi người chỉ thuộc về Person, trong khi
# superclass
class Person():
    def __init__(self, per_name, per_age):
        self.name = per_name
        self.age = per_age
		
    def display1(self):
        print("In superclass method: name:", self.name, "age:", self.age)
 
# subclass		
class Employee(Person):
    def display2(self):
        print("In subclass method: name:", self.name, "age:", self.age)
		
emp = Employee("John", 20)  # creating object of superclass
 
emp.display1()
emp.display2()
print("Outside both methods: name:", emp.name, "age:", emp.age)
0 thuộc về
# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
p = Person()  # creating object of superclass
 
p.display1()
p.display2()
0 cũng như Person.

# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
p = Person()  # creating object of superclass
 
p.display1()
p.display2()
05 Kiểm tra xem đối tượng Person7 thuộc về lớp Person.

Python Issubclass ()


Hàm này được sử dụng để kiểm tra xem một lớp có phải là một lớp con của một lớp khác hay không.

# superclass
class Person():
    pass

# subclass		
class Employee(Person):
    pass

print(issubclass(Person, Employee))
print(issubclass(Employee, Person))

Đầu ra

# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
p = Person()  # creating object of superclass
 
p.display1()
p.display2()
08 Kiểm tra xem lớp Person có phải là lớp con của lớp
# superclass
class Person():
    def display1(self):
        print("This is superclass")
 
# subclass		
class Employee(Person):
    def display2(self):
        print("This is subclass")
		
p = Person()  # creating object of superclass
 
p.display1()
p.display2()
0 hay không.

Trước khi kết thúc chương này, chúng ta hãy xem xét thêm một ví dụ về lớp con.

Ví dụ hình vuông và hình chữ nhật

class Rectangle():
    def __init__(self,leng,br):
        self.length = leng
        self.breadth = br
    '''while calling a method in a class python
    automatically passes an instance( object ) of it.
    so we have to pass sef in area i.e. area(self)'''
    def area(self):
        '''length and breadth are not globally defined.
        So, we have to access them as self.length'''
        return self.length*self.breadth
class Square(Rectangle):
    def __init__(self,side):
        Rectangle.__init__(self,side,side)
        self.side = side
s = Square(4)
print(s.length)
print(s.breadth)
print(s.side)
print(s.area())# It appears as nothing is passed but python will pass an instance of class.

Đầu ra

Không có gì mới trong mã trên để giải thích. Nếu bạn đang phải đối mặt với bất kỳ khó khăn nào, thì hãy đọc các nhận xét bên trong mã.

Bạn có thể đặt câu hỏi trong phần thảo luận bất cứ lúc nào và cảm thấy thoải mái khi hỏi.

Để học hỏi từ các video đơn giản, bạn luôn có thể xem khóa học video Python của chúng tôi trên CodeSdope Pro. Nó có hơn 500 câu hỏi thực hành và hơn 20 dự án.

Đặt mục tiêu là bước đầu tiên trong việc biến vô hình thành hữu hình.

- Tony Robbins


Lớp phụ là gì?

Định nghĩa: Một lớp con là một lớp xuất phát từ một lớp khác. Một lớp con kế thừa trạng thái và hành vi từ tất cả các tổ tiên của nó. Thuật ngữ siêu lớp đề cập đến tổ tiên trực tiếp của một lớp cũng như tất cả các lớp lên ngôi của nó.a class that derives from another class. A subclass inherits state and behavior from all of its ancestors. The term superclass refers to a class's direct ancestor as well as all of its ascendant classes.

Lớp phụ là gì và trong lớp là gì?

Siêu lớp so với lớp con
Khi thực hiện kế thừa, lớp hiện có mà các lớp mới có nguồn gốc là siêu lớp.
Khi thực hiện kế thừa, lớp kế thừa các thuộc tính và phương thức từ siêu lớp là lớp con.
Có một siêu lớp.
Có một lớp con.
Di truyền phân cấp
Sự khác biệt giữa lớp siêu lớp và lớp con

Một lớp học có thể có một python phân lớp?

Trong kế thừa, một lớp (thường được gọi là siêu lớp) được kế thừa bởi một lớp khác (thường được gọi là lớp con).Các lớp con thêm một số thuộc tính cho siêu lớp.Dưới đây là một chương trình Python mẫu để cho thấy cách thừa kế được thực hiện trong Python.# Cơ sở hoặc siêu lớp.a class (usually called superclass) is inherited by another class (usually called subclass). The subclass adds some attributes to superclass. Below is a sample Python program to show how inheritance is implemented in Python. # Base or Super class.

Lớp phụ là gì?

Các lớp con, còn được gọi là các lớp có nguồn gốc, lớp người thừa kế hoặc lớp con, là các lớp kế thừa một hoặc nhiều thực thể ngôn ngữ từ một lớp/lớp khác.