0
我有這樣的代碼:如何將類實例變量只讀作爲外部類的變量,但其成員是可讀/寫的?
struct Three
{
public int Number;
}
class One
{
public Three three;
public One(){three = new Three();}
}
class Two
{
private One test = new One();
public void SomeFunction()
{
test.three.Number = 5; // This works fine
}
}
但我只需要允許類一款能夠直接寫入三個變量(做的東西像這三=新三()),所以類兩個不能這樣做:test.three = new Three();但它應該能夠做到這一點test.three.Number = 5; (如在該結構的該實例變化值),所以我這樣做:
struct Three
{
public int Number;
}
class One
{
public Three three {private set; get;}
public One(){three = new Three();}
}
class Two
{
private One test = new One();
public void SomeFunction()
{
test.three.Number = 5; // This says i can't modify the return value because it is not a variable
}
}
。如果你把它改成「class」,那麼它就可以工作。 – 2016-07-16 12:58:12