2010-04-02 79 views
1

如果我有一個包含具有私有集和受保護集可訪問性屬性集的屬性的類,我是否可以在同一個類的另一個實例上更改這些屬性?從同一類的另一個實例中設置受保護/私有屬性

注意:我不在一臺機器上,我現在可以測試它,否則我只是運行下面的代碼。

例如:

public class Foo 
{ 
    public string A {get; private set;} 
    public string B {get; protected set;} 

    public void Bar() 
    { 
     var someOtherFoo = new Foo(); 

     // Does this change someOtherFoo's A? 
     someOtherFoo.A = "A"; 

     // Does this change someOtherFoo's B? 
     someOtherFoo.B = "B"; 
    } 
} 

回答

4

是。訪問是類型,而不是實例。這對於執行平等等事情特別有用,因爲您可以測試this.x == other.x && this.y == other.y;。訪問也可用於嵌套類型。

1

簡短的回答:是的

0

//這是否會改變someOtherFoo的A? //這是否改變其他一些其他的B?

是的,是的。

相關問題