04. Constructors

Source: Internet
Author: User

Ctrl+r+e generates properties based on fields.
Construction method: (1). The construction method is used to create the object, and the object can be initialized in the constructor method. (2). The constructor method is the special method used to create the object, the method name of the constructor method is the same as the class name, but the constructor method does not return a value, and even void does not write.  (3). The construction method can be privatized. Use private to modify the construction method (there is a problem!) (4). When creating a class, you should create a constructor method, and if you do not create a constructor method, the system defaults to athe method of constructing without parameters.(5). If a constructor method is specified, no longer has a default parameterless constructor, if a parameterless constructor is required .we must override the constructor.(6). This means that constructors can be overloaded.(7). The constructor is executed when instantiating the object.

Use the construction method to solve:

1) Repeat the name of the written object when assigning the initial value

2) If we have a property, the user is not allowed to change. We generally define this property as a read-only Type property.

Then the property of this read-only type cannot be assigned to him after instantiation, so how do we initialize it? We

It can be initialized by the constructor.

we define a class, and if there is no write constructor, then the compiler will automatically add to us in this class A a method of constructing a parameter.

Once we have written a construction method, the compiler will no longer add this to the no-parameter constructor method.


The benefit of the construction method: When instantiating an object, it is possible to assign values to the attributes in the class to which the object is instantiated, avoiding the redundancy of the code.
Keyword NEW: example: Perosn zsperson=new person (); new helps us do a few things? New helps us do three things: (1). Open up space in memory. (2). Create an object in the open space. (3). Call the object's constructor to initialize the object.
This keyword this is useful for: (1). An object that represents the current class. (2). Call the constructor of this class in the class
  
 
  1. /// <summary>
  2. /// 定义了有三个参数的构造函数
  3. /// </summary>
  4. /// <param name="name">姓名</param>
  5. /// <param name="age">年龄</param>
  6. ///<param name= "gender" & gt; gender </param>
  7. public student ( string name int Age char gender this ( name age Gender 0 0 0 ) Span class= "com" >//called the full parameter constructor
  8. {
  9. //this.Name = name;
  10. //this.Age = age; //这里就可以省略冗余代码了
  11. //this.Gender = gender;
  12. }
  13. /// <summary>
  14. /// 定义了一个有六个参数的构造函数
  15. /// </summary>
  16. /// <param name="name">姓名</param>
  17. /// <param name="age">年龄</param>
  18. /// <param name="gender">性别</param>
  19. /// <param name="chinese">语文成绩</param>
  20. /// <param name="math">数学成绩</param>
  21. /// <param name="english">英语成绩</param>
  22. public Student(string name, int age, char gender, double chinese, double math, double english)
  23. {
  24. this.Name = name;
  25. this.Age = age;
  26. this.Gender = gender;
  27. this.Chinese = chinese;
  28. this.Math = math;
  29. this.English = english;
  30. }






From for notes (Wiz)

04. Constructors

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.