为什么我没有使用“受保护”?工作?

发布于 2024-11-01 13:22:45 字数 206 浏览 4 评论 0原文

我读过可以从派生类访问受保护的成员,但以下内容不起作用。

class A
{
    protected int Test;
}
class B:A
{
    A instanceOfA= new A()

    public B()
    {
        instanceOfA.Test //Not possible
    }
}

I have read that a protected member can be accessed from derived classes, but the following does not work.

class A
{
    protected int Test;
}
class B:A
{
    A instanceOfA= new A()

    public B()
    {
        instanceOfA.Test //Not possible
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

隔纱相望 2024-11-08 13:22:45

我读到可以从派生类访问受保护的成员。为什么我使用“受保护”不起作用?

这是非法的,因为您没有保证您正在访问“B”实例的数据。考虑类似的情况:

abstract class BankAccount
{
    protected int accountNumber;
}
class SwissBankAccount : BankAccount
{
}
--- in another assembly, evil-doers write ---
class EvilBankAccount : BankAccount
{
    void DoEvil()
    {
        BankAccount b = GetASwissBankAccount();
        int number = b.accountNumber;
    }
}

EvilBankAccount 不继承自 SwissBankAccount,因此不允许在 EvilBankAccount 内部使用 SwissBankAccount 的受保护成员。您可以访问“父母”的受保护成员,但不能访问“兄弟姐妹”! EvilBankAccount 只能访问 EvilBankAccount 的受保护成员(或从 EvilBankAccount 派生的类型)。 SwissBankAccount 的受保护会员不受限制。

规则是受保护实例成员访问所通过的“接收者”表达式的类型必须至少与包含成员访问的类型声明一样派生。有关规则的精确措辞和一些说明性示例,请参阅 C# 4.0 规范的第 3.5.3 节。

顺便说一句,C++也有这个规则。

这条规则经常被误解。要更深入地分析此规则以及受保护访问的其他一些后果,请参阅我有关该主题的文章。我写过的关于这个主题的最切题的文章是 这个这个。我在此处撰写了更多有关相关主题的文章(尽管其中一些人偏离了受保护访问本身的主题,而转向了如何使用受保护访问来构建具有父引用的数据模型的主题。)

I have read that a protected member can be accessed from derived classes. Why my does my use of “protected” not work?

This is illegal because you have not provided a guarantee that you are accessing data of an instance of "B". Consider this similar case:

abstract class BankAccount
{
    protected int accountNumber;
}
class SwissBankAccount : BankAccount
{
}
--- in another assembly, evil-doers write ---
class EvilBankAccount : BankAccount
{
    void DoEvil()
    {
        BankAccount b = GetASwissBankAccount();
        int number = b.accountNumber;
    }
}

EvilBankAccount does not inherit from SwissBankAccount, so SwissBankAccount's protected member is not allowed to be used inside EvilBankAccount. You're allowed to access the protected members of your "parents", but not your "siblings"! EvilBankAccount can only access protected members of EvilBankAccount (or a type derived from EvilBankAccount). SwissBankAccount's protected members are off-limits.

The rule is that the type of the "receiver" expression that the protected instance member is accessed through has to be at least as derived as the type declaration containing the member access. For the precise wording of the rule and some illustrative examples see section 3.5.3 of the C# 4.0 specification.

Incidentally, C++ also has this rule.

This rule is frequently misunderstood. For a more in-depth analysis of this rule and some of the other consequences of protected access, see my articles on the subject. The most germane articles I've written on this subject are this one and this one. There are a bunch more articles I've written on related topics here (though some of them wander off the topic of protected access itself and onto the topic of how to use protected access to build a data model with parent references.)

一身仙ぐ女味 2024-11-08 13:22:45

您尚未正确设置代码。类 B 不应具有类 A 的实例。相反,类 B 本身继承了类 A 的受保护变量。

您的代码应该看起来更像:

class A
{
   protected int Test;
}
class B:A
{
   public B()
   {
       int someInt = this.Test;
   }
}

You haven't set up your code correctly. Class B shouldn't have an instance of class A. Instead class B itself inherits the protected variables from class A.

Your code should look more like:

class A
{
   protected int Test;
}
class B:A
{
   public B()
   {
       int someInt = this.Test;
   }
}
稍尽春風 2024-11-08 13:22:45

您可以在 B 类本身中访问 Test int。但是,您无法访问实例的属性。 A 不知道它是 B 的子级,因此不会授予对其属性的访问权限。

class A
{
    protected int Test;
}
class B : A
{
    public B()
    {
       Test = 3; //possible
       base.Test = 3;  //explicitly calling base member, but not necessary in this case
    }
}

You can access the Test int within the B class itself. However, you cannot access properties of an instance. A does not know that it is a child of B so won't give access to its property.

class A
{
    protected int Test;
}
class B : A
{
    public B()
    {
       Test = 3; //possible
       base.Test = 3;  //explicitly calling base member, but not necessary in this case
    }
}
太傻旳人生 2024-11-08 13:22:45

由于 B 已经继承自 A,因此您不需要单独的 A 实例。

   public B()
   {
     this.Test = 1; //possible
   }

As B already inherits from A, you don't need a separate instance of A.

   public B()
   {
     this.Test = 1; //possible
   }
带上头具痛哭 2024-11-08 13:22:45

您可以通过同一个类而不是作为基类的公共成员来访问它。

class A 
{ 
    protected int Test; 
} 

class B:A 
{ 
    void TestMethod()
    {
         this.Test = 3; // Possible
    }
}

检查 C# 中的 受保护 访问修饰符。

You can access to it through the same class not as a public member of the base class.

class A 
{ 
    protected int Test; 
} 

class B:A 
{ 
    void TestMethod()
    {
         this.Test = 3; // Possible
    }
}

Check protected access modifier in C#.

°如果伤别离去 2024-11-08 13:22:45

子类可以访问自己标记为受保护的继承成员。

class A
{
   protected int Test;
}

class B : A
{
   public B()
   {
     this.Test = 42; // Possible
   }
}

Subclasses can access their own inherited members that are marked as protected.

class A
{
   protected int Test;
}

class B : A
{
   public B()
   {
     this.Test = 42; // Possible
   }
}
樱花细雨 2024-11-08 13:22:45

如果继承 A,则不需要创建 A 的实例。

class A
{
   protected int Test;
}
class B:A
{
   public B()
   {
     this.Test = 666;
   }
}

You do not need to create an instance of A if you inherit A.

class A
{
   protected int Test;
}
class B:A
{
   public B()
   {
     this.Test = 666;
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文