Lập trình Windows - Lập Trình C #- Lập Trình C Shap - Chương 5 docx

57 738 0
Lập trình Windows - Lập Trình C #- Lập Trình C Shap - Chương 5 docx

Đ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

Generics Generics Nội dung Nội dung  An toàn kiểu lúc biên dịch (compile time type safety)  Overloaded methods  Constraints  Generic Classes and Structs  Generic Interfaces  Generic Methods  Generic Delegates  Generic Type Conversion  Nullable Types 2 Type Safety Type Safety (định kiểu an toàn) (định kiểu an toàn)  Các ngôn ngữ .net (C#, C++, VB ) đều là những ngôn ngữ được định kiểu mạnh (strong type) phải khai báo biến rõ ràng trước khi sử dụng.  Nhưng các kiểu tập hợp (collection) lại không được hỗ trợ kiểu an toàn( Type safety). Ví dụ kiểu ArrayList, chúng cho phép ta lưu trữ tất cả các object bên trong chúng. 3 Ví dụ về an toàn kiểu Ví dụ về an toàn kiểu  namespace TestApp { class Test { static void Main(string[] args) { ArrayList list = new ArrayList(); list.Add(3); list.Add(4); //list.Add(5.0); int total = 0; foreach(int val in list) { total = total + val; } Console.WriteLine( "Total is {0}", total); } } } 4 Type Safety of Generics Type Safety of Generics  Do ArrayList luôn cast mọi thứ về lớp cơ sở Object, nên không có cách nào để kiểm tra kiểu ở thời gian biên dịch.  Dùng Generics sẽ loại trừ việc upcast thành Object và làm cho compiler có thể kiểm tra kiểu được. Nhờ đó có thể tạo collection an toàn kiểu ngay thời gian biên dịch 5 5 Compile-time type safety Compile-time type safety  Phát hiện sai kiểu dữ liệu ở thời điểm biên dịch  Ví dụ 1: nếu Stack được dùng để lưu trữ giá trị int , nếu push vào 1 string thì sẽ gây lỗi (compile-time error).  Ví dụ 2: phương thức Sort chỉ có thể sắp xếp các phần tử cùng kiểu dữ liệu.  Để bảo đảm compile-time type safety, phải tạo các version kiểu khác nhau cho lớp Stack cũng như phương thức Sort  nhiều bản copy cho cùng 1 mã. 6 Generics Generics  Generics là 1 tính chất mới của C#  Generic methods enable you to specify, with a single method declaration, a set of related methods.  Generic classes enable you to specify, with a single class declaration, a set of related classes.  Generic interfaces enable you to specify, with a single interface declaration, a set of related interfaces.  Generics provide compile-time type safety. 7 Generic Generic  Generic cho phép định kiểu an toàn (type safety).  Cho phép tạo ra một cấu trúc dữ liệu mà không cần phải xác định đó là kiểu dữ liệu gì.  Tuy nhiên khi cấu trúc dữ liệu này được sử dụng, trình biên dịch phải đảm bảo rằng kiểu dữ liệu được sử dụng với nó là kiểu an toàn. Generic cũng tương đương vơi Template trong C++ tuy nhiên việc sử dụng Generic trong .net dễ dàng hơn nhiều so với Template. 8 From Overloaded methods From Overloaded methods  Overloaded methods thường được dùng thực thi cùng 1 thao tác trên nhiều loại dữ liệu.  Ví dụ: một console application chứa 3 phương thức overloaded PrintArray dùng để hiển thị các phần tử của mảng int, mảng double và mảng char. 9 10 [...]... contra-variant Concrete classes can implement closed constructed interfaces, as follows: interface IBaseInterface { } class SampleClass : IBaseInterface { } 33 Generic Interfaces  Generic classes can implement • generic interfaces or • closed constructed interfaces  The class parameter list supplies all arguments required by the interface interface IBaseInterface1 { } interface IBaseInterface2 Type... inheritance that apply to classes also apply to interfaces: interface IMonth { } interface IJanuary : IMonth { } //No error interface IFebruary : IMonth { } //No error interface IMarch : IMonth { } //No error //interface IApril : IMonth {} //Error 32 Generic Interfaces   Generic interfaces can inherit from non-generic interfaces if the generic interface is contra-variant Concrete... the containing type The outer type argument to MyClass is hidden from access by the identifier of the inner scope Declare it clearly as follows: public class MyClass { public class MyNestedClass { private T innerfield1; private R innerfield2; } private Containter field1; static void Main() { MyClass closedTypeInstance = null; } } 28 Generic Interfaces  Khai báo generic interface:... generic class can have one or more type parameters separated by commas 22 Name Overloading in generics  Generic types are overloaded based upon the number of arguments in their type argument lists Don’t appear with Don’t appear with public class Container {} public class Container {} simultaneously simultaneously public class Container {} public class Container {} public class Container . ca c phần tử cùng kiểu dữ liệu.  Để bảo đảm compile-time type safety, phải tạo ca c version kiểu kha c nhau cho lớp Stack cũng như phương thư c Sort  nhiều bản copy cho cùng. luôn cast mọi thứ về lớp c sở Object, nên không có cách nào để kiểm tra kiểu ở thời gian biên dịch.  Dùng Generics sẽ loại trừ viê c upcast thành Object và làm cho compiler. và làm cho compiler có thể kiểm tra kiểu đươ c. Nhờ đó có thể tạo collection an toàn kiểu ngay thời gian biên dịch 5 5 Compile-time type safety Compile-time type safety  Phát

Ngày đăng: 12/07/2014, 02:20

Từ khóa liên quan

Mục lục

  • Generics

  • Nội dung

  • Type Safety (định kiểu an toàn)

  • Ví dụ về an toàn kiểu

  • Type Safety of Generics

  • Compile-time type safety

  • Slide 7

  • Generic

  • From Overloaded methods

  • Slide 10

  • Overloaded methods

  • Generic method

  • Generic method declaration

  • Generic method’s body

  • Generic Methods

  • Slide 16

  • Slide 17

  • Slide 18

  • Slide 19

  • Overloading generic method

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

Tài liệu liên quan