Search C # (attribute one) a week

zhaozj2021-02-16  56

Search C # (attribute one) a week

C # Talent Bird (QQ: 249178521)

Problem

The original package is troublesome

Struct Time

{

...

Public int gethour ()

{

Return Hour;

}

Public void setHour (int value)

{

Hour = Value;

}

...

Private Int Hour, Minute, Second;

}

Static void

Main

()

{

Time Lunch = New Time ();

Lunch.sethour (12);

Console.writeline (Lunch.getHour ());

}

The package hides some unimportant details so you can concentrate on dealing with important content. However, it is difficult to encapsulate, and a typical package misuse is blindly converting the public field to a private field. For example, in the above example, the program defines a private field Hour and the setHour function and the gethour function, not to define a public HOUR field. If the getHour function is only returned to a private field, the sethouse is just a value of the private field, then you will not get any benefits in addition to making the TIME class.

2. Not a solution

· If the field is public, it is easy to use.

W But if you use the public field, you will lose control.

W To simplify instead of simple

Struct Time

{

...

Public int hour;

Public int minute;

Public int special;

}

Static void

Main

()

{

Time Lunch = New Time ();

Lunch.Hour = 30;

Lunch.minute = 12;

Lunch.second = 0;

...

}

The above example uses the public field to make the field of use. For example, you don't write:

Lunch.SetHour (Lunch.Gethour () 1);

And as long as you write:

Lunch.Hour;

However, this simple expression is cost. Considering the above example, the program gives Hour and Minute fields to 30 and 12, respectively. The problem is that 30 is not in the range of HOUR (0-23). But if the field is public, you have no way to capture this error.

So although Get and SET functions are more troublesome, they have advantages in this regard than public fields. The GET and SET functions allow the programmer to control the read and write in the inner field of the class. This is very useful, for example, you can check the parameter range of the set function.

Of course, the most ideal way is to retain the simple and direct expression of the public field and the control provided by the GET and SET functions. (Oh, people always want to be lazy and want to get a lot)

3. Solved approach

· Attributes

W Automatically use the GET identifier to read

W Automatically use the set identifier to write

Struct Time

{

...

Public int hour // No (), is h instead of h

{

Get {...}

Set {...}

}

Private Int Hour, Minute, Second;

}

Time Lunch = New Time ();

...

Lunch.Hour = 12;

...

Console.writeLine (Lunch. Hour);

C # provides a good way to solve the above problems. You can combine the GET and SET functions into a simple properties. The declaration of the property includes an optional access modifier (in an example is a public), the return value (int), the attribute name, and a attribute body containing the GET and SET statements. It is important to note that the attribute is not parenthery because the attribute is not a function. The naming rules of the attribute should meet the general naming rules, namely the public use of the Pascalcase rules, not public use of Camelcase rules. In the above example, the HOUR attribute is public, so named Hour instead of HOUR. The usage of the properties is demonstrated in the example. The syntax and fields used by the attribute, no parentheses. If you want to write an attribute, then you can write this:

Lunch.Hour = 12;

The set statement of the property is automatically executed.

If you want to read an attribute, you can write this:

INT Hour = lunch.Hour;

The attribute Get statement is automatically executed.

4. Get statement

l Get statement

Ø Must return a value with a certain type

Ø Function like a "get function"

Struct Time

{

...

Public int hour

{

get

{

Return Hour;

}

...

}

Private Int Hour, Minute, Second;

}

Time Lunch = New Time ();

... console.writeLine (Lunch. Hour);

/ / Please note that get and set are not a keyword

When reading an attribute, the GET statement of the property is automatically run.

The Get statement must return a value with a certain type. In the above example, the TIME structure class has a integer attribute HOUR, so its Get statement must return a full value.

The return value of the attribute cannot be VOID (from here you can infer the type of field) cannot be a void. This means that the GET statement must contain a complete return statement (RETUN; this form is wrong).

The GET statement can contain any other statements before the Retun statement (for example, check the type of variable), but the return statement cannot be omitted.

Note that get and set are not a keyword, so you can declare a local variable in any place, and the name of the constant is Get or SET, but it is best not to do this.

转载请注明原文地址:https://www.9cbs.com/read-22154.html

New Post(0)