C # basic knowledge sorting: Basic knowledge (2)

Source: Internet
Author: User

Class is the basis of object-oriented language. Three main features of the class: encapsulation, inheritance, and polymorphism. The most basic feature is encapsulation.
Programmers use programs to describe the world and regard everything in the world as an object. How can they describe this object? That is the class. That is, the class is used to encapsulate objects. In the book, classes are the abstraction of objects with the same attributes and behaviors. BMW cars, Buick Cars, Wuling light cars... basically, they have the same attributes and behaviors, so they can abstract a car class. Of course, they can also take Roman's BMW car and passers-by's Buick car... abstract A Car class.
After class abstraction is complete, it can be instantiated. after instantiation, it is called an object, and then it can assign values to attributes or run class methods. Attributes and methods are associated with each object. Different objects have the same attributes, but the attribute values may be different. They also have the same method, but the results of method running may be different.
Class attributes and methods are encapsulated by the class.
See the definition of the following classes:
[Csharp]
1. using System;
2.
3. namespace YYS. CSharpStudy. MainConsole
4 .{
5. /// <summary>
6. // define a school class
7. // this class has only attributes and has no methods (actually, there is a default constructor method)
8. /// </summary>
9. public class YSchool
10 .{
11. /// <summary>
12. // field. The variables defined in the class are called "fields"
13. // save the school ID
14. /// </summary>
15. private int id = 0;
16.
17. // <summary>
18. // save the school name
19. /// </summary>
20. private string name = string. Empty;
21.
22. // <summary>
23. // attribute. The field is used as the variable to save the attribute value, and the attribute has a special "behavior ".
24. /// use get/set to indicate the behavior of attributes. Get gets the property value, and set assigns a value to the property. Therefore, get/set is called "accessors ".
25 .///
26. // ID attribute
27. // </summary>
28. public int ID
29 .{
30. get
31 .{
32. // get returns a value indicating the attribute value of the current object.
33. return this. id;
34 .}
35. // The. sign is used to access the attributes or methods of the object.
36. // this indicates the current object, that is, the instance in which the operation attributes and methods are located. this indicates the instance.
37. set
38 .{
39. // value of the local variable. The value is the value assigned to this attribute externally.
40. this. id = value;
41 .}
42 .}
43. // <summary>
44. // name attributes
45. // </summary>
46. public string Name
47 .{
48. get
49 .{
50. return name;
51 .}
52.
53. set
54 .{
55. name = value;
56 .}
57 .}
58 .}
59.
60. public class YTeacher
61 .{
62. private int id = 0;
63.
64. private string name = string. Empty;
65.
66. // here, the YSchool class is used as an attribute of YTeacher.
67. private YSchool school = null;
68.
69. private string introDuction = string. Empty;
70.
71. private string imagePath = string. Empty;
72.
73. public int ID
74 .{
75. get
76 .{
77. return id;
78 .}
79.
80. set
81 .{
82. id = value;
83 .}
84 .}
85.
86. public string Name
87 .{
88. get
89 .{
90. return name;
91 .}
92.
93. set
94 .{
95. name = value;
96 .}
97 .}
98.
99. public YSchool School
100 .{
101. get
102 .{
103. if (school = null)
104 .{
105. school = new YSchool ();
106 .}
107. return school;
108 .}
109.
110. set
111 .{
112. school = value;
113 .}
114 .}
115.
116. public string IntroDuction
117 .{
118. get
119 .{
120. return introDuction;
121 .}
122.
123. set
124 .{
125. introDuction = value;
126 .}
127 .}
128.
129. public string ImagePath
130 .{
131. get
132 .{
133. return imagePath;
134 .}
135.
136. set
137 .{
138. imagePath = value;
139 .}
140 .}
141.
142. // <summary>
143. // method of giving lectures to students
144. /// </summary>
145. public void ToTeachStudents ()
146 .{
147. // {0}, {1}, {2} are placeholders, corresponding to the following parameters. Generally, if the displayed content contains parameters, I prefer string. Format.
148. Console. WriteLine (string. Format (@ "{0} instructor Education Students: Good Study, Day Up! ", This. name ));
149 .}
150. // <summary>
151. // punishment for students who make mistakes
152. /// </summary>
153. // <param name = "punishmentContent"> </param>
154. public void PunishmentStudents (string punishmentContent)
155 .{
156. console. writeLine (string. format (@ "{0}'s {1} students who make mistakes {2}", this. school. name, this. name, punishmentContent ));
157 .}
158.
159. // pre-modifier for fields, attributes, and Methods: public, private, protected, and internal
160. // public. Fields, attributes, and methods are public. Not only other members of the class can access them, but also can access them through the instance of the class.
161. // private. Fields, attributes, and methods are private. They can only be accessed by other members of the class and cannot be accessed through the instance of the class.
162. // protected, including the private feature, and the fields, attributes, and Methods Modified by protected can be accessed by the quilt class.
163. // internal, which is the same as public in the same assembly, but cannot be accessed by other assembly, and sub-classes can only be accessed by sub-classes of the same namespace.
164 .}
165 .}
 
[Csharp]
1. using System;
2.
3. namespace YYS. CSharpStudy. MainConsole
4 .{
5. class Program
6 .{
7. static void Main (string [] args)
8 .{
9. // instantiate a specific object and assign a value
10. YSchool shool1 = new YSchool ();
11.
12. shool1.ID = 1;
13.
14. shool1.Name = "Tsinghua 中 ";
15.
16. YSchool school2 = new YSchool ();
17.
18. school2.ID = 2;
19.
20. school2.Name = "中 ";
21.
22. YTeacher techerS = new YTeacher ();
23.
24. techerS. ID = 1;
25.
26. techerS. Name = @ "Shang Jin ";
27.
28. techerS. School = shool1;
29.
30. techerS. IntroDuction = @ "very strict ";
31.
32. techerS. ImagePath = @ "http ://";
33.
34. // Method for running the current instance
35. techerS. ToTeachStudents ();
36.
37. // run the method of the current instance and input parameters
38. techerS. PunishmentStudents (@ "Copy all Tang poems that have been learned one hundred times ");
39.
40. Console. WriteLine ();
41.
42. YTeacher techerQ = new YTeacher ();
43.
44. techerQ. ID = 2;
45.
46. techerQ. Name = @ "Qin Fen ";
47.
48. techerQ. School = school2;
49.
50. techerQ. IntroDuction = @ "amiable ";
51.
52. techerQ. ImagePath = @ "http ://";
53.
54. techerQ. ToTeachStudents ();
55.
56. techerQ. PunishmentStudents (@ "Copy all learned mathematical formulas ");
57.
58. Console. ReadKey ();
59 .}
60 .}
61 .}
,
Result
:
Author: yysyangyangyangshan

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.