How can I hide a base class public property in the derived class

后端 未结 16 1378
自闭症患者
自闭症患者 2021-01-01 08:39

I want to hide the base public property(a data member) in my derived class:

class Program
{
    static void Main(string[] args)
    {
        b obj = new b()         


        
相关标签:
16条回答
  • 2021-01-01 09:06

    If you wanna hide a member from base class then you will need to add a new base class let's call it baseA and your code should be as follows:

    class Program
    {
        static void Main(string[] args)
        {
            b obj = new b();
            obj.item1 = 4;// should show an error but it doent ???
        }
    }
    
    class baseA
    {
        public int item2 { get; set; }
    }
    class a:baseA
    {
        public int item1 { get; set; }        
    }
    
    class b : baseA { }
    
    class c : a { }
    
    0 讨论(0)
  • 2021-01-01 09:08

    You can user new modifer.

    Sample;

    public class Duck
    {
        public string Color{get; set;}
        public void Swim() { }
    }
    public class DonaldDuck : Duck
    {
        new public void Swim() 
        { 
            /*you could call in DonaldDuck.Swim  only here but not public for DonaldDuck client.*/ 
        }
    }
    
    0 讨论(0)
  • 2021-01-01 09:09

    Vadim's response reminded me of how MS achieve this in the Framework in certain places. The general strategy is to hide the member from Intellisense using the EditorBrowsable attribute. (N.B. This only hides it if it is in another assembly) Whilst it does not stop anyone from using the attribute, and they can see it if they cast to the base type (see my previous explination) it makes it far less discoverable as it doesn't appear in Intellisense and keeps the interface of the class clean.

    It should be used sparingly though, only when other options like restructuring the inheritance hierarchy would make it a lot more complex. It's a last resort rather than the first solution to think of.

    0 讨论(0)
  • 2021-01-01 09:11

    What you want to do goes directly against the grain of OO, you can't 'unpublish' members as this violates the substitution principle. You have to refactor this into something else.

    0 讨论(0)
  • 2021-01-01 09:11

    The only thing I can think of is to make item1 virtual in class a:

    class a
    {
        public virtual int item1 { get; set; }
        public int item2 { get; set; }
    
    }
    

    and then override it in class b but throw an exception in getter and setter. Also if this property is used in a visual designer you can use Browsable attribute to not display.

    class b : a
    {
        [Browsable(false)]
        public override int item1
        {
            get
            {
                throw new NotSupportedException();
            }
            set
            {
                throw new NotSupportedException();
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-01 09:11

    Yes, it is possible. What say you on the delegation. I will try to give an idea of what is called "delegation" in OOP with a piece of code:

    public class ClassA
    {
        // public
        public virtual int MyProperty { get; set; }
    
        // protected
        protected virtual int MyProperty2 { get; set; }
    }
    
    public class ClassB
    {
        protected ClassC MyClassC;
    
        public ClassB()
        {
            MyClassC = new ClassC();
        }
    
        protected int MyProperty2
        {
            get { return MyClassC.MyProperty2; }
            set { MyClassC.MyProperty2 = value; }
        }
    
        protected int MyProperty
        {
            get { return MyClassC.MyProperty; }
            set { MyClassC.MyProperty = value; }
        }
    
        protected class ClassC : ClassA
        {
            public new int MyProperty2
            {
                get { return base.MyProperty2; }
                set { base.MyProperty2 = value; }
            }
    
            public override int MyProperty
            {
                get { return base.MyProperty; }
                set { base.MyProperty = value; }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题