Class trong Python và tính kế thừa
Python là ngôn ngữ lập trình hướng đối tượng
Nên tất cả mọi thứ trong python đều là object với những properties và methods.
Tạo một Class trong python:
Tạo một class trong pythong có tên là Person và property có tên là : xclass Person:
x = 5
Tạo một object:
Tạo một object tên là p1 và in ra giá trị x:
p1 = Person()
print(p1.x)
Hàm __init__
Các lớp (class) đều có một hàm ( function ) khi một class được khởi động thì hàm __init__luôn thực thi khi class khởi động (initiate) .Ví dụ:
Tạo một class có tên là Person và và sử dụng hàm __init__ để gán giá trị cho name và age.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("Thanh", 35)
print(p1.name)Ví dụ 2 : Thêm một fuction để in ra câu chào mừng thực thi nó trên object p1.
print(p1.age)
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def chaomung(self):
print("chao mung den voi devops blog cua" + self.name)
p1 = Person("Thanh", 35)
p1.chaomung()
Thông số self
Là thông số instance hiện tại dùng để truy cập vào các biến của class đó.Ví dụ sử dụng abc và xyz theo cho self cho một class:
class Person:
def __init__(abc, name, age):
abc.name = name
abc.age = age
def chaomung(xyz):
print("Chao mung den voi devops blog cua" + abc.name)
p1 = Person("thanh", 18)
p1.chaomung()
Thay đổi object properties:
Ví dụ trên ta muốn thay đổi age 35 thành 40
ta set như sau:
p1.age = 40
Xoá một property:
del p1.age
Xoá một object:
del p1
Pass statement:
Class không thể empty nên nếu trong một trường hợp nào đó mà class không có content thì gắn thông số pass vào class:
class Color:
pass
Tính kế thừa class của python:
Tính kế thưà cho phép chúng ta định nghĩa một lớp mà tất cả những methods và properties được kế thừa từ những lớp khác.Lớp cha ( parent class ) là lớp có tính kế thừa cho các class khác còn gọi là base class.
Lớp con ( child class ) là lớp có tính kế thừa từ lớp khác còn gọi là derived class.
Tạo môt lớp cha ( Parent class )
Ví dụ tạo một lớp Person có các properties firstname , lastname và method là : printnameclass Person:
def __init__(self, fname, lname)
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
x = Person("Thanh", "Nguyen" )
x.printname()
Tạo một lớp con
Tạo một lớp con Student có tính kế thừa properties và method từ class cha là Personclass Student(Person):Tạo một object để in ra môt Student name:
pass
y = Student("Toan", "Nguyen")
y.printname()
Thêm function __init__ vào lớp con:
class Student(Person):Khi ban thêm function __init__ vào lớp con thì tính kế thừa function từ lớp cha sẽ mất , nếu ta muốn giữ tính kế thừa từ lớp cha:
def __init__(self, fname, lname):
class Student(Person):
def __init__(self, fname, lname):
Person.__init__(self, fname, lname):
Sử dụng function super()
Sử dụng hàm super() trong lớp con nó cho phép kế thừa tất cả các properties và methods từ lớp cha của nó.class Student(Person):def __init__(self, fname, lname):
super().__init__(self, fname, lname):
Thêm properties:
Cách thứ 2 có thể gắn biến year và lớp con Student
Thêm method:
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year
def welcome(self):
print("Welcome", self.firstname, self.lastname, "to the class of", self.graduationyear)
x = Student("Mike", "Olsen", 2019)
x.welcome()
Nhận xét
Đăng nhận xét