Hướng dẫn how do you define class and subclass in python? - làm thế nào để bạn xác định lớp và lớp con trong python?
Show
Keno Leon Ngày 15 tháng 4 năm 2020 8 phút đọc Cái gì, tại sao, khi nào nên sử dụng.Cùng với các chức năng, các lớp học là nền tảng của Python và nhiều ngôn ngữ lập trình khác; Phân loại phụ hoặc kế thừa cho phép bạn sắp xếp mã của mình và tái sử dụng chức năng nhưng có thể không rõ ràng như thế nào và khi nào nên sử dụng chúng, hãy để Lôi có một cái nhìn 👋👋 Hi there 👋👋 all my content is free for Medium subscribers, if you are already a subscriber… # 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()09 Kiểm tra xem lớp # 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()0 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") emp = Employee() # creating object of subclass emp.display1() emp.display2()1 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 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.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;
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 Chúng tôi đã tạo hai lớp # 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()0 và # 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()1 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") emp = Employee() # creating object of subclass emp.display1() emp.display2()2 → 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") emp = Employee() # creating object of subclass emp.display1() emp.display2()1 được kế thừa từ lớp # 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()0. 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") emp = Employee() # creating object of subclass emp.display1() emp.display2()1 là một lớp con của # 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()0. (Một nhân viên là một người) Vì # 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()1 là một lớp con của # 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()0, 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") emp = Employee() # creating object of subclass emp.display1() emp.display2()9 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()0 → Chúng tôi đã tạo một đối tượng # 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 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") emp = Employee() # creating object of subclass emp.display1() emp.display2()1. # 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()3 → 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 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 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") emp = Employee() # creating object of subclass emp.display1() emp.display2()9 của lớp cha ____10. 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 # 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()0 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") emp = Employee() # creating object of subclass emp.display1() emp.display2()1 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 # 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()0 và # 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()1 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 Chúng tôi đã tạo hai lớp # 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()0 và # 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()1 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") emp = Employee() # creating object of subclass emp.display1() emp.display2()2 → 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") emp = Employee() # creating object of subclass emp.display1() emp.display2()1 được kế thừa từ lớp # 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()0. 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") emp = Employee() # creating object of subclass emp.display1() emp.display2()1 là một lớp con của # 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()0. (Một nhân viên là một người) Vì # 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()1 là một lớp con của # 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()0, 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") emp = Employee() # creating object of subclass emp.display1() emp.display2()9 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()0 → Chúng tôi đã tạo một đối tượng # 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 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") emp = Employee() # creating object of subclass emp.display1() emp.display2()1. # 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()3 → 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 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 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") emp = Employee() # creating object of subclass emp.display1() emp.display2()9 của lớp cha ____10. 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 # 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()0 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") emp = Employee() # creating object of subclass emp.display1() emp.display2()1 của nó. Chúng tôi đã tạo một đối tượng # 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 của lớp cha # 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()0. Đố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") emp = Employee() # creating object of subclass emp.display1() emp.display2()9 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 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 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 Hãy để cố gắng hiểu những gì đang xảy ra ở đây. Lớp # 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()1 là một 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") emp = Employee() # creating object of subclass emp.display1() emp.display2()0. Do đó, # 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()1 kế thừa các thuộc tính ( # 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)6 và # 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)7), 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") emp = Employee() # creating object of subclass emp.display1() emp.display2()9) và hàm tạo ( # 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)9) của # 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()0. Do đó, chúng cũng có thể được truy cập bởi các đố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") emp = Employee() # creating object of subclass emp.display1() emp.display2()1. Do đó, trong phương thức # 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 của lớp con, chúng tôi đã truy cập trực tiếp các thuộc tính # 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)6 và # 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)7 của lớp cha. # 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 → Một đối tượng # 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 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") emp = Employee() # creating object of subclass emp.display1() emp.display2()1 được tạo bằng cách truyền các giá trị của John John và 20 cho các tham số # 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)8 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)9 của hàm tạo của lớp cha (vì lớp con đã thừa hưởng hàm tạo của lớp cha). Do đó, trong hàm tạo, các giá trị của các thuộc tính # 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)6 và # 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)7 trở thành John John và 20 cho đối tượng # 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. # 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 # 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()3 → Đối tượng # 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 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") emp = Employee() # creating object of subclass emp.display1() emp.display2()9 của lớp cha. name: John age: 20 # 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()6 → Đối tượng # 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 gọi phương thức # 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 của lớp riêng của nó. Hàm python siêu ()Trong ví dụ trước, thay vì # superclass class Person(): pass # subclass class Employee(Person): pass print(issubclass(Person, Employee)) print(issubclass(Employee, Person))7, chúng ta có thể sử dụng hàm # superclass class Person(): pass # subclass class Employee(Person): pass print(issubclass(Person, Employee)) print(issubclass(Employee, Person))8 để gọi hàm tạo và phương thức của lớp cha trong lớp con. & NBSP; Hàm # superclass class Person(): pass # subclass class Employee(Person): pass print(issubclass(Person, Employee)) print(issubclass(Employee, Person))8 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 # superclass class Person(): pass # subclass class Employee(Person): pass print(issubclass(Person, Employee)) print(issubclass(Employee, Person))8. # 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ế # superclass class Person(): pass # subclass class Employee(Person): pass print(issubclass(Person, Employee)) print(issubclass(Employee, Person))1 bằ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.2 và # superclass class Person(): pass # subclass class Employee(Person): pass print(issubclass(Person, Employee)) print(issubclass(Employee, Person))6 bằ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.4 bên trong lớp con. Khi gọi một phương thức bằng cách sử dụng # superclass class Person(): pass # subclass class Employee(Person): pass print(issubclass(Person, Employee)) print(issubclass(Employee, Person))8, 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à 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 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.7, 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 Trong ví dụ này, chúng tôi đã thay thế # superclass class Person(): pass # subclass class Employee(Person): pass print(issubclass(Person, Employee)) print(issubclass(Employee, Person))1 bằ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.2 và # superclass class Person(): pass # subclass class Employee(Person): pass print(issubclass(Person, Employee)) print(issubclass(Employee, Person))6 bằ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.4 bên trong lớp con. Khi gọi một phương thức bằng cách sử dụng # superclass class Person(): pass # subclass class Employee(Person): pass print(issubclass(Person, Employee)) print(issubclass(Employee, Person))8, 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à 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 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.7, 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; # superclass class Person(): pass # subclass class Employee(Person): pass print(issubclass(Person, Employee)) print(issubclass(Employee, Person)) Đầu ra Trong ví dụ này, chúng tôi đã thay thế # superclass class Person(): pass # subclass class Employee(Person): pass print(issubclass(Person, Employee)) print(issubclass(Employee, Person))1 bằ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.2 và # superclass class Person(): pass # subclass class Employee(Person): pass print(issubclass(Person, Employee)) print(issubclass(Employee, Person))6 bằ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.4 bên trong lớp con. Khi gọi một phương thức bằng cách sử dụng # superclass class Person(): pass # subclass class Employee(Person): pass print(issubclass(Person, Employee)) print(issubclass(Employee, Person))8, 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ó.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 Trong ví dụ này, chúng tôi đã thay thế # superclass class Person(): pass # subclass class Employee(Person): pass print(issubclass(Person, Employee)) print(issubclass(Employee, Person))1 bằ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.2 và # superclass class Person(): pass # subclass class Employee(Person): pass print(issubclass(Person, Employee)) print(issubclass(Employee, Person))6 bằ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.4 bên trong lớp con. Khi gọi một phương thức bằng cách sử dụng # superclass class Person(): pass # subclass class Employee(Person): pass print(issubclass(Person, Employee)) print(issubclass(Employee, Person))8, 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à Python isinstance () Làm thế nào để bạn xác định lớp con?Đị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.
Chúng ta có thể xác định lớp học trong Python không?Xác định một lớp trong các định nghĩa chức năng giống như Python bắt đầu bằng từ khóa def trong Python, các định nghĩa lớp bắt đầu bằng từ khóa lớp.Chuỗi đầu tiên bên trong lớp được gọi là DocString và có một mô tả ngắn gọn về lớp.Mặc dù không bắt buộc, điều này rất được khuyến khích.class definitions begin with a class keyword. The first string inside the class is called docstring and has a brief description of the class. Although not mandatory, this is highly recommended.
Làm thế nào để bạn xác định một biến lớp trong Python?Một biến lớp được khai báo bên trong lớp, nhưng ngoài bất kỳ phương thức thể hiện nào hoặc phương thức __init __ ().Theo quy ước, thông thường nó được đặt ngay bên dưới tiêu đề lớp và trước phương thức cấu trúc và các phương thức khác.. By convention, typically it is placed right below the class header and before the constructor method and other methods.
Làm thế nào để bạn xác định một kế thừa lớp trong Python?Kế thừa cho phép chúng tôi xác định một lớp kế thừa tất cả các phương thức và thuộc tính từ một lớp khác.Lớp cha là lớp được kế thừa, còn được gọi là lớp cơ sở.Lớp con là lớp kế thừa từ một lớp khác, còn được gọi là lớp dẫn xuất.. Parent class is the class being inherited from, also called base class. Child class is the class that inherits from another class, also called derived class. |