C # Basic Knowledge Organization: Basic knowledge (3) Construction

Source: Internet
Author: User

We define the YSchool and YTeacher classes. when instantiating an object:

[Csharp]
YSchool shool1 = new YSchool ();
 
Shool1.ID = 1;
 
Shool1.Name = "Tsinghua 中 ";
 
YSchool school2 = new YSchool ();
 
School2.ID = 2;
 
School2.Name = "中 ";
YSchool shool1 = new YSchool ();

Shool1.ID = 1;

Shool1.Name = "Tsinghua 中 ";

YSchool school2 = new YSchool ();

School2.ID = 2;

School2.Name = "中 ";
If this is the case, the actual logic is incorrect, because when an object is instantiated, its attributes should be available together with the instantiation, rather than adding attributes later. In a program, the attribute must have an initial value.
Therefore, there must be such a method in the class. There is 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 constructor or constructor ". One or more constructors can exist in a class. Of course, sometimes you do not write constructor. This does not mean that this class does not have constructor, and it still has a default constructor. If multiple constructors are used, their parameter lists must be different.
The following uses YSchool as an example to improve this class.

[Csharp]
/// <Summary>
/// The id and name of the YSchool class are its inherent attributes, and its value should be determined.
/// The initial value is assigned to the attribute while being instantiated, And the constructor is used.
/// </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 without parameters is called the "default constructor". If no constructor is written
/// The System also provides a default constructor. Therefore, the class must have at least one constructor;
/// Of course, if the system provides the default constructor, the initial attribute value is the initial value assigned during declaration.
/// If the initial value is not assigned, it is the "type default value", such as 0 or null.
/// </Summary>
Public YSchool ()
{
This. id = 0;
 
This. name = @ "中 ";
}
/// <Summary>
/// Constructor with parameter list,
/// The attribute value is the value of the input list.
/// </Summary>
/// <Param name = "id"> </param>
/// <Param name = "name"> </param>
Public YSchool (int id, string name)
{
This. id = id;
 
This. name = name;
}
Public YSchool (int id)
{
This. id = id;
 
This. name = @ "Shaanxi ";
}
}
/// <Summary>
/// The id and name of the YSchool class are its inherent attributes, and its value should be determined.
/// The initial value is assigned to the attribute while being instantiated, And the constructor is used.
/// </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 without parameters is called the "default constructor". If no constructor is written
/// The System also provides a default constructor. Therefore, the class must have at least one constructor;
/// Of course, if the system provides the default constructor, the initial attribute value is the initial value assigned during declaration.
/// If the initial value is not assigned, it is the "type default value", such as 0 or null.
/// </Summary>
Public YSchool ()
{
This. id = 0;

This. name = @ "中 ";
}
/// <Summary>
/// Constructor with parameter list,
/// The attribute value is the value of the input list.
/// </Summary>
/// <Param name = "id"> </param>
/// <Param name = "name"> </param>
Public YSchool (int id, string name)
{
This. id = id;

This. name = name;
}
Public YSchool (int id)
{
This. id = id;

This. name = @ "Shaanxi ";
}
} [Csharp]
Class Program
{
Static void Main (string [] args)
{
YSchool shool1 = new YSchool ();
 
YSchool school2 = new YSchool (1, @ "");
 
YSchool school3 = new YSchool (2 );
 
Console. ReadKey ();
}
}
Class Program
{
Static void Main (string [] args)
{
YSchool shool1 = new YSchool ();

YSchool school2 = new YSchool (1, @ "");

YSchool school3 = new YSchool (2 );

Console. ReadKey ();
}
}
The get/set attributes of id and name have been modified as only get. Because these attribute values are inherent attributes, it is not logical to assign values after instantiation. That is, these attributes are read-only.

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

[Csharp]
String name = string. Empty;
String name = string. Empty;
This is not to assign values to variables. The default value is only in the form of assigning values to variables or in constructors. Generally, Standard Code requires that the initial value be assigned when the variable is declared. If no value is assigned to a field in life, the compiler adds an assignment code to assign the field to the default value. In fact, whether or not we add a default value to a field, the field has the default value, but if we do not manually add the default value, the default value of the field will be 0 or null.
Note that the system default constructor is mentioned earlier, but the system default constructor does not exist after the constructor is defined. Therefore, if you need to use both the default constructor and the constructor with parameters, you must manually define the default constructor. For example,

[Csharp]
Public YSchool ()
{
 
}
Public YSchool ()
{

}
Or

[Csharp]
Public YSchool ()
{
This. id = 0;
 
This. name = @ "Shang Jin ";
}
Public YSchool ()
{
This. id = 0;

This. name = @ "Shang Jin ";
}
 
In summary, the standard expression of the instantiated class should be: Class Name Instance name = new Class Name (list of constructor parameters );

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.