C # class property encapsulation and field explanation,
When I typed the code today, I suddenly felt a bit confused about class attribute encapsulation and fields .. I don't even know the basics. How can the code be rigorous and efficient. Pick up various advanced programming and solutions ~~ This will not write down your understanding (the definition is just a copy of the book), but it will be dedicated to the same confused little friends.
First, let's talk about the following class members, which are divided into data members and function members. Data members can be simply understood as class data, including fields, constants, and events. function members are data in the operation class, contains methods, attributes, constructor, Terminator (destructor), operators, and indexers. (For more information, see the following.) fields are the variables defined in the class, and attributes are the extension of fields. Therefore, before using the attribute, you must combine a normal field declaration. This field is called the backing field of the attribute ):
1 private int numberOfCows; 2 public int NumberOfCows 3 { 4 get 5 { 6 return numberOfCows; 7 } 8 set 9 {10 numberOfCows = value;11 BagOfFeed = numberOfCows * FeedMultiplier;12 }13 }
When reading the NumberOfCows field, the get method is run and the private field numberOfCows is returned. When setting the NumberOfCows field, the set method is called. It seems that this method has no parameter. In fact, value is its parameter, which includes the value set for this field. In fact, this is to use attributes to encapsulate fields (this is encapsulation ).
Encapsulation can also be used to read-only and write-only attributes:
1 public string Name {get; private set;} // read-only 2 public string Name {private get; set;} // write only
In this way, the compiler automatically creates a private string name. This will automatically implement the attributes of the Reserve member variables.
If:
1 public string Name { get; set; }
There are no limited attributes in this way, but they are not very different from public fields.
But from the object-oriented perspective, the public is a method, not a field. Microsoft also recommends that all fields be set as private fields. to use them, encapsulate them as attributes. For such a simple set or get, the compiler will automatically perform inline processing, without the performance loss of function access to the stack. Therefore, we recommend that you do not use public fields. Wear clothes for the fields ~~
In the C language, what is the symbol (->) and how to use it?
This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.
In the C language, what is the symbol (->) and how to use it?
This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.