C# Chapter III notes 2021-9-27
1. C# access modifier
The access modifiers and scope of action in C # are as follows:
Access modifier | explain |
---|---|
public | Shared access. No restrictions. |
private | Private access. Only this class can access, but instances cannot. |
protected | Secure access. Access is limited to this class and subclasses, and instances cannot be accessed. |
internal | Internal access. Access is limited to this project, and others cannot be accessed |
protected internal | Internally protected access. Access is limited to this project or subclass, and others cannot be accessed |
The modifiable and default modifiers of C# member types are as follows:
Member type | Default modifier | Can be modified |
---|---|---|
enum | public | none |
class | private | public,protected,internal,private, protected internal |
interface | public | none |
struct | private | public,internal,private |
public has the highest access level
private access level is the lowest
2. this keyword
Look at the following code. What's the problem
class Strdent { private string _name; //full name public int _age = 19; //Age public string _cardID = "145236985674526685"; //ID card No. public void SetName(string _name) { _name = _name; } }
Analysis: a private member variable is defined in the Student class_ Name, a variable with the same name is also defined in the parameter of SetName() method_ name. At this time, the compiler will find that the parameter names of member variables and methods are the same.
At this point, the compiler cannot distinguish between the two that appear in the code_ name is the member variable and which is the parameter in the method. We can use this keyword to solve this problem.
This keyword refers to the current object itself. Through this, you can reference the member variables and methods of the current class.
Therefore, the above code can be changed to:
class Strdent { private string _name; //full name public int _age = 19; //Age public string _cardID = "145236985674526685"; //ID card No. public void SetName(string _name) { this._name = _name; } }
Using this keyword can solve the problem of name conflict between member variables and local variables.
3. C # attribute
3.1. Use methods to ensure data security
Based on the student class definition, write and create an instance student of the Strdent class, and re check the age of the object_ Assign a value to a value.
Student student = new Student(); student._age = -2; student._cardID = "1433223"; student.SetName("Wang Lili"); student.Say(); //Printing method
After the program is run, the age of the target student becomes "-2" and the ID number is changed to "1433223" at will.
It is illogical to arbitrarily modify the assignment of public variables of objects.
We can solve this problem through class methods. You can define a method with parameters to implement
Implementation idea: pass the age entered by the console as the parameter of this method to the method, and use the if structure to judge the value of age in the method. as follows
///Student class class Student { private string _name; //full name private int _age; //Age public string -cardID; //ID number //set - get method public int GetAge() { return this._age; } public void SetAge(int age) { // if structure judges the value of age if(age < 0 || age > 100) { this._age = 19; } else { this._age = age; } } } ///Test class class Program { static void Main(string[] args) { Student student = new Student(); student.SetAge(-10); Console.WriteLine("Age is:{0}",student.GetAge()); student.SetAge(25); Console.WriteLine("Age is:{0}",student.GetAge()); Console.ReadLine(); } }
3.2. Realize field encapsulation with attributes
3.2.1 C# attribute
In C #, fields are usually private. If you want to access the fields in the class, you need to implement it through get and set accessors. This implementation method combining fields and methods is called property
The syntax is as follows:
private string _name; public string Name { get{ return _name;} set{ _name = value;} }
get read set change
3.2.2 data type of attribute
When defining a property in a class, the data type of the property must be consistent with the field type it accesses..
For example: Age field_ If Age is an integer, its attribute Age must also be an integer.
3.2.3. Access type of attribute
Property can not only restrict data access, but also set read and write properties to limit its access type. There are three types of access to properties.
- Read only property that contains only get accessors.
- Write only properties, including only set accessors.
- Read and write properties, including get and set accessors.
The flexible use of get and set accessors can ensure the security of fields in the class
3.2.4. Quickly create attributes in coding
Visual Studio provides a quick method:
In a class, define a field, which is usually set to private use. Select this field, right-click it, and select from the pop-up shortcut menu:
Refactor → encapsulate field option. As shown in the figure:
3.2.5 what are the C# fields and attributes?
The field is usually specified as private and used inside the class. Specify the property as public, expose it externally, and provide secure and effective range protection for the field through get or set accessors.
3.2.6 what are the differences between attributes and methods?
"()" is not used after the property set accessor and get accessor in C# because the accessor does not return a value, so it is not necessary to specify void.
3.2.7 object initializer
Generally, we instantiate a Student object first, and then assign a value to the attribute, as follows:
Student student = new Student(); student.Age = -20;
In C #, an object initializer is provided to more conveniently assign values to the attributes of the object, as follows:
Student student = new Student(){ Age = -20 };
When there are multiple attributes in the class, the object initializer can be used to assign values to multiple attributes at the same time. Multiple attributes are separated by commas and enclosed by {}. As follows:
This article is a simple study note. Please indicate the error if usedStudent student = new Student(){ Age = -20,Name = "zhagnsan"};