Chapter 5 category (2)
5.3 class attributes
There are two ways to reveal the naming attributes of a class-either through a domain member or through a property. The former is implemented as a member variable with public access; the latter does not directly respond to the storage location, but is only accessed through the accessors.
When you want to read or write the attribute value, the access flag limits the implemented statement. The access Mark used to read the attribute value is recorded as the keyword get, and the read/write mark used to modify the attribute value is recorded as set.
Before you understand this theory, take a look at the example in listing 5.9. The SquareFeet attribute is labeled with the get and set access signs.
Listing 5.9 attribute access flag
1: using System;
2:
3: public class House
4 :{
5: private int m_nSqFeet;
6:
7: public int SquareFeet
8 :{
9: get {return m_nSqFeet ;}
10: set {m_nSqFeet = value ;}
11 :}
12 :}
13:
14: class TestApp
15 :{
16: public static void Main ()
17 :{
18: House myHouse = new House ();
19: myHouse. SquareFeet = 250;
20: Console. WriteLine (myHouse. SquareFeet );
21 :}
22 :}
The House class has an attribute named SquareFeet, which can be read and written. The actual value is stored in a variable that can be accessed from inside the class -- if
You want to rewrite it as a domain member. All you need to do is to ignore the accesskey and redefine the variable:
Public int SquareFeet;
This is good for such a simple variable. However, if you want to hide the details of the class's internal storage structure, you should use an access flag. In this situation
The set access flag transmits a new value to the attribute in the value parameter. (You can change the name. For more information, see row 10th .)
In addition to hiding implementation details, you can also freely limit various operations:
Get and set: allow read/write access to properties.
Get only: Read attribute values only.
Set only: only attribute values can be written.
In addition, you can get the opportunity to implement valid code in the set flag. For example, for various reasons (or no reason at all), you can reject a new
Value. It is better that no one tells you that it is a dynamic attribute-when you request it for the first time, it will be saved, so we should postpone resource allocation as much as possible.
5.4 Index
Have you ever thought about using the index category class like accessing an array? With the index function of C #, you can expect it to be finished.
The syntax is basically like this:
Attribute modifier declaration {declaration content}
The specific example is
Public string this [int nIndex]
{
Get {...}
Set {...}
}
Returns an index or sets a string based on the given index. It has no attributes, but uses the public modifier. The declaration part consists of the string type and this type for the table.
Class index.
The execution rules of get and set are the same as those of attributes. (You cannot cancel one of them .) There is only one difference, that is, you can almost define anything
Parameters in the ARC. The limit is that at least one parameter must be specified, and the ref and out modifiers are allowed.
This keyword ensures an explanation. The index does not have a user-defined name. this indicates the index of the default Interface. If the class implements multiple interfaces, you can add
Multiple indexes described by InterfaceName. this.
To demonstrate the use of an index, I created a small class that can resolve a host name as an IP address-or an IP address list (
Http://www.microsoft.com as an example ). This list can be accessed through indexes. You can refer to the specific implementation of listing 5.10.
Listing 5.10 getting an IP address through an index
1: using System;
2: using System. Net;
3:
4: class ResolveDNS
5 :{
6: IPAddress [] m_arrIPs;
7:
8: public void Resolve (string strHost)
9 :{
10: IPHostEntry iphe = DNS. GetHostByName (strHost );
11: m_arrIPs = iphe. AddressList;
12 :}
13:
14: public IPAddress this [int nIndex]
15 :{
16: get
17 :{
18: return m_arrIPs [nIndex];
19 :}
20 :}
21:
22: public int Count
23 :{
24: get {return m_arrIPs.Length ;}
25 :}
26 :}
27:
28: class DNSResolverApp
29 :{
30: public static void Main ()
31 :{
32: ResolveDNS myDNSResolver = new ResolveDNS ();
33: myDNSResolver. Resolve ("http://www.microsoft.com ");
34:
35: int nCount = myDNSResolver. Count;
36: Console. WriteLine ("Found {0} IPs for hostname", nCount );
37: for (int I = 0; I <nCount; I ++)
38: Console. WriteLine (myDNSResolver [I]);
39 :}
40 :}
To resolve the host name, I used the DNS class, which is part of the System. Net namespace. However, this namespace is not included in the core
Library, so the library must be referenced in the compilation command line:
Csc/r: System. Net. dll/out: resolver.exe dnsresolve. cs
The parsing code is parsed forward. In this Resolve method, the Code calls the static method GetHostByName of the DNS class and returns an IPHostEntry
Object. Result: The object contains the AddressList array, which is the array I am looking. Before exiting the Resolve method
M_arrIPs stores a copy of AddressList array (the IPAddress object type is stored in it ).
With the generated array, the application code can cite IP addresses in the columns 37th to 38 by using the index obtained in the ResolveDNS class.
(In chapter 6th "control statements", you can find more information about statements .) Because there is no way to change the IP address, only the get access flag is used for the index. To
In a nutshell, I ignore the array boundary overflow check.
5.4 events
When you write a class, it is sometimes necessary to let the class customers know the events that have occurred. If you are a programmer with years of programming experience
There are many solutions, including function pointers for callback and event sinks for ActiveX controls ). Now you are going to learn another
How to associate user code with class notifications-use events.
Events can be declared as class domain members (member variables) or attributes. The commonality between the two is that the event type must be the element, and the letter
The prototype of the number pointer has the same meaning as the representative element of C.
Each event can be occupied by zero or more customers, and the customer can associate or cancel the event at any time. You can use static or instance-based definitions
And the latter is very popular with C ++ programmers.
Now that I have mentioned all the functions of the event and the corresponding representative yuan, see the example in listing 5.11. It vividly embodies this theory.
List 5.11 implement event processing in the class
1: using System;
2:
3: // Forward Declaration
4: public delegate void EventHandler (string strText );
5:
6: class EventSource
7 :{
8: public event EventHandler TextOut;
9:
10: public void TriggerEvent ()
11 :{
12: if (null! = TextOut) TextOut ("Event triggered ");
13 :}
14 :}
15:
16: class TestApp
17 :{
18: public static void Main ()
19 :{
20: EventSource evsrc = new EventSource ();
21:
22: evsrc. TextOut + = new EventHandler (CatchEvent );
23: evsrc. TriggerEvent ();
24:
25: evsrc. TextOut-= new EventHandler (CatchEvent );
26: evsrc. TriggerEvent ();
27:
28: TestApp theApp = new TestApp ();
29: evsrc. TextOut + = new EventHandler (theApp. InstanceCatch );
30: evsrc. TriggerEvent ();
31 :}
32:
33: public static void CatchEvent (string strText)
34 :{
35: Console. WriteLine (strText );
36 :}
37:
38: public void InstanceCatch (string strText)
39 :{
40: Console. WriteLine ("Instance" + strText );
41 :}
42 :}
Row 4th declares the representative element (the original event method), which is used to declare the TextOut event Domain Member for the EventSource class in row 8th. You can observe the generation
As a new type declaration, table meta can be used to declare an event.
This class has only one method, which allows us to trigger events. Please note that you must check if the event Domain Member is not null, because there may be no customers
Users are interested in the event.
The TestApp class contains the Main method and the other two methods, both of which have the signal required for the event. One method is static, while the other
One is the instance method.
EventSource is instantiated, while the static method CatchEvent is pre-associated with the TextOut event:
Evsrc. TextOut + = new EventHandler (Cat