Lập trình cơ sở dữ liệu C Sharp-Phần 4 pdf

57 1.4K 2
Lập trình cơ sở dữ liệu C Sharp-Phần 4 pdf

Đ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 toan kiêu luc biên dich (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 (đinh kiêu an toan)̣ ̉ ̀ (đinh kiêu an toan)̣ ̉ ̀  Cac ngôn ng .net (C#, C++, VB ) đêu la nh ng ngôn ng ́ ữ ̀ ̀ ữ ữ đ c đinh kiêu manhượ ̣ ̉ ̣ (strong type) phai khai bao biên ro ̉ ́ ́ ̃ rang tr c khi s dung.̀ ướ ử ̣  Nh ng ư cac kiêu tâp h p (collection) lai không ́ ̉ ̣ ợ ̣ đ c ượ hô tr ̃ ợ kiêu an tỏ àn( Type safety). Vi du kiêu ArrayList, chung cho ́ ̣ ̉ ́ phep ta l u tr tât ca cac object bên trong chung.́ ư ữ ́ ̉ ́ ́ 3 Vi du vê an toan kiêú ̣ ̀ ̀ ̉ Vi du vê an toan kiêú ̣ ̀ ̀ ̉  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 moi th vê l p c s Object, nên ̣ ứ ̀ ớ ơ ở không co cach nao đê kiêm tra kiêu th i gian biên ́ ́ ̀ ̉ ̉ ̉ ở ờ dich. ̣  Dung Generics se loai tr viêc upcast thanh Object va ̀ ̃ ̣ ừ ̣ ̀ ̀ lam cho compiler co thê kiêm tra kiêu đ c. Nh đo co ̀ ́ ̉ ̉ ̉ ượ ờ ́ ́ thê tao collection an toan kiêu ngay th i gian biên dich̉ ̣ ̀ ̉ ờ ̣ 5 5 Compile-time type safety Compile-time type safety  Phat hiên sai kiêu d liêu th i điêm biên dich́ ̣ ̉ ữ ̣ ở ờ ̉ ̣  Vi du 1: nêu Stack đ c dung đê l u tr gia tri int ́ ̣ ́ ượ ̀ ̉ ư ữ ́ ̣ , nêu push vao 1 string thi se gây lôi (compile-́ ̀ ̀ ̃ ̃ time error).  Vi du 2: ph ng th c Sort chi co thê săp xêp cac ́ ̣ ươ ứ ̉ ́ ̉ ́ ́ ́ phân t cung kiêu d liêu. ̀ ử ̀ ̉ ữ ̣  Đê bao đam compile-time type safety, phai tao ̉ ̉ ̉ ̉ ̣ cac version kiêu khac nhau cho l p Stack cung ́ ̉ ́ ớ ̃ nh ph ng th c Sort ư ươ ứ  nhiêu ban copy cho ̀ ̉ cung 1 ma. ̀ ̃ 6 Generics Generics  Generics la 1 tinh chât m i cua 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 phep đinh kiêu an toan (type safety). ́ ̣ ̉ ̀  Cho phep tao ra môt câu truc d liêu ma không cân phai ́ ̣ ̣ ́ ́ ữ ̣ ̀ ̀ ̉ xac đinh đo la kiêu d liêu gi. ́ ̣ ́ ̀ ̉ ữ ̣ ̀  Tuy nhiên khi câu truc d liêu nay đ c s dung, trinh ́ ́ ữ ̣ ̀ ượ ử ̣ ̀ biên dich phai đam bao răng kiêu d liêu đ c s dung ̣ ̉ ̉ ̉ ̀ ̉ ữ ̣ ượ ử ̣ v i no la kiêu an toan. Generic cung t ng đ ng v i ớ ́ ̀ ̉ ̀ ̃ ươ ươ ơ Template trong C++ tuy nhiên viêc s dung Generic ̣ ử ̣ trong .net dê dang h n nhiêu so v i Template.̃ ̀ ơ ̀ ớ 8 From Overloaded methods From Overloaded methods  Overloaded methods th ng đ c dung th c thi cung 1 ườ ượ ̀ ự ̀ thao tac trên nhiêu loai d liêu.́ ̀ ̣ ữ ̣  Vi du: môt console application ch a 3 ph ng th c ́ ̣ ̣ ứ ươ ứ overloaded PrintArray dung đê hiên thi cac phân t cua ̀ ̉ ̉ ̣ ́ ̀ ử ̉ mang int, mang double va mang 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 0) { temp = lhs; lhs = rhs; rhs = temp; } } 19 Overloading generic method    Generic method có thể overloaded Môt class có thể cung c p 2 hay nhiêu generic methods có ̣ ́ ̀ cung tên nhưng tham số khac nhau ̀ ́ Môt generic method có thể overloaded với 1 method ̣ generic khac hay với cac method non-generic ́ ́ void DoWork()... 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... public class Container {} public class Container {} public class Container {} //public class Container {} 23 23 Name Overloading in generics   Each generic Container identifier distinguishs it with another thanks to a number of type parameters The name overloading rules for generic declarations are based on the count of type parameters rather than the names given to their placeholders... thư c PrintArray, loai phân tử mang (int, ̣ ̀ ̉ double or char) xuât hiên tai 2 vị trí trong môi phuơng thư c ́ ̣ ̣ ̃ Thay thế cac loai nay thanh 1 tên chung (generic name) ́ ̣ ̀ ̀ Cả 3 phương thư c sẽ có chung 1 dang sau: ̣ 11 Generic method    Nêu nhiêu overloaded methods chỉ khac nhau ́ ̀ ́ ở kiêu dữ liêu cua cac đôi sô, nên dung ̉ ̣ ̉ ́ ́ ́ ̀ generic method Môt khai bao generic method... Generic Interfaces   When an interface is specified as a constraint on a type parameter, only types that implement the interface can be used Multiple interfaces can be specified as constraints on a single type, as follows: class Stack where T : System.IComparable, IEnumerable { } 31 Generic Interfaces  An interface can define more than one type parameter, as follows: interface IDictionary . ̉ ̉ ̣ cac version kiêu khac nhau cho l p Stack cung ́ ̉ ́ ớ ̃ nh ph ng th c Sort ư ươ ứ  nhiêu ban copy cho ̀ ̉ cung 1 ma. ̀ ̃ 6 Generics Generics  Generics la 1 tinh chât m i cua C# ̀ ́ ́. Generics Generics Nôi dung̣ Nôi dung̣  An toan kiêu luc biên dich (compile time type safety)̀ ̉ ́ ̣  Overloaded methods  Constraints  Generic Classes and Structs  Generic Interfaces  Generic.  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 phep

Ngày đăng: 21/06/2014, 09: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