Components are some functional classes that can be reused.
Property is used to hide fields in the class. generally, fields are set to private, and restricted access fields are used. Meanwhile, additional logic and rules are encapsulated in the attributes to process all possible values.
Attribute is an intersection of fields and methods.CodeThe access restriction function is generally defined as follows:
Accessmodifier type propertyname
{
Get
{
// Get accessor
}
Set
{
// Set accessor
}
}
Read-only attribute: only the get accessor attribute is included;
Write-only attribute: only the set accessor attribute is included; (generally used for password data protection ).
Although the access syntax for fields and attributes is the same, considering compatibility issues, we should try to use attributes to encapsulate fields.
Automatic attributes:
Public int length {Get; set ;}
The compiler automatically converts the code to the following code:
Private int length;
Public int Length
{
Get
{
Return length;
}
Set
{
Length = value;
}
}
Use attribute to initialize an object -- you can assign values to the attribute of Public set while initializing the object:
Triangle tri = New Triangle ("equilateral triangle") {side1length = 5, side2length = 5, side3length = 5 };
It is a bit similar to the with... Do syntax in VB and Delphi, but only when the object is initialized.
Default property value: In the default class constructor, set the default value for the public set attribute.