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:

Using system; namespace yys. csharpstudy. mainconsole {/// <summary> /// defines a school class. // this class has only attributes and has no method (actually, there is a default constructor method) /// </Summary> public class yschool {/// <summary> /// field, the variables defined in the class are called "fields" // save the school ID /// </Summary> private int id = 0; /// <summary> /// save the school name /// </Summary> private string name = string. empty; // <summary> // attribute. A field is used as a variable to save the attribute value, and the attribute has a special "behavior ". /// 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 ". ///// ID property /// </Summary> Public int ID {get {// get returns a value, indicating the attribute value of the current object. Return this. ID;} // The. sign is used to access the attributes or methods of the object. // This indicates the current object, that is, the instance in which the operation attributes and methods are applied. This indicates the instance. Set {// local variable value. The value is the value that is externally assigned to this attribute. This. id = value ;}/// <summary> // name attributes /// </Summary> Public string name {get {return name ;} set {name = value ;}} public class yteacher {private int id = 0; private string name = string. empty; // The yschool class is used as an attribute of yteacher. Private yschool = NULL; private string introduction = string. empty; private string ImagePath = string. empty; Public int ID {get {return ID;} set {id = value ;}} public string name {get {return name ;}set {name = value ;}} public yschool school {get {If (school = NULL) {school = new yschool ();} return school ;}set {school = value ;}} public String introduction {get {retur N introduction;} set {introduction = value ;}} Public String ImagePath {get {return ImagePath ;}set {ImagePath = value ;}} /// <summary> /// Method for giving lectures to students /// </Summary> Public void toteachstudents () {// {0}, {1 }, {2} is a placeholder, corresponding to the following parameters. Generally, if the displayed content contains parameters, I prefer string. format. Console. writeline (string. Format (@ "{0} instructor Education Students: Good good study, day up! ", This. name ));} /// <summary> // Method for punishing students who make mistakes /// </Summary> /// <Param name = "punishmentcontent"> </param> Public void punishmentstudents (string punishmentcontent) {console. writeline (string. format (@ "{0}'s {1} students who make mistakes {2}", this. school. name, this. name, punishmentcontent);} // field, attribute, and method Prefix: Public, private, protected, internal // public. Fields, attributes, and methods are public, not only can other members of the class be accessed, but also can be accessed through the instance of the class. // 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. // Protected, which includes the private attribute, and the fields, attributes, and Methods Modified by protected can be accessed by the quilt class. // 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. }}
Using system; namespace yys. csharpstudy. mainconsole {class program {static void main (string [] ARGs) {// instantiate a specific object and assign the value yschool shool1 = new yschool (); shool1.id = 1; shool1.name = "中"; yschool school2 = new yschool (); school2.id = 2; school2.name =" 中"; yteacher techers = new yteacher (); techers. id = 1; techers. name = @ "Shang Jin"; techers. school = shool1; techers. introduction = @ "very strict"; techers. imagePath = @ "http: //"; // method of running the current instance techers. toteachstudents (); // method used to run the current instance. input the techers parameter. punishmentstudents (@ "Copy all Tang Poems one hundred times"); console. writeline (); yteacher techerq = new yteacher (); techerq. id = 2; techerq. name = @ "Qin Fen"; techerq. school = school2; techerq. introduction = @ "amiable"; techerq. imagePath = @ "http: //"; techerq. toteachstudents (); techerq. punishmentstudents (@ "Copy all learned mathematical formulas"); console. readkey ();}}}

Result:

Code download: http://download.csdn.net/detail/yysyangyangyangshan/4387330

PS: the code in this article comes from the project you have done.

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.