2017-06-20 6 views
-3

派生クラスの値を継承または変更しないようにクラス変数を制限する方法を教えてください。Cで派生クラスで編集しないフィールドを制限する方法

EX-

Class A 
{ 
    public string name="ABC"; 
} 
Class B:A 
{ 
    //I don't want here name property of base class should be accessible 
    or can be changed 
} 
+2

プライベート –

+0

こんにちは@BhubanShresthaに公開フィールドを作るが、プライベートメンバーがアクセス可能である真ではありません派生クラスで – user2147163

+0

[継承したクラス(継承クラス)を継承しないで継承したプロパティを非表示にする方法](https://stackoverflow.com/questions/1875401/how-to-hide-an-inherited -property-in-a-class-without-modifying-the-inherited-c la) –

答えて

3

次のように、民間セットでプロパティを使用することができます

class A 
{ 
    public string Name 
    { 
     get; 
     private set; 
    } 
} 

class B:A 
{ 
    public B() 
    { 
     this.Name = "Derived"; // This will not work. If you want to prevent the derived class from accessing the field, just mark the field as private instead of public. 
    } 
} 
関連する問題