简体   繁体   English

C# 中的受保护方法 inheritance

[英]Protected method inheritance in C#

I have a simple C# code written after reading documentation about protected access modifier, but I am recieving a lot of illogical errors.在阅读有关受保护的访问修饰符的文档后,我编写了一个简单的 C# 代码,但我收到了很多不合逻辑的错误。 I cant find any solutions to them.我找不到任何解决方案。

using System;

public class WeaponController
{
    protected void Reload()
    {
        Console.WriteLine("Reload");
    }

    protected virtual void Shoot()
    {

    }
}

public class SMGController : WeaponController
{

    private override void Shoot()
    {
        Console.WriteLine("Shoot");
    }
}

public class Test
{
    public static void Main()
    {
        var test = new SMGController();
        test.Shoot();
        test.Reload();
    }
}

And I am recieving following errors:我收到以下错误:

prog.cs(19,27): error CS0621: SMGController.Shoot()': virtual or abstract members cannot be private prog.cs(19,27): error CS0507: SMGController.Shoot()': cannot change access modifiers when overriding protected' inherited member WeaponController.Shoot()' prog.cs(10,25): (Location of the symbol related to previous error) Compilation failed: 2 error(s), 0 warnings prog.cs(19,27): error CS0621: SMGController.Shoot()': virtual or abstract members cannot be private prog.cs(19,27): error CS0507: SMGController.Shoot()': cannot change access modifiers when覆盖protected' inherited member WeaponController.Shoot()' prog.cs(10,25):(与先前错误相关的符号位置)编译失败:2 个错误,0 个警告

If I will change access modifier of Shoot() method in SMGController to protected I recieve even more errors:如果我将 SMGController 中的 Shoot() 方法的访问修饰符更改为 protected,我会收到更多错误:

prog.cs(30,8): error CS1540: Cannot access protected member WeaponController.Shoot()' via a qualifier of type SMGController'. prog.cs(30,8):错误 CS1540:无法WeaponController.Shoot()' via a qualifier of type '。 The qualifier must be of type Test' or derived from it prog.cs(10,25): (Location of the symbol related to previous error) prog.cs(30,8): error CS0122: WeaponController.Shoot()' is inaccessible due to its protection level prog.cs(10,25): (Location of the symbol related to previous error) prog.cs(31,8): error CS1540: Cannot access protected member WeaponController.Reload()' via a qualifier of type SMGController'.限定符必须是Test' or derived from it prog.cs(10,25): (Location of the symbol related to previous error) prog.cs(30,8): error CS0122: WeaponController.Shoot()' 是由于其保护级别 prog.cs(10,25) 无法访问:(与先前错误相关的符号位置) prog.cs(31,8):错误 CS1540:无法WeaponController.Reload()' via a qualifier of type SMGController'。 The qualifier must be of type Test' or derived from it prog.cs(5,17): (Location of the symbol related to previous error) prog.cs(31,8): error CS0122: WeaponController.Reload()' is inaccessible due to its protection level prog.cs(5,17): (Location of the symbol related to previous error) Compilation failed: 4 error(s), 0 warnings限定符必须是Test' or derived from it prog.cs(5,17): (Location of the symbol related to previous error) prog.cs(31,8): error CS0122: WeaponController.Reload()' 是由于其保护级别 prog.cs(5,17) 无法访问:(与先前错误相关的符号位置)编译失败:4 个错误,0 个警告

What is wrong with my code?我的代码有什么问题?

You can't call protected methods from outside the class, ie call WeaponController.Shoot from Test .您不能从 class 外部调用protected的方法,即从Test调用WeaponController.Shoot

Also, if you override a protected method, it has to be protected , too.此外,如果您覆盖protected的方法,它也必须是protected的。

Obviously, you want Reload and Shoot to be public , so you can call them from Test.Main .显然,您希望Reload and Shootpublic ,因此您可以从Test.Main调用它们。

The protected modifier means that only the class itself or a subclass can access Shoot . protected修饰符意味着只有class 本身或子类可以访问Shoot

You are trying to access it from some other class Test that has no inheritance relation with both SMGController and WeaponController , and such access is forbidden by the protected modifier.您正在尝试从与SMGControllerWeaponController没有 inheritance 关系的其他 class Test访问它,并且protected的修饰符禁止此类访问。

For reference, see https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/protected , especially this line in the first section:有关参考,请参阅https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/protected ,尤其是第一节中的这一行:

A protected member is accessible within its class and by derived class instances.受保护的成员可在其 class 内和派生的 class 实例中访问。

You can fix this by making Shoot public .您可以通过将Shoot public来解决此问题。 Or if you insist on keeping it protected , then you would need to add a method eg public PublicShoot() that then calls the protected method.或者,如果您坚持保持它protected ,那么您将需要添加一个方法,例如public PublicShoot() ,然后调用受保护的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM