Basic knowledge of C #: Basic Knowledge (2) class

Source: Internet
Author: User
Class, which is the basis of the object-oriented language. The three main characteristics of a class: encapsulation, inheritance, polymorphism. The most basic feature is encapsulation.
Programmers use programs to describe the world, to treat everything in the world as objects, and how to describe this object? That's the class. That is, classes are used to encapsulate objects. In the words of the book, a class is an abstraction of an object with the same properties and behavior. BMW cars, Buick cars, Wuling light car ... Basically have the same properties and behavior, so can abstract a car class, of course, can also be a passer-by BMW, passers-by B Buick car ... Abstract a car class.
After the class abstraction is complete, you can instantiate, instantiate, call an object, and then assign a value to the property or run a method of the class. Properties and methods are associated with each object, different objects have the same properties, but the property values may be different, and the same methods are available, but the results of the methods may run differently.
The properties and methods of the class are encapsulated by the class.
See the definition of the following class:

Using System;namespace Yys. csharpstudy.mainconsole{///<summary>///define a school class///This class has only attributes, no method (in fact there is a default constructor method)///</summar Y> public class Yschool {//<summary>//////fields, variables defined within the class are called "fields"///Save the school's ID///        </summary> private int id = 0; <summary>///Save the name of the school///</summary> private String name = String.        Empty;        <summary>///properties, fields are variables that hold property values, and properties have a special "behavior". Use Get/set to represent the behavior of the property. Get property value, set assigns a value to the property.        So Get/set is called an "accessor".                ID attribute///</summary> public int ID {get {                Get returns a value that represents the property value of the current object's property.            return this.id;            }//Here the. Number is used to access the properties or methods of an object.            This refers to the current object, which is the instance in which the property and method are manipulated, and this refers to which instance.                The set {//local variable Value,value value is the value that is used externally to assign the property.            This.id = value; }}///&LT;SUMMARY&GT Name attribute///</summary> public string name {get {return}            Name            } set {name = value;        }}} public class Yteacher {private int id = 0; private string name = String.        Empty;        Here the Yschool class is used as a property of the Yteacher.        Private Yschool school = 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 {return IntroDuction;            } set {introDuction = value;            }} public string ImagePath {get {return ImagePath;            } set {ImagePath = value;         }}///<summary>////For Students////</summary> public void toteachstudents () {//{0},{1},{2} is a placeholder that corresponds to the following argument. In general, if the contents of the display contain parameters, I prefer to use string.            Format. Console.WriteLine (String.        Format (@ "{0} Teacher Education students: Good good Study,day day up!", this.name)); }///<summary>//Penalties for mistakes students///</summary>//<param name= "Punishmentconten T></param> public void Punishmentstudents (string punishmentcontent) {Console.WriteLine (s Tring.        Format (@ ' {0} ' {1} teacher let the student who made the mistake {2} ", This.school.Name, THIS.name, punishmentcontent));        }//Fields, properties, and methods pre-modifiers are: public,private,protected,internal//public, Fields, properties, and methods are exposed, not only to other members of the class, but also to the instances of the class that you access.        Private, fields, properties, and methods are private and can only be accessed by other members of the class and cannot be accessed through instances of the class.        Protected, which contains the private attribute, and the fields, properties, and methods that are protected decorated can be accessed by the quilt class.    Internal, like public in the same assembly, but not accessible by other assemblies, and subclasses can only be accessed by subclasses of the same namespace. }}
Using System;namespace Yys.            Csharpstudy.mainconsole{class Program {static void Main (string[] args) {//Instantiate concrete object, and assign value            Yschool shool1 = new Yschool ();            Shool1.id = 1; Shool1.            Name = "Tsinghua attached";            Yschool school2 = new Yschool ();            School2.id = 2; School2.            Name = "North Normal University attached";            Yteacher techers = new Yteacher ();            Techers.id = 1;            Techers.name = @ "Shang Jin";            Techers.school = Shool1;            Techers.introduction = @ "very strict";            Techers.imagepath = @ "/http";            The method of running the current instance techers.toteachstudents ();            Run the current instance of the method, passed in the parameter techers.punishmentstudents (@ "Copy all learned Tang poetry 100 times");            Console.WriteLine ();            Yteacher Techerq = new Yteacher ();            Techerq.id = 2;            Techerq.name = @ "Qinfen";            Techerq.school = School2;            Techerq.introduction = @ "amiable";            Techerq.imagepath = @ "/http"; TEcherq.toteachstudents ();            Techerq.punishmentstudents (@ "Copy all the math formulas you've learned over and over");        Console.readkey (); }    }}

Results:

The above is the basic knowledge of C #: Basic Knowledge (2) class content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.