C # object-oriented learning-fields and attributes,
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 7 namespace ClassDemo 8 {9 // <summary> 10 // student class 11 /// </summary> 12 // summary: 13 // field: (member variable) 14 // The field is mainly used for internal data interaction of the class, generally private; 15 // The field can be assigned a value, you can also set a value. 16 // when a field needs to provide external data, encapsulate the field as an attribute instead of using a public field. This is what object-oriented means; 17 // attribute: 18 // The attribute generally provides external data and is mainly used to describe the static features of the object. Therefore, the general attribute is public; 19 // attributes can be set to read-only and write-only as needed to improve data security; 20 // The attribute can add the business logic we need internally to avoid illegal data; 21 class Student22 {23 // <summary> 24 // field: Also known as a member variable. It is generally used for data interaction within the class, so private modification is generally used; 25 // field naming rules: Generally, the camel naming method is used for field naming. The first letter is lowercase 26 // a common understanding of the field: The field is like our personal property and is only for our own use, therefore, it is generally private modification; 27 // Add standard: several fields need to be added to a class based on the needs of the programming process; 28 /// </summary> 29 // field: Student ID 30 private int studentId; 31 // field: Name 32 private string studentName = string. empty; 33 // field: age 34 private int age; 35 36 37 38 // <summary> 39 // attribute is the entry to the private field for external access, the attribute itself does not store any data 40 // assign a value to the attribute, in fact, it is to assign a value to the private field of the attribute pointing to the field; 41 // read the attribute value, in fact, it is to obtain the private field value or other values pointed to by the property; 42 // The attribute is generally capitalized; 43 // </summary> 44 // The property: student ID 45 public int StudentId46 {47 get {return studentId;} 48 set {studentId = value;} 49} 50 51 // attribute: student name 52 public string StudentName53 {54 get {return studentName;} 55 set {studentName = value;} 56} 57 58 // attribute: age 59 public int Age60 {61 // Add constraints to attributes; 62 get {return age;} 63 set {64 if (value <18) 65 age = 18; 66 else67 age = value; 68} 69} 70 71 // method: obtain student information; 72 // <summary> 73 // access modifier, which can be omitted, the default is private. You can change it to public74 // method name as needed. It is generally a "verb" or "verb phrase". It uses the Pascal naming method and the first letter is capitalized, the parameter list cannot start with a number. // The parameter list can be added as needed or omitted. The parameter list must be in the form of "type parameter name", and different parameters must be separated by commas; 76 // return value: return is used; no other statements can be followed by return statements; 77 // No return Value: If no data is returned, viod is used; 78 // </summary> 79 // <returns> </returns> 80 81 public string GetStudent () 82 {83 string info = string. format ("Name: {0} student ID: {1}", studentId, studentName); 84 return info; 85} 86 // automatic attribute prop + tab + tab87 // Applicability: private fields cannot be operated directly, read-only attributes cannot be set, and verification logic cannot be added; 88 public int MyProperty {get; set;} 89} 90}