访问修饰符 - 我试图在类级别上实现私有AccessModifiers。虽然我已经在班级使用私人访问修饰符,但我能够访问受保护的成员... [英] Access Modifiers -Here i have tried to impliment private AccessModifiers at class level. Eventhough i have used private access modifier at class level, i'm able to access protected member...

查看:84
本文介绍了访问修饰符 - 我试图在类级别上实现私有AccessModifiers。虽然我已经在班级使用私人访问修饰符,但我能够访问受保护的成员...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些访问修饰符仅适用于类的成员函数和数据成员,或者我们也可以在c#中将它们应用于类级别?



如果是,则

-这是我试图在类级别上实现私有AccessModifiers。尽管我在类级别使用了私有访问修饰符,但我能够访问子类中父类的受保护成员函数。但私有意味着我们不应该能够访问该类吗?(如果我错了请纠正我)。





ParentA .cs

-----------------

These access modifiers are only for member functions and data member of the class or we can apply them at class level also in c# ?

If yes then,
-Here i have tried to impliment private AccessModifiers at class level. Eventhough i have used private access modifier at class level, i''m able to access protected member functions of the parent class in child class. But private means we shouldnot be able to access that class right?(please correct me if i''m wrong).


ParentA.cs
-----------------

namespace PrivateAccessModifier
{
    private class ParentA
    {
        private int a;
        protected void methoda()
        {
            Console.WriteLine("in Parent");
        }
    }
}





ChildA.cs

- ---------------



ChildA.cs
----------------

namespace PrivateAccessModifier
{
    private class childA : ParentA
    {
        public void method1()
        {
            methoda();
        }
    }
}

推荐答案

所以首先,如果ur命名空间是相同的不需要将类声明为Private,Protected或Protected internal。因为非嵌套类型的默认值是内部的。嵌套类型的默认值是私有的。在这两种情况下,默认(对于类)是未密封的。

其次是,如果你声明父类成员是受保护的,你将始终在子类中访问这些成员。

参考 - 访问修饰符(C#编程指南) [ ^ ]
so first things is that, if ur namespace is same need not declare the class as Private, Protected or Protected internal. because The default for non-nested types is internal. The default for nested types is private. In both cases the default (for classes) is unsealed.
Second is that, if u declare parent class member is as a Protected u will always access these member in child class.
Refer - Access Modifiers (C# Programming Guide)[^]


只需要一件事请注意这两个类,即ParentA和childA是同一名称空间的一部分,因此它们可供彼此引用。您可以查看名称空间内的私有类[已关闭] [ ^ ]



问候

Pawan
just one thing to note here both the classes i.e, ParentA and childA are part of the same namespace so they will be available to be referenced by one another. you may check private classes inside namespaces [closed][^]

Regards
Pawan


这篇关于访问修饰符 - 我试图在类级别上实现私有AccessModifiers。虽然我已经在班级使用私人访问修饰符,但我能够访问受保护的成员...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆