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

后端 未结 16 1511
自闭症患者
自闭症患者 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条回答
  •  旧时难觅i
    2021-01-01 09:01

    What you actually need are interfaces:

        public interface ProvidesItem1
        {
            int item1 { get; set; }
        }
    
        public interface ProvidesItem2
        {
            int item2 { get; set; }
        }
    
        class a : ProvidesItem1, ProvidesItem2
        {
            public int item1 { get; set; }
            public int item2 { get; set; }
        }
    
        class b : ProvidesItem1
        {
            public int item1 { get; set; }
        }
    

    Then just pass the interfaces around. If the classes should use a common implementation, put that in a third class and let them derive from that class aswell as implement their respective interface.

提交回复
热议问题