为什么Resharper宁愿不隐藏另一个抽象类中的抽象类的属性? [英] Why does resharper prefer to not hide a property from an abstract class inside another abstract class?

查看:105
本文介绍了为什么Resharper宁愿不隐藏另一个抽象类中的抽象类的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么重新整理者要我不要对抽象类隐藏属性? 它希望我使用"new",但这始终是可取的吗? 似乎暗示着隐藏一个变量,使用户无法使用base.property是一件坏事.

Why does re-sharper want me to not hide a property from an abstract class? It wants me to use 'new', but is that always preferable? It seems to imply that hiding a variable so the user can't do base.property is a bad thing.

我对这种面向对象的概念有些困惑,并且想知道是否有很强的理由支持或反对它.

I'm just a little confused about this OO concept and was wondering if there were strong reasons for or against it.

我有

public abstract class baseClass{
 protected string commonProperty {get; set;}
}

public abstract class moreSpecificBaseClass : baseClass {
   // Resharper would prefer I use 'new' below
   protected string commonProperty = "Some specific setting"; 
}

public class verySpecificClass : moreSpecificBaseClass {
// Some code
}

推荐答案

来自

尽管您可以在不使用新修饰符的情况下隐藏成员,但是 结果是警告.如果使用new显式隐藏成员,则它 禁止显示此警告,并记录以下事实: 版本将被替换.

Although you can hide members without the use of the new modifier, the result is a warning. If you use new to explicitly hide a member, it suppresses this warning and documents the fact that the derived version is intended as a replacement.

隐藏班级成员可能导致误解和细微的错误.因此,编译器会警告您,并要求您使用"new"修饰符来明确表示可能有害的成员隐藏的意图.这样可以防止意外发现基本类型成员的意外隐藏.

Hiding class members can lead to misunderstandings and subtle errors. Hence the compiler warns you and requires you to make your intention of a possibly harmful member hiding explicit by using the "new" modifier. This prevents an accidental hiding of a base type member from being unnoticed.

看到这个隐藏成员时问题如何潜入您的代码中的小例子:

See this little example of how problems can sneak into your code when hiding members:

    public abstract class A
    {
        public int Value = 0;
    }

    public class B : A
    {
        // This hides the Value member from the base type A
        public new int Value = 0;
    }

    static void SetValue(B obj, int value)
    {
        obj.Value = value;
    }

    static void AddToValue(A obj, int value)
    {
        obj.Value += value;
    }

    static void Main(string[] args)
    {
        B obj = new B();

        SetValue(obj, 11);
        AddToValue(obj, 5);

        Console.Out.WriteLine("obj.Value = " + obj.Value);
        Console.Out.WriteLine("((A) obj).Value = " + ((A) obj).Value);
        Console.Out.WriteLine("((B) obj).Value = " + ((B) obj).Value);
    }

这将输出:

obj.Value = 11
((A) obj).Value = 5
((B) obj).Value = 11

仅通过查看 Main 方法的流程,您可能会认为在该方法的末尾obj.Value的值为16(= 11 + 5).但这不是-甚至更糟:根据您访问 Value 成员的方式,您会遇到不同的值.

Just by looking at the flow of the Main method, you might think that at the end of the method the value of obj.Value is 16 (=11+5). But it isn't - it is even worse: depending on how you access the Value member you will encounter different values.

现在,在这个小例子中,在所有情况下输出都不相同且期望值不同的原因可能很容易并且很容易找到.但是,想象一下大型软件项目,其中包含许多类,大量继承,方法参数是抽象基类或接口类型,第三方类库,您可以将它命名为...,隐藏成员引起的问题可能会变得更加难以解决.识别.

Now, in this small example, the reason(s) of why the output is not the same and not the expected value in all cases might be easy and quickly to spot. But imagine larger software projects with many classes, lots of inheritances, method arguments being of abstract base class or interface types, 3rd-party class library, you name it..., and the problems caused by hidden members might become much more difficult to identify.

这篇关于为什么Resharper宁愿不隐藏另一个抽象类中的抽象类的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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