✔ 最佳答案
get and set are used to let the property of a class to be read and changed. If you want the property to be readable, use get. If you also want the property to be editable, use set.
Example:
public class Creature
{
private int limbs;
public Creature()
{
// constructor
}
public int Legs // Legs is the public property of Creature
{
get
{
// allow legs to be read
return limbs;
}
set
{
// allow legs to be changed
limbs = value;
}
}
}
// codes in the program
Creature dog = new Creature(); // create a new Creature
dog.Legs = 4; // call set
int x = dog.Legs; // call get
If the property is read-only, do not write the set part in the class.