Python Tạo lớp cha
Tạo lớp cha mẹ
Bất kỳ lớp nào cũng có thể là lớp cha, vì vậy cú pháp giống như tạo bất kỳ lớp nào khác:
Thí dụ
Tạo một lớp có tên Person
, với
firstname
và lastname
thuộc tính và một printname
phương thức:
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname,
self.lastname)
#Use the Person class to create an object, and then
execute the printname method:
x = Person("John", "Doe")
x.printname()