Basic knowledge of C #: Basics (3) class construction

Source: Internet
Author: User
We define the Yschool,yteacher class, when instantiating an object:

            Yschool shool1 = new Yschool ();            Shool1.id = 1;            Shool1. Name = "Tsinghua attached";            Yschool school2 = new Yschool ();            School2.id = 2;            School2. Name = "North Normal University attached";

This is wrong, in fact, it is not logically correct, because when instantiating an object, its properties should be the same as the instantiation, rather than adding attributes later. The performance in the program is that the attribute should have an initial value.
Therefore, there is a method in the class that has no return type, the method name is the same as the class name, there is a parameter class table, or there is no parameter list. It is a construction method, commonly known as a "constructor" or "constructor". There can be one or more constructors in a class, and of course sometimes you don't write a constructor, which doesn't mean that the class has no constructors, and it still has a default constructor. If multiple constructors are made, their respective argument lists must be different.
The following takes Yschool as an example to refine this class.

  The ID and name of the <summary>//Yschool class is its intrinsic property, and its value should be deterministic.    A constructor is used to assign an initial value to an attribute while instantiating it.        </summary> public class Yschool {private int id = 0; private string name = String.        Empty;            public int ID {get {return this.id;            }} public string name {get {return Name;        }}///<summary>//////The constructor called "default constructor" or not, and if a constructor is not written, then///The system also provides a default constructor, so that the class has at least one constructor;        Of course, if the default constructor is provided by the system, then the initial value of the property is the value assigned at the time of declaration, or "type default" if it is declared or not assigned, such as 0 or null.            </summary> public Yschool () {this.id = 0;        THIS.name = @ "Tsinghua Middle School";        }///<summary>///A constructor with a parameter list, the value of the///property is the value of the passed-in list.        </summary>//<param name= "id" ></param>//<param name= "name" ></param> public yschool (int ID, stringName) {this.id = ID;        THIS.name = name;            } public yschool (int id) {this.id = ID;        THIS.name = @ "Shaanxi Hkust Secondary School"; }    }
   Class program    {        static void Main (string[] args)        {            Yschool shool1 = new Yschool ();            Yschool school2 = new Yschool (1, @ "NPU attached");            Yschool school3 = new Yschool (2);            Console.readkey ();        }    }

The above also modifies the ID and name of the Get/set property to only get, because these property values are intrinsic properties, and then assigning values after instantiation is not logical. That is, these properties are read-only.

The default value is mentioned in the code, which is briefly described here. When declaring a field, you can use the assignment operator "=" to add a value directly to the field, such as

    String name = String. Empty;

This is not to assign a value to a variable, the default is just a form, really assigns a value to the variable or in the constructor. The Code of the general specification requires the initial value to be assigned when declaring a variable. If there is no field assigned to the life, the compiler adds an assignment code that assigns the field to the default value set. In fact, whether or not we add a default value to the field, the field has a default value, but if we do not artificially increase the default value, the default value of the field will be 0 or null.
One more thing to note here is that the system default constructor is mentioned earlier, but when the constructor is defined, the system default constructor does not exist. So if, in some cases, you need to use both the default constructor and the constructor with parameters, you define the default constructor manually. Like what

        Public Yschool ()        {        }

Or

        Public Yschool ()        {            this.id = 0;            THIS.name = @ "Shang Jin";        }

In summary, the canonical expression of the instantiated class should be: Class name Instance name = new class name (constructor argument list);

The above is the basic knowledge of C #: Basic knowledge (3) of the structure of the 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.