有什么理由不使用“受保护”?特性?

发布于 2024-10-15 18:14:27 字数 369 浏览 1 评论 0原文

只是想知道......

有什么理由不使用受保护的属性吗?

我的意思是不要使用这个:

public abstract class Foo
{
    protected Bar { get; private set; }
}

使用这个:

public abstract class Foo
{
    private Bar _bar;

    protected Foo(Bar bar)
    {
        _bar = bar;
    }

    protected GetBar()
    {
        return _bar;
    }
}

Just wondering...

Is there any reasons not to use protected properties?

I mean instead of using this:

public abstract class Foo
{
    protected Bar { get; private set; }
}

to use this one:

public abstract class Foo
{
    private Bar _bar;

    protected Foo(Bar bar)
    {
        _bar = bar;
    }

    protected GetBar()
    {
        return _bar;
    }
}

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

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

发布评论

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

评论(4

琉璃梦幻 2024-10-22 18:14:27

我不认为您有任何理由使用 GetXXX() 方法而不是属性,无论它的修饰符如何。

由于您使用 C# 标签标记了这个问题,因此我强烈建议使用第一种方法。这就是属性的用途。

仅当每次调用该方法时返回的值可能不同(无论其状态如何)时,才使用该方法。例如,DateTime.Now 是一个错误,应该是一个方法。

有关更多详细信息,请参阅属性 使用指南在 MSDN 上。顺便说一句,令人惊讶的是,自 Framework 1.1 以来他们就不需要更改它了。

I don't see any reason why you would use a GetXXX() method instead of a property regardless of it's modifiers.

Since you tagged this question with C# tag, I strictly recommend using the first approach. That is what properties are for.

Use a method only when value returned by it can be different every time you call it regardless of it's state. For example, DateTime.Now was a mistake and should have been a method.

For more details refer to Property Usage Guidelines on MSDN. On a side note, it's surprising how they haven't had a need to change it since Framework 1.1.

予囚 2024-10-22 18:14:27

使用属性而不是方法的更多原因:

  • 数据绑定只能查看属性
  • 您可以使用只读或只写语义。

A few more reasons to use properties instead of methods:

  • Data binding can only see properties
  • You can use read only or write only semantics.
原来分手还会想你 2024-10-22 18:14:27

仅当类中有不可写或不可读取的属性时

Only if you have non-writable or non-readable Attributes inside the class

淡淡の花香 2024-10-22 18:14:27

问题实际上并不是关于受保护的,它更多的是:为什么我应该使用属性?

属性在逻辑上与 Getter/Setter 方法对等价,无论您可以使用 Name {get{} \ set{}} 做什么,都可以使用 GetName() \ SetName( ) 配对,反之亦然,特别是 C# 有 getter 和 setter 访问器,因此我们也可以使用可访问性。

但是,有些人认为公共(或受保护)属性从 OOP 的角度来看有点像作弊,特别是如果该属性所做的只是公开一个支持字段。毕竟 person.Name = "SWeko" 看起来像是从外部改变了对象的状态,而 person.SetName("SWeko") 只是告诉对象它需要改变它的状态。从纯粹的理论角度来看,我认为这些反对意见是有道理的。

然而,并不是所有人都能奢侈地生活在象牙塔里,因此属性的概念确实很有用,因为它更具可读性,不易出错,而且恕我直言,更接近现实世界的模型。它还为我们提供了一种二分法,当我写这样的东西时:

person.Name = "SWeko";
person.Work();

我期望第一行将是快速分配,并且不会有副作用,而我期望第二行会做更多的事情,并且可能影响物体的内部状态。当我使用 Getter 和 Setter 方法时,没有立即明显的区别:

person.SetName("SWeko");
person.Work();

The question is not really about protected it has more with: why should I use properties?

Propeties are logically equivalent with Getter/Setter method pairs, whatever you can do with a with a Name {get{} \ set{}} you can do with a GetName() \ SetName() pair and vice versa, especially as C# has getter and setter accessors, so we can play with the accessibility too.

But, there are some folks that feel that public (or protected) properties are a bit like cheating from an OOP perspective, especially if all that the property does is just expose a backing field. After all person.Name = "SWeko" seems like it changes the state of the object externally, while person.SetName("SWeko") merely tells the object that it needs to change it's state. And from a purely theoretic standpoint I think that those objections have merit.

However, not all of us have the luxury of living in ivory towers, so the concept of properties is really usefull, as it's more readable, less error prone, and IMHO, closer to the real world model. It also provides us with a kind of dichotomy, when i write something like this:

person.Name = "SWeko";
person.Work();

I expect that the first line will a fast assignment, and will not have side-effects, while I expect that the second line will do something more, and probably affect the inner state of the object. When I use Getter and Setter method, no such distiction is immediately obvious:

person.SetName("SWeko");
person.Work();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文