Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 221 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
221
Dung lượng
3,49 MB
Nội dung
,GlAO TRINH * LAP TRlHH HU0NG DOI TlfOHG ■ NHA XUAT BAN MONG BAN DOC GOP Y KIEN, PHE BINH ■ • 1.,r ■ 60 : 674 - 2020 QDND - 2020 BQ GIAO DUG VA DAO TAO TRirdNG DAI HOC CUU LONG ' ' • fr GIAO TRINH LAP TRINH HUANO BOl TIRING ■ fl (Dung cho sinh vien ngdnh cong nghe thong tin) TRJdNG DAI HOC Cl)u LONG THtfVliN NHA XUAT BAN QUAN DOI NHAN DAN Ha Noi - 2020 Chu bien: TS Truong Quoc Bao Tham gia bien soan: ThS Tran Thi Thuy ThS Tran Lam Quyen\ , | wlwUHTj i - MUC LUC DANH MUC HINH ANH VA BANG BIEU 12 DANH MUC Ttf VIET TAT 14 LOT Md DAU 15 ChirOTig 1: TONG QUAN VE LAP TRINH HUONG DOITU QNG 1.1 Giai thieu 19 • 19 1.2 Cac phuong phap lap trinh 20 1.2.1 Lap trinh tuyen tinh 20 1.2.2 Lap trinh huang cau true 20 1.2.3 Lap trinh huang dan the 22 1.2.4 Phuong phap tien can huang doi tuang 23 1.2.5 So sanh hai each tiep can 1.2.6 Xu huang phat then cua lap trinh huang doi tuang 1.3 Mot so khai niem ca ban 31 34 36 1.3.1 Doi tuang 37 1.3.2 Lap 39 1.3.3 Thuoc tinh 41 1.3.4 Phuong thuc 42 43 1.3,5 Trinitiiong hoa 46 1.3.6 Thong diep t • f 1.4 Cac dac diem quan cua lap trinh huong doi tuong 1.4.1 Dong goi :46 1.4.2 KSthua 48 1.4.3 Da hinh 49 1.5 Mot so thuat ngu cua lap trinh huong doi tuong 50 1.6 Mot so ngon ngu lap trinh huong doi tuong 50 1.6.1 C++ 1.6.2 ASP.NET va C#.NET 1.6.3 Java 51 ' -52 53 TONG KET CHUONG 53 CAU HOI ON TAP 55 TAI LIEU TEAM KHAO 56 Chuang 2: LOIP DOI TURING ■ 57 2.1 Khai bao lop'■ 57 2.2 Thuoc tinh truy cap (Muc truy cap) 60 2.3 Cach su dung cac doi tuong ciia lop 64 2.4 Ttr khoa this 66 2.5 Tao ddi tuong 67 2.5.1 Phuong thuc tao (constructor) ■ , 46 67 2.5.2 Phuong thuc tao chep (copy constructor) 69 2.6 Huy doi tuong 70 2.7 Thanh vien tinh -72 2.7.1 Thuoc tinh tinh 73 2.7.2 Phuong thuc tinh 74 2.8 Truyen tham so 76 2.8.1 Truyen tham tri bang tham so kieu gia tri A /X t ’ll f 2.8.2 Truyen tham chieu bang tham so kieu gia tri vdi tu khoa ref 2.8.3 Truyen tham chieu voi tham so kieu gia tri bing tii khoa out 78 80 2.9 Qua tai phuong thuc (nap chong - overloading) 81 2.10 Dong goi du lieu vai thuoc tinh 86 2.11 Mot s6 khai niem khac 90 2.11.1 Cac truong chi doc 90 2.11.2 Lop truu tuong 90 2.11.3 Lop giao dien 91 2.11.4 Lop Sealed 91 2.11.5 Lop tinh 92 TONG KET CHLTONG CALF HOI ON TAP ■ 92 93 BAI TAP 94 TAI LIEU TEAM KHAO 103 Chiroug 3: NAP CHONG TOAN TU* (OPERATOR OVERLOADING) 5.1 Toantu 105 3.2 Nap chong loan tur 106 ■3.2.1 Toan tu mot ngoi 107 3.2.2 Toan tu hai ngoi 112 3.3 Pham vi su dung toan tu 117 3.4 Yeu ciu su dung 118 3.5 Uu nhugc diem cua tai dinh nghTa toan tu 118 TONG LET CHLTONG ' ‘ 119 CAUHOIONTAP 119 BAI TAP 119 TAI LIEU THAM KHAO 121 Chiroug 4: KE THlfA 105 123 4.1 Gioi thieu 123 4.2 Cu phap ke thua 124 4.3 Tu khoa base va new 125 4.4 Goi phuang thuc tao cua lap ca sa 126 4.5 Vi du 128 4.6 Lop co so va doi tuong phan 138 4.7 Nghiem cam ke thira 140 TONG KET CHllONG 141 CAU HOI ON TAP 141 BAI TAP 143 TAI LIEU TEAM KHAO 147 Chirong 5: DA HINH 149 5.1 Gioi thieu 149 ; 5.2 Tao phuong thiic da hinh 150 5.3 Phan biet tir khoa new va override - 5.4 VI du 5.5 Lop triru tuong 5.6 Lop sealed 151 156 166 : 171 TONG KET CHLTONG 173 CAU HOI ON TAP 173 BAI TAP 174 TAI LIEU THAM KHAO 179 ChtfOTig 6: INTERFACE (GIAO DIEN) 181 6.1 Gioi thieu 181 6.2 Thue thi mot giao dien 181 6.3 Thue thi nhiSu giao dien 184 Long Ulong -9.223.372.036.864.775.808 9.223.372.036.854.775.807 : Int64 dBn Oxffffffffffffffff Uint64 3.1.2 Chon kieu xay dung san Kieu char dirac bieu dien dang Unicode Unicode va ky tvr escape phai duoc gan voi diu Vi du, A la mot ky tu dom gian \u00041 la mot ky tu unicode Bang 2: Mot so ky tu bien KytyT Mo ta V Nhay don \” Nhay kep \\ Xuyet trai \0 Null \a • Alert \b Xoa trai mot ky tu \f Form feed \n Xuong dong \r Carriage return \t Tab ngang \v Tab doc 206 3.1.3 Chuyen kieu xay dung san / 'f f f Doi tuong cua mot kieu co the duoc chuyen mot doi tuong khac theo each tudng minh hay khong tuong minh Chuyen kieu tudng minh se thuc hien chuyen tu dong va trinh bien dich se quan ly gium chung ta Vi du: short x = 5; inty = x; Ndu chung ta chuyen theo mot each khac chung ta cd thd mat thong tin Trinh bien dich se khong thuc hien chuyen kieu ro rang tu kieu short sang int Vi du: short x; int y = 500; x = y;L Chung ta phai khai bao ro rang: short x; int y = 500; x = (short)y; 3.2 Bien va hang Mot bidn la mot vi tri luu tru vai mot kieu Trong vi du tren, ca x va y la bidn Chung ta tao bien bang each khai bao kieu va gan ten cho no Chung ta cd the khdi tao bidn chung ta khai bao nd Chung ta cd the gan gia tri ddn bien bat cu luc nao Vi du minh hoa: int mylnt = 7; System.Console.WriteLinef'Khcri t?o,mylnt: {0}", mylnt); mylnt = 5; System.Console.WriteLine("Sau gan, mylnt: {0}", mylnt); 207 □ @ C'\WlNDOW$\system32\cmd.«e X Khoi tao^mylnt: Sau gan, mylnt: Press any key to continue 3.2.1 Dinh nghia gan C# yeu cau bien dirge khoi tao tnroc chung dirge sir dung Vi du sau sur dung mot bien khong dirge khoi gan: int mylnt; System.Console.WriteLine("Khdng khcri g3n, mylnt: {0}", mylnt): mylnt = 5; System.Console.WriteLineC'Khdi gan, mylnt: {0}", mylnt); Khi chay chuong trinh se thong bao Idi sau: Error List < Y - ã â Error ' ' ! Warnings CX, Messages ! Description Error List f Output Ready That sir chung ta khong cSn khdi tao mot bidn nhimg chung ta can gan mot gia tri tnroc str dung chung Vi du sau minh hoa mot chirong trinh dung 208 int mylnt; mylnt = 7; System.Console.WriteLineC'Khd'i gan, mylnt: {0}", mylnt); mylnt = 5; System.Console.WriteLinef'Gia trjsau gan, mylnt: {0}", mylnt); 3.2.2 Hang Mot hang la mot bien ma gia tri khong bi thay doi Cu phap: const kieu ten bien gia tri Vi du: const int Tile = 10; Vi du minh hoa su dung bien hang const int NhietDoDongLanh = 32; // dp Farenheit const int NhietDoSoi = 212; System.Console.WriteUne(" Nhiet dong lanh cua nuoc:{0}",NhietDoDongLanh); System.Console.WriteLine("Nhiet soi cua nuoc: {0}",NhietDoSoi); @3 C:\WINDOWS\system32\cmd.exe □ Nhiet dong lanh cua nuoc:32 Nhiet soi cua nuoc: 212 Press any key to continue X II v 3.2.3 Kieu liet ke Ki^u b6 sung nhung tinh nang moi thuan tien hon kiSu hing Ki£u liet ke la mpt ki£u gia tri phan biet bao gom mot tap cac ten hang 209 Vi du chung ta tao hang lien quan const int NhietDoDongLanh = 32; // dQ Farenheit const int NhietDoSoi = 212; Chung ta co the bo sung mot so hang khac vao danh sach nhu: const int NhietDoCoTheBoi = 72; //dp Farenheit Qua trinh thuc hien rat cong kenh Do do, chung ta co the dung danh sach liet ke de giai quyet van de: enum NhietDof NhietDoDongLanh= 32, NhietDoSoi = 212, NhietDoCoTheBoi=72, }; Moi kieu liet ke phai co mot kieu ben duoi, chung co the la (int, short, long ) ngoai tru kieu char static void Main(string[] args){ Console.WriteUnef'Nhiet Do Dong Lanh: {0}", (int)NhietDo.NhietDoDongLanh); Console.WriteLinef'Nhiet Do Soi: {0}", (int)NhietDo.NhietDoSoi); Console.WriteLine("Nhiet Do Co The Boi: {0}", (int)NhietDo.NhietDoCoTheBoi); } _ _ □ IsS C:\WINDOWS\system32\cmd.exe Mhiet Do Dong Lanh: Nhiet Do Soi: 212 32 Nhiet Do Co The Boi: 72 Press any key to continue 210 x u 3.2.4 Kieu chuoi Doi tuang string liru tru mot chuoi cac ky tu Khi chung ta khai bao mot bien chuoi su dung tu khda string chung ta phai tao mot the hien cua chuoi: f a string myString; Sau gan gia tri cho bidn myString hay dung khai bao sau: string myString = "Chuong trinh dau tien"; 3.3 Lenh re nhanh 3.3.1 Lenh if else Cu phap: if ( bieu there) l?nh else Ignh Vi du minh hoa lenh re nhanh: So sanh hai so nguyen int valueOne = 10; int valueTwo = 20; if(valueOne>valueTwo) { Console.WriteLine( "Gia tri mot: {0} Ion hon gia tri hai: {1}", valueOne, valueTwo); } else { Console.WriteLinef'Gia tri hai: {0} Ion hon gia tri mot: {1}", valueTwo, valueOne); J • K6t qua chay chuong trinh so sanh hai so nguyen 211 □ | C:\WlNDOWS\system32\cmd.exe X Gia tri hai: 20 Ion non gia tri mot: 10 11 [Press any key to continue Vi du minh hoa lenh re nhanh, giai phuang trinh bac nhat ax + b = va ket qua chay chuang trinh ba truong hop cua phuong trinh bac nhat int a = 1, b = -1; if (a == 0) if(b==O) Console.WritelineC'PTVSN"); else Console.WriteLineC'PTVN"); else Console.WriteLine("PT co nghiem: {0}", -b/a); I Si C:\WIN00WS\9stem32\ciTid.exe — PT co nghiem: Press any key to continue int a = 0, b = 0; if (a == 0) if(b==O) Console WritelineC'PTVSN"); else Console.WriteLine("PTVN"); else Console.WriteLine("PT co nghiem: {0}", -b/a); I S G\WINDOWS\system32\cmd.ae PTVSN Press any key to continue 212 int a = 0, b = 1; if (a == 0) if(b==O) Console.WriteLinef'PTVSN"); else Console.WriteLine("PTVN"); else Console.WriteLinef'PT co nghiem: {0}", -b/a); I S G\WINDOWS\system32\cmd.&e PTVN Press any key to continue , 33.2 Lenh switch (mot thay doi cua lenh if long nhau) Cu phap: switch (expression)! case constant-expression: statement jump-statement [default: statement] } Vi du: int loai = 1; switch (loai){ case 1: Console.WriteLine("Loai 1!"); break; case 2: Console.WriteLine("Loai 2!"); break; 213 case 3: Console.WriteLinef'Loai 3!"); break; default: Console.Writelinef’Khong thuoc loai nao!"); break; } r Ket qua chay chuong trinh vi du ve lenh switch: I G\WINDOWS\system32\cmd.exe ’ □ X Loai 1! Press any key to continue 13 3.4 Lenh lap 3.4.1 Lenh lap while Cu phap: while (bleu there) l?nh; Vi du dung lenh while: Xuat cac gia tri tu int i = 0; while (i < 10){ Console.WriteLine("i = {0}", i); i++; } * f Ket qua chay chuong trinh vi du: Xuat cac gia tri tir -> 214 | S C\WlNDOWS^system32\cmd.exe □ X ,3.4.2 Lenh while Cu phap: bi^U-thtic while Ienh; Vi du: Xu^t cac gia tri tu -> int i = 0; ( Console.WriteLine("i = {0}", i); i++; } while (i < 10); 3.4.3 Lenh for Cu phap: for (khcri tgo; bieu_thi>c; Igp) Ignh; Vi du: Xuat cac gia tri tu -> for (int i = 0; i < 10; i++){ Console.WriteLine("i = {0}", i); } 215 3.4.4 Lenh continue va break * Thinh thoang chung ta mudn quay lai vong lap ma khong can thuc hien cac lenh lai vong lap, r chung ta co the dung lenh continue Nguoc lai, neu chung ta muon thoat khdi vong lap lap turc chung ta co the dung lenh break; 3.5 Toan tfr Mot toan tu la mot ky hieu ma nguyen nhan lam C# thuc hien mot hanh dong Cac kieu co ban C# ho tro cac toan tu nhu gan, cong, tru, tang, giam Cac toan tu tuong tu nhu C++ 3.6 Khong gian ten Be khai bao khong gian ten chung ta su dung tir khoa namespace Vi du: namespace Hello BAI TAP Bai tap 1; Viet chuong trinh de chuyen doi so giay gid, phut, giay Vi du: 13809 = 03h50m09s Z f Bai tap 2\ Viet chuong trinh nhap vao so roi doi sang nam, tuan, Gia su nam co-365 va tu&n co Vi du: 734 = nam tu4n Bai tap 3: Cho mang so nguyen a gom n pMn tu Thuc hien cac thao tac sau 216 - Nhap mang a - Xuat mang a •> - Tinh tong, tich mang a - Tim min, max cua mang a - In cac so nguyen to mang - Sap xep mang giam dan Bai tap 4: Cho ma tran a va b cap m x n Thtrc hien cac thao tac sau - Nhap ma tran a, b - Xuat ma tran a, b X •> - Tinh tong cac phan ttr cua ma tran a - Tim min, max va trung binh cua ma tran cua dong thu i - Tim min, max va trung binh cua ma tran cua cot thui - Tinh tdng a + b - Tinh tich a x b - In cac so nguyen to co ma tran f f ' Bai tap 5: Cho so nguyen dirang a Kiem tra so a co phai la so nguyen to hay khong Bai tap 6: Giai phuong trinh bac 2, Bai tap 7: Cho day so 3, 5, 8, 11, 15, 19, hoi so o vi tri 2019 la so bao nhieu? 217 r r f Bai tap 8: Tim cac so thieu day so sau? -3,9,27, ,729 (9 = 3x3) -3,8,23, ,.,608 (8 = 3x3-1) Bai tap 9: Cho day so 3, 8, 13, 23, , 2008 Tim so hang cua day? ; r r Bai tap 10: Tao mot mang cac so nguyen to va in mang man hinh TAI LIEU THAM KHAO TiGng Anh: [1] Christian Nagel, Professional C# and NET, 2016 [2] Dan Clark, “Beginning C# Object - Oriented Programming”, Springer Science+Business Media New York, 2013 [3] Simon Robinson, Christian Nagel, Karli Watson, Jay Glynn, Morgan Skinner - Bill Evjen, Professional C# [4] Tony Sintes, “Teach Yourself Object Oriented Programming in 21 Days” Sams Publishing, 2002 TiSng Viet: [5] Pham Quang Huy, “Lap trinh hirong doi tuong”, Dai hoc Da Lat, nam 2008 [6] Pham Van At, “C++ va Lap trinh hudng doi tuong”, Khoa hoc Ky thuat, 2000 218 [7] Tran Ngoc Bao, “Lap trinh huang doi tuong”, Dai hoc Sir pham Thanh Ho Chi Minh, 2007 [8] Vu Thanh Hien, “Lap trinh hudng doi tnong”, Dai hoc Khoa hoc til nhien Thanh Ho Chi Minh, 2000 219 GIAO TRINH LAP TRINH HlrtNG flfil THONG Chiu trach nhiem xuat ban: Giam doc - Tong Bien tap DAU XUAN LUAN Chiu trach nhiem ban thao: Gidm doc - Tong Bien t§p DAU XUAN LUAN NGUYEN MANH CVONG Bien t^ip: PHAM THI HOA Trinh bay va siia ban in: DUNG HA Bia: NBA XUAT BAN QUAn DOI NHAN DAN 23 Ly Nam De - H4 Noi DT: 024.38455766 - 37470780; Fax: 024.37471106 Website: httD://nxbadnd.com.vn Email: nxbcidnd@nxbadnd.com.vn Chi nhanh tai Thanh pho' Ho Chi Minh So 161-163 Tran Qudc Thao, phifcfng 9, quan DT: 028.62565588 - 069.667452; Fax: 028.62565588 Cd quan Dai di^n tai Thanh Can Thd Phi trifdng 31 - difcfng Cach mang thdng T&m DT: 069.629905 - 0292.3814772; Fax: 0292.3814772 Cd quan Dai di£n tai Thanh Da Nang S6' 172 difdng 2-9, qu^n Hai Chfiu DT: 0236.6250803; Fax: 0236.6250803 In xong va nop lull chieu: Quy 1/2020 Kho sach: 14.5 x 20.5 cm So trang: 220 S6' lifting: 323 So dang ky KHXB: 674-2020/CXBIPH/2-19/QDND Quyet dinh xuat ban so': 40/QD-NXB QDND Ngay thdng nam 2020 Sap chtf tai: Chi nhanh NXB QDND tai TP.H6 Chi Minh In va dong sach tai: Cong ty co phan In khuyen hoc phia Nam L6 B5-8, difbng D4, KCN Tan Phu Trung, huyen Cu Chi, TP H6 Chi Minh ISBN: 978-604-51-5929-3