20

Click here to load reader

Pbc day-10-class

Embed Size (px)

Citation preview

Page 1: Pbc day-10-class

Class

Th.S Trần Đức Lợi

Pythonvietnam.info

Page 2: Pbc day-10-class

Ôn tập bài cũ

• Ôn lại nội dung đã học về File I/O

PythonBeginnerClass @loitd #pythonvietnam.info

Page 3: Pbc day-10-class

Mục đích bài học

• Tìm hiểu về Class trong python

PythonBeginnerClass @loitd #pythonvietnam.info

Page 4: Pbc day-10-class

Class

• Một mẫu do người dùng tự định nghĩa vớimột tập hợp các thuộc tính (attributes)

• Attributes = data members (variables) + methods

• Truy nhập thuộc tính thông qua dấu “.”

PythonBeginnerClass @loitd #pythonvietnam.info

Page 5: Pbc day-10-class

Class: Sample

• Dùng từ khóa class để khai báo

• class classname:

– ‘’’Docstring for class’’’

– Attributes

• Classname.__doc__

PythonBeginnerClass @loitd #pythonvietnam.info

Page 6: Pbc day-10-class

Class: Class variables vs Instance variables

• Class variables:

– Dùng chung bởi các instance của class

– Định nghĩa ở trong class nhưng ngoài các method

• Instance variables:

– Khác nhau giữa các instance

– Được định nghĩa trong methods

PythonBeginnerClass @loitd #pythonvietnam.info

Page 7: Pbc day-10-class

Class: Method

• Function được khai báo trong khai báo của lớp

• class classname:

– ‘’’Docstring for class’’’

– Def method1(self):

• Method content

PythonBeginnerClass @loitd #pythonvietnam.info

Page 8: Pbc day-10-class

Class: Instance

• Một đối tượng của một class

• Person = classname()

• Person được coi là một instance củaclassname

PythonBeginnerClass @loitd #pythonvietnam.info

Page 9: Pbc day-10-class

Class

• Ví dụ một class thực tế:

• Class People:– ‘’’docstrings here’’’

– Pcount = 0

– Def __init__(self, name):• Self.name = name

• People.pcount =+ 1

– Def getpopulation(self):• Print People.pcount

PythonBeginnerClass @loitd #pythonvietnam.info

Page 10: Pbc day-10-class

Class: __init__()

• Một loại method đặc biệt

• Initialization method (# constructor)

• Được gọi khi khởi tạo một instance mới

• Ví dụ:

– Loi = People(‘Tran Duc Loi’)

PythonBeginnerClass @loitd #pythonvietnam.info

Page 11: Pbc day-10-class

Class: self

• Tất cả các method được khai báo trong class đều có self

• Khi gọi một method python sẽ không cần khaibáo

• Xem lại ví dụ.

PythonBeginnerClass @loitd #pythonvietnam.info

Page 12: Pbc day-10-class

Class: Truy nhập thuộc tính

• Sử dụng “.” để truy nhập thuộc tính

• Ví dụ:

– Person. Getpopulation()

– Person.name

PythonBeginnerClass @loitd #pythonvietnam.info

Page 13: Pbc day-10-class

Class: bài tập

• Hãy viết chương trình Quản lý nhân sự dướidạng OOP

PythonBeginnerClass @loitd #pythonvietnam.info

Page 14: Pbc day-10-class

Class

• Hasattr(object, name)

• Getattr(object, name, default)

• Setattr(object, name, value)

• Delattr(object, name)

PythonBeginnerClass @loitd #pythonvietnam.info

Page 15: Pbc day-10-class

Class: __del__()

• Xóa các đối tượng không còn cần thiết

• Python tự động thực hiện garbage collection

• __del__()

• Ví dụ:

• Class People:– ‘’’docstrings here’’’

– Pcount = 0

– Def __init__(self, name):

– Def __del(self):

PythonBeginnerClass @loitd #pythonvietnam.info

Page 16: Pbc day-10-class

Class: Inheritance

• Tính kế thừa trong hướng đối tượng

• Ví dụ:

– Class Children(Parent1, Parent2, …):

• ‘’’docstring’’’

• Class Children sẽ được kế thừa các thuộc tínhtrong các class Parent1, …

• Class Children có thể ghi đè các thuộc tính củaclass Parent1, …

PythonBeginnerClass @loitd #pythonvietnam.info

Page 17: Pbc day-10-class

Class: Inheritance

• Ví dụ:

• Class Parent(object):– Age = 100

– Def __init(self):

– Def setAttr(self, value):

– Def getAttr(self):

• Class Child(Parent):– Def __init__(self):

• Super(Child, self).__init__()

– Def childget(self):

PythonBeginnerClass @loitd #pythonvietnam.info

Page 18: Pbc day-10-class

Class: Polymorphism

• Overriding methods

• Cho các phương thức khác nhau ở các lớp con khác nhau

• Hàm __init__() là một ví dụ về overriding

• Xem xét ví dụ

PythonBeginnerClass @loitd #pythonvietnam.info

Page 19: Pbc day-10-class

Class: Encapsulation

• Hạn chế truy cập vào các thuộc tính của đốitượng

• Public, protected, private?

• __wheels

• Object._classname__attributename

• Python đã bảo vệ truy nhập thuộc tính đốitượng như thế nào?

PythonBeginnerClass @loitd #pythonvietnam.info

Page 20: Pbc day-10-class

Class: Bài tập

• Viết module configloader, có comment trongfile dưới dạng OOP

PythonBeginnerClass @loitd #pythonvietnam.info