Bài giảng Lập trình môi trường Window: Chương 2 - Ngô Thanh Hùng

139 5 0
Bài giảng Lập trình môi trường Window: Chương 2 - Ngô Thanh Hùng

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Tiếp nối chương 1, Bài giảng Lập trình môi trường Window: Chương 2 có nội dung trình bày về ngôn ngữ C#, kiểu dữ liệu trong C#; các nhóm toán tử trong C#; lập trình hướng đối tượng (OOP) trong C#; giao diện và ví dụ các câu lệnh.

NGƠN NGỮ C# CuuDuongThanCong.com https://fb.com/tailieudientucntt C# Ngơn ngữ lập trình “thuần” hướng đối tượng 70% Java, 10% C++, 5% Visual Basic, 15% Trình biên dịch C# trình biên dịch hiệu dịng sản phẩm NET 24/02/2009 CuuDuongThanCong.com Lập Trình mơi trường Windows https://fb.com/tailieudientucntt Đặc điểm ngôn ngữ C#  Khoảng 80 từ khóa  Hỗ trợ lập trình cấu trúc, lập trình hướng đối tượng, hướng thành phần (Component oriented)  Có từ khóa khai báo dành cho thuộc tính (property)  Cho phép tạo sưu liệu trực tiếp bên mã nguồn (dùng tool mã nguồn mở NDoc phát sinh sưu liệu)  Hỗ trợ khái niệm interface (tương tự java)  Cơ chế tự động dọn rác (tương tự java)  Truyền tham số kiểu: in(ø), out, ref 24/02/2009 CuuDuongThanCong.com Lập Trình mơi trường Windows https://fb.com/tailieudientucntt 24/02/2009 Cấu trúc chương trình C# CuuDuongThanCong.com Lập Trình mơi trường Windows https://fb.com/tailieudientucntt Hello World using System; class Hello { public static void Main() { Console.WriteLine("Hello, World"); } } 24/02/2009 CuuDuongThanCong.com Lập Trình mơi trường Windows https://fb.com/tailieudientucntt Namespace Namespace cung cấp cho cách tổ chức quan hệ lớp kiểu khác Namespace cách mà NET tránh né việc tên lớp, tên biến, tên hàm trùng tên lớp namespace CustomerPhoneBookApp { using System; public struct Subscriber { // Code for struct here } } 24/02/2009 CuuDuongThanCong.com Lập Trình mơi trường Windows https://fb.com/tailieudientucntt Namespace Từ khoá using giúp giảm việc phải gõ namespace trước hàm hành vi thuộc tính using Wrox.ProCSharp; Ta gán bí danh cho namespace Cú pháp : using alias = NamespaceName; 24/02/2009 CuuDuongThanCong.com Lập Trình mơi trường Windows https://fb.com/tailieudientucntt Ví dụ 01 02 03 04 05 06 07 08 09 10 /* Chương trình C#*/ class Hello { static void Main(string[] args) { System.Console.WriteLine(“Hello C Sharp”); System.Console.ReadLine(); } } Để biên dịch Class, sử dụng tập tin csc.exe cửa sổ Command Prompt với khai báo sau: D:\csc CSharp\ Hello.cs 24/02/2009 CuuDuongThanCong.com Lập Trình mơi trường Windows https://fb.com/tailieudientucntt Ví dụ 01 02 03 04 05 06 07 08 09 10 /* Chương trình C#*/ using System; class Hello { static void Main(string[] args) { Console.WriteLine(“Hello C Sharp”); Console.ReadLine(); } } Để biên dịch Class, sử dụng tập tin csc.exe cửa sổ Command Prompt với khai báo sau: D:\csc CSharp\ Hello.cs 24/02/2009 CuuDuongThanCong.com Lập Trình mơi trường Windows https://fb.com/tailieudientucntt Ví dụ 01 02 03 04 05 06 07 08 09 10 /* Chương trình C#*/ using Con=System.Console; class Hello { static void Main(string[] args) { Con.WriteLine(“Hello C Sharp”); Con.ReadLine(); } } Để biên dịch Class, sử dụng tập tin csc.exe cửa sổ Command Prompt với khai báo sau: D:\csc CSharp\ Hello.cs 24/02/2009 CuuDuongThanCong.com 10 Lập Trình môi trường Windows https://fb.com/tailieudientucntt CuuDuongThanCong.com https://fb.com/tailieudientucntt Phát biểu throw Phát biểu throw dùng để phát tín hiệu cố bất thường chương trình thực thi với cú pháp: throw [expression]; CuuDuongThanCong.com https://fb.com/tailieudientucntt using System; public class ThrowTest { public static void Main() { string s = null; if (s == null) { throw(new ArgumentNullException()); } Console.Write("The string s is null"); // not executed } } CuuDuongThanCong.com https://fb.com/tailieudientucntt public class Test { public static void Main() { Console.WriteLine(“Enter Main ”); Test t = new Test(); t.Func1(); Console.WriteLine(“Exit Main ”); } public void Func1() { Console.WriteLine(“Enter Func1 ”); Func2(); Console.WriteLine(“Exit Func1 ”); } public void Func2() { Console.WriteLine(“Enter Func2 ”); throw new System.Exception(); Console.WriteLine(“Exit Func2 ”); } } Lập Trình mơi trường Windows 128 24/02/2009 CuuDuongThanCong.com https://fb.com/tailieudientucntt Enter Main Enter Func1 Enter Func2 Exception occurred: System.Exception: An exception of type System.Exception was throw at Programming_CSharp.Test.Func2() in exception01.cs:line 26 at Programming_CSharp.Test.Func1() in exception01.cs:line 20 at Programming_CSharp.Test.Main() in exception01.cs:line 12 24/02/2009 CuuDuongThanCong.com 129 Lập Trình môi trường Windows https://fb.com/tailieudientucntt Phát biểu try catch  Trong C#, trình xử lý ngoại lệ hay đoạn chương trình xử lý ngoại lệ gọi khối catch tạo với từ khóa catch  Ví dụ: câu lệnh throw thực thi bên khối try, khối catch sử dụng để công bố CuuDuongThanCong.com https://fb.com/tailieudientucntt public void Func2() { Console.WriteLine(“Enter Func2 ”); try { Console.WriteLine(“Entering try block ”); throw new System.Exception(); Console.WriteLine(“Exiting try block ”); } catch { Console.WriteLine(“Exception caught and handled.”); } Console.WriteLine(“Exit Func2 ”); } 24/02/2009 CuuDuongThanCong.com 131 Lập Trình mơi trường Windows https://fb.com/tailieudientucntt Enter Main Enter Func1 Enter Func2 Entering try block Exception caught and handled Exit Func2 Exit Func1 Exit Main 24/02/2009 CuuDuongThanCong.com 132 Lập Trình môi trường Windows https://fb.com/tailieudientucntt public void Func1() { Console.WriteLine(“Enter Func1 ”); try { Console.WriteLine(“Entering try block ”); Func2(); Console.WriteLine(“Exiting try block ”); } catch { Console.WriteLine(“Exception caught and handled.”); } Console.WriteLine(“Exit Func1 ”); } 24/02/2009 CuuDuongThanCong.com 133 Lập Trình mơi trường Windows https://fb.com/tailieudientucntt 24/02/2009 public void Func2() { Console.WriteLine(“Enter Func2 ”); throw new System.Exception(); Console.WriteLine(“Exit Func2 ”); } Enter Main Enter Func1 Entering try block Enter Func2 Exception caught and handled Exit Func1 Exit Main CuuDuongThanCong.com 134 Lập Trình mơi trường Windows https://fb.com/tailieudientucntt 24/02/2009 Ví dụ class Test { static void Main(string[] args) { Test t = new Test(); t.TestFunc(); } public double DoDivide(double a, double b) { if ( b == 0) throw new System.DivideByZeroException(); if ( a == 0) throw new System.ArithmeticException(); return a/b; } CuuDuongThanCong.com 135 Lập Trình mơi trường Windows https://fb.com/tailieudientucntt public void TestFunc() { try { double a = 5; double b = 0; Console.WriteLine("{0} / {1} = {2}", a, b, DoDivide(a,b)); } catch (System.DivideByZeroException) { Console.WriteLine("DivideByZeroException caught!"); } catch (System.ArithmeticException) { Console.WriteLine("ArithmeticException caught!"); } catch { Console.WriteLine("Unknown exception caught"); } } CuuDuongThanCong.com 136 24/02/2009 Lập Trình mơi trường Windows https://fb.com/tailieudientucntt Câu lệnh finally Đoạn chương trình bên khối finally đảm bảo thực thi mà không quan tâm đến việc ngoại lệ phát sinh try try-block catch catch-block finally finally-block 24/02/2009 CuuDuongThanCong.com 137 Lập Trình mơi trường Windows https://fb.com/tailieudientucntt Câu lệnh finally Dòng thực thi bước vào khối try Nếu khơng có lỗi xuất hiện, - tiến hành cách bình thường xuyên suốt khối try, đến cuối khối try, dòng thực thi nhảy đến khối finally ( bước 5), - lỗi xuất khối try,thực thi nhảy đến khối catch ( bước tiếp theo) Trạng thái lỗi xử lí khối catch vào cuối khối catch , việc thực thi chuyển cách tự động đến khối finally 24/02/2009 CuuDuongThanCong.com 138 Lập Trình mơi trường Windows https://fb.com/tailieudientucntt Tạo riêng ngoại lệ  phải dẫn xuất từ System.ApplicationException 24/02/2009 CuuDuongThanCong.com 139 Lập Trình mơi trường Windows https://fb.com/tailieudientucntt ... in(ø), out, ref 24 / 02/ 2009 CuuDuongThanCong.com Lập Trình mơi trường Windows https://fb.com/tailieudientucntt 24 / 02/ 2009 Cấu trúc chương trình C# CuuDuongThanCong.com Lập Trình mơi trường Windows... Percent: {1:P} " + "(R) Round-trip: {1:R} " + "(X) Hexadecimal: {0:X} ", - 123 , - 123 .45f); 24 / 02/ 2009 CuuDuongThanCong.com 12 Lập Trình mơi trường Windows https://fb.com/tailieudientucntt...  Double.Parse() 24 / 02/ 2009 CuuDuongThanCong.com 14 Lập Trình mơi trường Windows https://fb.com/tailieudientucntt 24 / 02/ 2009 Kiểu liệu C# CuuDuongThanCong.com 15 Lập Trình mơi trường Windows https://fb.com/tailieudientucntt

Ngày đăng: 17/04/2022, 09:52

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan