继承Readonly成员 [英] Inherited Readonly Members

查看:75
本文介绍了继承Readonly成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个标记为readonly的抽象类中有一个字段。但是,当我尝试在继承它的类的构造函数中修改该字段时,我得到一个

编译时错误,指出''readonly字段不能分配给

(除了在构造函数或变量初始化程序中)。''任何想法都是

apreciated,虽然我希望我不必使用一个属性。

Reuben。

I have a field in an abstract class marked readonly. However, when I attempt
to modify that field in the constructor of a class which inherits it, I get a
compile-time error stating that ''A readonly field cannot be assigned to
(except in a constructor or a variable initializer).'' Any ideas would be
apreciated, though I was hoping I wouldn''t have to use a property.
Reuben.

推荐答案

它只能在包含类的构造函数或其
变量初始值设定项。私有(非只读)字段,受保护的设置方法

也会这样做,是吗?


问候,


Tim Haughton


" Reuben" <再**** @ discussions.microsoft.com>在消息中写道

新闻:6B ********************************** @ microsof t.com ...
It can only be changed in the containing classes constructor, or in its
variable initializer. Private (non readonly) field, protected set method
would do the same, yes?

Regards,

Tim Haughton

"Reuben" <Re****@discussions.microsoft.com> wrote in message
news:6B**********************************@microsof t.com...
我在一个标记为readonly的抽象类中有一个字段。但是,当我
尝试在继承它的类的构造函数中修改该字段时,我
得到一个编译时错误,指出''readonly字段不能分配给
(除了在构造函数或变量初始化程序中)。''任何想法都会被贬值,虽然我希望我不必使用属性。
Reuben。
I have a field in an abstract class marked readonly. However, when I attempt to modify that field in the constructor of a class which inherits it, I get a compile-time error stating that ''A readonly field cannot be assigned to
(except in a constructor or a variable initializer).'' Any ideas would be
apreciated, though I was hoping I wouldn''t have to use a property.
Reuben.


" Tim Haughton" < TI ********* @ gmail.com>在新闻中写道:tmNye.72339
"Tim Haughton" <ti*********@gmail.com> wrote in news:tmNye.72339


H46.63876

@ fe03.news.easynews.com:
H46.63876
@fe03.news.easynews.com:
它只能是在包含类的构造函数或其变量初始值设定项中更改。私有(非只读)字段,受保护的集合方法
也会这样做,是吗?
It can only be changed in the containing classes constructor, or in its
variable initializer. Private (non readonly) field, protected set method
would do the same, yes?




您不需要set方法,只需访问该字段直接,如果你宣布它受到保护。

-

Chad Z. Hower(又名Kudzu) - http://www.hower.org/Kudzu/

编程是一种反击的艺术形式 />

使用IntraWeb授权ASP.NET
http ://www.atozed.com/IntraWeb/


这篇关于继承Readonly成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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