giỚi thiỆu lẬp trÌnh hƯỚng ĐỐi...
TRANSCRIPT
![Page 1: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/1.jpg)
Click to edit Master subtitle style
GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG
Khoa Công nghệ thông tin Trường Đại học Ngoại ngữ - Tin học, TP.HCM
CHƯƠNG 1:
1
![Page 2: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/2.jpg)
Một số Mô thức lập trình Khái niệm Lớp và Đối tượng Ba nguyên tắc lập trình hướng đối tượng Một số phần mềm được viết theo hướng đối tượng
NỘI DUNG
2
![Page 3: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/3.jpg)
Object oriented programming (OOP) là mô thức quan trọng
Tất cả ngôn ngữ hiện đại điều object oriented hay hỗ trợ object và class Các framework, library, API hiện đại điều object oriented
• .NET Framework Class Library (C#), Java API classes (Java), • Laravel (PHP), Rails (Ruby), • STL (C++), Django (Python), Android, …
Bất kỳ ai bắt đầu công việc phát triển phần mềm nên làm quen với OOP và OO modeling
Tại sao OOP?
3
![Page 4: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/4.jpg)
MỘT SỐ MÔ THỨC LẬP TRÌNH
4
![Page 5: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/5.jpg)
Programming paradigm: Mô thức/Mô hình lập trình Cách chúng ta “nhìn” về việc xây dựng các chương trình máy tính
Một số programming paradigm: Imperative Structured Procedural Object oriented Functional Declarative …
Mô thức lập trình
5
![Page 6: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/6.jpg)
Mô thức “Lập trình Tuần tự” với ý tưởng chủ đạo là lệnh (commands)
Chương trình là chuỗi các lệnh (cụ thể/rõ ràng (explicit)) có thứ tự (step by step) dùng để thay đổi trạng thái chương trình
Imperative programming
Input data Process
Output data
6
![Page 7: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/7.jpg)
Ví dụ DATA SEGMENT A DB 1,2,3,4,5,6,7,8,9,10 DATA ENDS CODE SEGMENT ASSUME DS:DATA,CS:CODE START: MOV AX,DATA MOV DS,AX MOV CL,10 LEA BX,A MOV AH,00 MOV AL,00 L1: ADD AL,BYTE PTR[BX] INC BX DEC CL CMP CL,00 JNZ L1 MOV AH,4CH INT 21 END START CODE ENDS
7
![Page 8: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/8.jpg)
Đặc điểm: Các biến toàn cục Mỗi bước ảnh hưởng đến trạng thái toàn cục của chương trình Có lệnh goto để nhảy đến lệnh khác Có những đoạn mã được viết nhiều lần
Ngôn ngữ: Assembly, Pascal, C, …
Imperative programming
8
![Page 9: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/9.jpg)
Mô thức “Lập trình Cấu trúc” là dạng Imperative programming, nhưng Chương trình có tổ chức, cấu trúc logic hơn:
Structured programming
result = []; for i = 0; i < length(people); i++ { p = people[i]; if length(p.name)) > 5 { addToList(result, toUpper(p.name)); } } return sort(result);
9
![Page 10: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/10.jpg)
Đặc điểm: Lệnh tuần tự (Sequence), Lệnh lặp (Loop), Lệnh điều kiện (Condition) Phong cách lập trình Thục đầu dòng (indentation) Loại bỏ/hạn chế lệnh goto
Ngôn ngữ: Pascal, C, …
Khuyết điểm:
Khó phân công cho nhiều người cùng viết Dữ liệu được “thả tự do” Có những đoạn mã viết nhiều lần
Structured programming
10
![Page 11: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/11.jpg)
Mô thức “Lập trình Thủ tục” kế thừa structured programming và đưa thêm ý tưởng chương trình con (procedure/function) chứa các đoạn mã thường sử dụng
Chương trình máy tính có thể chia thành các chương trình con (procedure) và các chương trình con gọi nhau để giải quyết bài toán
Procedural programming
11
![Page 12: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/12.jpg)
Ví dụ public static int Factorial(int n) { int result = 1; for (int i = 2; i <= n; i++) result = result * i; return result; } static void Main(string[] args) { int fact; fact = Factorial(5); Console.WriteLine("5! = " + fact); int n = 10; int m = 5; fact = Factorial(n) + Factorial(m); Console.WriteLine("n! + m! = " + fact); }
12
![Page 13: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/13.jpg)
Đặc điểm Tập trung vào “action” của chương trình, không phải data Phương pháp top-down, làm mịn dần: Chia chương trình lớn thành các chương trình con, … Biến toàn cục (global variable) và Biến cục bộ (local variable)
Ngôn ngữ: Pascal, C, …
Khuyết điểm:
Mối quan hệ giữa dữ liệu và hàm không được quan tâm, quản lý
Procedural programming
13
![Page 14: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/14.jpg)
Mô thức “Lập trình Hướng đối tượng” cải biên procedure programming bằng cách đưa vào ý tưởng object (đối tượng)
Chương trình máy tính gồm các objects, các objects giao tiếp với nhau để giải quyết bài toán đặt ra
Object oriented programming
Đối tượng = Dữ liệu + Hàm thao tác trên Dữ liệu
14
![Page 15: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/15.jpg)
Ví dụ class Student { string code; string name; int yearOfBirth; public Student(string studentCode, string studentName, int year) { code = studentCode; name = studentName; yearOfBirth = year; } public int GetYearOld() { return DateTime.Now.Year - yearOfBirth; } }
15
![Page 16: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/16.jpg)
Ví dụ
class Program { static void Main(string[] args) { InformaticsClass a = new InformaticsClass(); a.Input(); Console.WriteLine("No. " + a.NumStudents()); } }
class InformaticsClass { Student[] students; string className; public void Input() { } public int NumStudents() { if (students == null) return 0; return students.Length; } }
16
![Page 17: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/17.jpg)
Đặc điểm: Tập trung xung quanh khái niệm object (tập trung vào dữ liệu và những gì làm với dữ liệu đó)
• Tập trung cả Dữ liệu và Hàm • Tổ chức code theo cách (mô phỏng) hoạt động của thế giới thực • Mô hình hóa các đối tượng trong thế giới thực vào máy tính
Hỗ trợ Các nguyên tắc:
• Đóng gói (encapsulation) • Thừa kế (inheritance) • Đa hình (polymorphism), …
Object oriented programming
17
![Page 18: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/18.jpg)
Ưu điểm: Quản lý mối quan hệ giữa Dữ liệu và Hàm (trong object) Tổ chức code hiệu quả theo cách thức tổ chức trong tự nhiên Giúp mô hình hóa các đối tượng trong thế giới thực trên máy tính
Ngôn ngữ: C++, Java, C#, …
Object oriented programming
18
![Page 19: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/19.jpg)
KHÁI NIỆM LỚP VÀ ĐỐI TƯỢNG
19
![Page 20: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/20.jpg)
Trong thế giới thực có nhiều đối tượng:
Đối tượng (object)
20
![Page 21: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/21.jpg)
Hoạt động thường ngày: Sáng ra khỏi gường, dùng bàn chảy đánh răng, ăn sáng và uống cafe, đến công ty làm việc, đến nhà hàng ăn trưa, giải lao, rồi quay về công ty làm viêc, … tối vào bếp nấu ăn, xem ti vi/đọc sách, lên gường ngủ.
Life là quá trình tương tác giữa các đối tượng với nhau Ý tưởng: Tiến trình viết phần mềm là quá trình mô hình hóa từ thực tế vào máy tính
Đối tượng (object)
21
![Page 22: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/22.jpg)
Trong thế giới thực, đối tượng (object) thường có Một số thuộc tính cấu tạo nên đối tượng Một số hành vi
Đối tượng (object)
Attributes: - ID - Name - YearOfBirth - …
Behaviours: - GetAge - SetName - …
Attributes: - Title - Author - Pages - …
Behaviours: - CountWords - BookMark - SetTitle - …
22
![Page 23: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/23.jpg)
OOP là phương pháp chuyển các vấn đề thế giới thực (đối tượng thực) sang các mô hình (đối tượng trên máy tính) có thể giải quyết trên máy tính Trong OOP, Đối tượng (object): Bất kỳ thứ gì có thể được mô hình thành:
Dữ liệu (data/field/property/attribute/state) Hàm (function/method/behaviour/operation) thao tác trên dữ liệu
Trong OOP, Đối tượng (object) là biến có Tập dữ liệu và Hàm
Đối tượng (object)
23
![Page 24: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/24.jpg)
Real world object: Car Properties: color, make, model, year, …
• Velocity
Methods: • Accelerator (chân ga) • Brake (thắng) • Speedmotor (đồng hồ tốc độ)
Đối tượng (object) – ví dụ
24
![Page 25: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/25.jpg)
Lớp (class): Nhiều đối tượng cụ thể “giống nhau” được nhóm thành một loại/loài hay lớp (class) Lớp (class) là mẫu (template) hay bản thiết kế (blueprint) để tạo đối tượng
Lớp (class)
25
![Page 26: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/26.jpg)
Lớp (class) – ví dụ
26
![Page 27: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/27.jpg)
Trong OOP, Lớp (class) dùng để định nghĩa/tạo ra kiểu mới, từ đó tạo ra đối tượng, lớp có
Dữ liệu (data/field/property/attribute/state), dùng biến lưu trữ Các hàm (function/method/behaviour) thao tác trên dữ liệu
Lớp (class)
27
![Page 28: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/28.jpg)
VD: Để tạo Car, Lấy Car class, dựa trên đó tạo Car
Lớp và Đối tượng
Class
object
object
object
28
![Page 29: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/29.jpg)
Lớp và Đối tượng class Car { int velocity; public Accelerator() { } public int Speedmotor() { } }
class Program { staitc void Main () { Car a = new Car(); Car b = new Car(); Car c = new Car(); } }
29
![Page 30: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/30.jpg)
3 NGUYÊN TẮC LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG
30
![Page 31: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/31.jpg)
Đóng gói (Encapsulation) và Giao diện (Interface) Ẩn chi tiết của lớp (Đóng gói), chỉ cung cấp các phương thức cần thiết để dùng (Giao diện)
Thừa kế (Inheritance) Tạo lớp mới thừa kế lớp đã có
Đa hình (Polymorphism) Khả năng tạo ra các lớp dẫn xuất, cài đặt cùng một method của lớp cơ sở theo những cách khác nhau (tùy từng lớp dẫn xuất)
3 nguyên tắc lập trình hướng đối tượng
31
![Page 32: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/32.jpg)
Đóng gói (Encapsulation) Ẩn chi tiết công việc thực thi của lớp Trong OOP, Đóng gói là
• Ẩn đi các property • Ẩn đi một số method • Ẩn đi các chi tiết hiện thực của method
Giao diện (Interface)
Bản điều khiển cho phép dùng đối tượng Trong OOP, Giao diện là tập các method cho phép dùng object
Encapsulation và Interface
32
![Page 33: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/33.jpg)
Encapsulation và Interface
33
![Page 34: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/34.jpg)
Thừa kế (inheritance) Tạo lớp mới thừa kế lớp đã có Một cách tái sử dụng các lớp bằng cách mở rộng lớp đã có thành một lớp cụ thể “Lớp đã có”: base class, superclass, parent class “Lớp mới”: derived class, subclass, child class
Thừa kế (inheritance)
34
![Page 35: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/35.jpg)
Thừa kế (inheritance)
35
![Page 36: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/36.jpg)
Thừa kế (inheritance)
36
![Page 37: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/37.jpg)
Thừa kế (inheritance)
37
![Page 38: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/38.jpg)
Thừa kế: Lớp con thừa kế Các dữ liệu của lớp cha Các hàm của lớp cha
Ngoài ra, lớp con còn Có những dữ liệu riêng của lớp con Có những hàm riêng của lớp con
Thừa kế (inheritance)
38
![Page 39: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/39.jpg)
Đa hình (polymorphism) Poly = many Morphism = forms Các lớp con cài đặt cùng một hàm của lớp cha, nhưng mỗi lớp con có cách cài đặt khác nhau
Đa hình (polymorphism)
39
![Page 40: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/40.jpg)
Đa hình (polymorphism)
40
![Page 41: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/41.jpg)
Photoshop Adobe Acrobat NotePad++ VLC (VideoLan) Camtasia Studio Paint.NET Visual Studio Microsoft Office (Word, Excel, …) SQL Server Sky client
MỘT SỐ PHẦN MỀM VIẾT THEO OOP
41
![Page 42: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/42.jpg)
Sử dụng các lớp có sẵn: Lớp Random Viết chương trình mô phỏng gieo súc sắc Hãy tiến hành thí nghiệm gieo một súc sắc N lần (N=100), tính xác suất của các mặt súc sắc trong thí nghiệm này Thay N=1.000, N=10.000, N=100.000, N=1.000.000 và quan sát kết quả (so sánh với giá trị xác suất tính trong lý thuyết).
BÀI TẬP
42
![Page 43: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/43.jpg)
Sử dụng các lớp có sẵn: Lớp Random Hãy tiến hành thí nghiệm gieo một súc sắc N lần (N=100), tính xác suất tổng hai mặt súc sắc bằng 8 trong thí nghiệm này. Thay N=1.000, N=10.000, N=100.000, N=1.000.000 và quan sát kết quả (so sánh với giá trị xác suất tính trong lý thuyết).
BÀI TẬP
43
![Page 44: GIỚI THIỆU LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGfit.huflit.edu.vn/.../Chuong-01.Gioi-thieu-lap-trinh-huong...tuong.pdf · LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG ... • STL (C++),](https://reader030.vdocuments.net/reader030/viewer/2022020120/5af54dd17f8b9a190c8e24d0/html5/thumbnails/44.jpg)
Tại sao học OOP? OOP là gì? Object và Class 3 nguyên tắc cơ bản của lập trình hướng đối tượng
Đóng gói (Encapsulation) Thừa kế (Inheritance) Đa hình (Polymorphism)
ĐIỂM CHÍNH
44