tailieunhanh - ASP.NET 4 Unleased - p 77

Các bộ sưu tập có tên _quotes được khai báo trong cơ thể của lớp. Bằng cách đó, bạn có thể tham khảo các trường _quotes trong xây dựng của cả hai và GetQuote sự () phương pháp. | 734 CHAPTER 17 Building Components LISTING using System using public class Quote private List string _quotes new List string public string GetQuote Random rnd new Random return _quotes public Quote All paid jobs absorb and degrade the mind -- Aristotle No evil can happen to a good man either in life or after death --Plato The only good is knowledge and the only evil is ignorance -Plato The collection named _quotes is declared in the body of the class. That way you can refer to the _quotes field in both the constructor and the GetQuote method. NOTE You can create static constructors by using the static keyword when declaring a constructor. A static constructor is called once before any instance constructors. Overloading Methods and Constructors When a method is overloaded a component contains two methods with exactly the same name. Many methods in .NET Framework are overloaded including and . For example here is a list of the three overloaded versions of the method Next Returns a random number between 0 and 2 147 483 647. Next upperbound Returns a number between 0 and the upper bound. Next lowerbound upperbound Returns a number between the lower bound and the upper bound. From the Library of Wow eBook Building Basic Components 735 Because all three methods do the same thing they all return a random number it makes sense to overload the Next method. The methods differ only in their signatures. A method signature consists of the order and type of parameters that a method accepts. For example you can t overload two methods that have exactly the same set of parameters even if the names of the parameters differ . Overloading is useful when you want to associate related methods. Overloading is also useful when you want to provide default values for parameters. For example the StoreProduct component in Listing .