Copy codeThe Code is as follows: public class Product
{
Private String name;
Public String Name
{
Get
{
Return name;
}
Private set
{
Name = value;
}
}
Private Decimal price;
Public Decimal Price
{
Get
{
Return price;
}
Set
{
Price = value;
}
}
Public Product (String name, Decimal price)
{
This. price = price;
This. name = name;
}
}
It can be rewritten:
Copy codeThe Code is as follows: public class Product
{
Public String Name
{
Get;
Private set;
}
Public Decimal Price
{
Get;
Set;
}
Public Product (String name, Decimal price)
{
Name = name;
Price = price;
}
Public override string ToString ()
{
Return String. Format ("{0 }:{ 1}", this. Name, this. Price );
}
}
Is the code much simpler!
Note:
Read-only or write-only attributes cannot be defined.
If you want to add judgment, verification, and other logic to an attribute, you can only use the traditional attribute definition method.