代码之家  ›  专栏  ›  技术社区  ›  niklasfi Rahul Tripathi

是否有允许继承类修改c#中父类成员的访问修饰符?

c#
  •  1
  • niklasfi Rahul Tripathi  · 技术社区  · 17 年前

    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;
        }
    }
    

    这比这里的示例代码要复杂一点,但我想我已经把我的问题讲清楚了。

    1 回复  |  直到 17 年前
        1
  •  11
  •   marc_s MisterSmith    17 年前

    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
    }
    

    马克