2009-08-01 47 views
1

有沒有辦法限制評論中概述的這些類的成員的訪問權限;是否有一個訪問修飾符,它允許繼承類修改c#中父類的成員?

class a 
{ 
    int p //should be accessable by b,c, but not by x 
} 

class b:a 
{ 
    int q //should be accessable by c, if it has to by a, but not by x 
} 

class c:b 
{ 
    public int r //obviously accessable by anyone 
} 

class x 
{ 
    c testfunction() 
    { 
     c foo=new c(); 
     foo.r=20; 
     return foo; 
    } 
} 

這是比這裏的示例代碼更復雜一點,但我認爲我得到了我的問題。

回答

11

是 - 那將是protected訪問修飾符 - 允許後代訪問它,但不允許「外部」用戶訪問。

class a 
{ 
    protected int p //should be accessable by b,c, but not by x 
} 

class b:a 
{ 
    protected int q //should be accessable by c, if it has to by a, but not by x 
} 

class c:b 
{ 
    public int r //obviously accessable by anyone 
} 

馬克

相關問題