Python Tạo lớp cha
Tạo một lớp cha mẹ
Bất kỳ lớp nào cũng có thể là lớp cha, do đó cú pháp giống như tạo bất kỳ lớp nào khác:
Ví dụ
Tạo một lớp có tên Person
, với các thuộc tính firstname
và lastname
và phương thức printname
:
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()
Hãy tự mình thử »