具有不同访问者的属性? [英] Properties with different accessors?

查看:66
本文介绍了具有不同访问者的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以创建一个具有与get属性不同的可访问性的set属性




例如,我有一个私有字段一个基类。 Get属性应该是公开的,因此任何人都可以看到该字段的值。但是,设置

属性应该受到保护且不可覆盖(因为它会根据新值进行各种额外的计算/操作)并且只能派生

类应该能够设置一个新值。


希望有意义,


提前感谢您的帮助,


Ann-Marie Ratcliffe

Is it possible to create a set property which has a different accessibility
than the get property?

For example, I have a private field in a base class. The Get property should
be public so that anyone can see the value of the field. However the Set
property should be protected and non-overridable (since it does various
additional calculations/actions based on the new value) and only derived
classes should be able to Set a new value.

Hope that makes sense,

Thanks in advance for any help,

Ann-Marie Ratcliffe

推荐答案

是否可以创建一个设置属性具有与获取属性不同的可访问性?
Is it possible to create a set property which has a different accessibility
than the get property?




编号但它是未来版本的常用功能。


Mattias


-

Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/

请回复新闻组。



No. But it''s a commonly requested feature for future versions.

Mattias

--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.


数字;(


我猜最好解决方法是将属性作为

hoi-polloi的只读属性,然后对派生类有一个受保护的方法,但是

似乎都是这可能是一件非常简单的事情。


感谢您的快速回复,


你的,

Ann-Marie Ratcliffe


PS如果有一种比我上面提到的更简洁的方式,我很乐意听到它。



" Mattias Sj?gren" <毫安******************** @ mvps.org>在消息中写道

新闻:OQ ************* @ TK2MSFTNGP11.phx.gbl ...
Figures ;(

I guess the best work-around is to have the Property as read-only for the
hoi-polloi, and then have a protected Method for the derived classes, but it
all seems like a two''n''eight for what could be so simple.

Thanks for your quick reply,

Yours,

Ann-Marie Ratcliffe

P.S. If there is a neater way than I''ve suggested above I''d love to hear it.


"Mattias Sj?gren" <ma********************@mvps.org> wrote in message
news:OQ*************@TK2MSFTNGP11.phx.gbl...
是否可以创建一个具有与get属性不同的
可访问性的set属性?
Is it possible to create a set property which has a different accessibilitythan the get property?



否。但它是未来版本的常用功能。

Mattias

- Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
请仅回复新闻组。



No. But it''s a commonly requested feature for future versions.

Mattias

--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.



A Ratcliffe< ar ******** @ archiNOSPAMmagic.net>写道:
A Ratcliffe <ar********@archiNOSPAMmagic.net> wrote:
我想最好的解决方法是将属性作为
hoi-polloi的只读,然后对派生类有一个受保护的方法,但它<对于可能如此简单的事情,所有这些似乎都是两个'''''。
I guess the best work-around is to have the Property as read-only for the
hoi-polloi, and then have a protected Method for the derived classes, but it
all seems like a two''n''eight for what could be so simple.




是的,这很痛苦 - 坦白说,我我很惊讶它是在v1.0之前没有得到的b $ b固定的。这似乎是一件显而易见的事情......

也许有些问题我不知道。


一个只读属性和一个mutator方法,反之亦然

(私有财产,公共访问者),或者只是有两种方法

目前确实是选项。


幸运的是,不仅要求下一个版本,而且

显然也会发生这种情况:)


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该群组,请不要给我发邮件



Yes, it''s a pain - and frankly, I''m surprised it''s one which didn''t get
fixed before v1.0. It seems such an obvious thing to want to do...
maybe there are problems with it that I''m not aware of.

Having either a readonly property and a mutator method, or vice versa
(private property, public accessor), or just having two methods are
indeed the options at the moment.

Fortunately, not only has this been requested for the next version, but
apparently it''s going to happen, too :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于具有不同访问者的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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