在派生类的构造函数中设置只读。 [英] Set readonly in constructor of a derived class.

查看:90
本文介绍了在派生类的构造函数中设置只读。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道不允许以下内容,但不应该这样吗? sharedObject是衍生的
的一部分,应该能够在构造函数中设置 - 不是吗? tia


公共抽象类Base1

{

protected readonly object sharedObject;


}

公共类派生:Base1

{


public Derived()

{

sharedObject = new object();

}


}


-

William Stacey,MVP

I know the following is not allowed, but shouldn''t it be? sharedObject is
part of Derived and should be able to be set in the constructor - no? tia

public abstract class Base1
{
protected readonly object sharedObject;

}
public class Derived : Base1
{

public Derived()
{
sharedObject = new object();
}

}

--
William Stacey, MVP

推荐答案

Hi William,


readonly关键字是一个可以在字段上使用的修饰符。当字段

声明包含只读修饰符时,声明引入的字段

的赋值只能作为声明的一部分出现或

在同一个类的构造函数中。

^^^^^^^^^^^^^^^^^^^^^^^^^^^^

这是详细信息的链接。
http://msdn.microsoft.com/library/de...us/csref/html/

vclrfreadonlypg.asp

问候,

Peter Huang

Microsoft在线合作伙伴支持

安全! www.microsoft.com/security

此帖子是提供按原样没有保证,也没有赋予任何权利。


--------------------
Hi William,

The readonly keyword is a modifier that you can use on fields. When a field
declaration includes a readonly modifier, assignments to the fields
introduced by the declaration can only occur as part of the declaration or
in a constructor in the same class.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Here is the link for detail information.
http://msdn.microsoft.com/library/de...us/csref/html/
vclrfreadonlypg.asp

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
来自: 威廉斯泰西 < st ***** @ mvps.org>
主题:在派生类的构造函数中设置只读。
日期:2003年10月7日星期二00:10:08 -0400
行数:23
X-Priority:3
X-MSMail-Priority:正常
X-Newsreader:Microsoft Outlook Express 6.00.3790.0
X-MimeOLE:由Microsoft MimeOLE V6制作.00.3790.0
消息ID:<或************** @ TK2MSFTNGP09.phx.gbl>
新闻组:microsoft.public.dotnet.languages。 csharp
NNTP-Posting-Host:66.188.59.114.bay.mi.chartermi.net 66.188.59.114
路径:
cpmsftngxa06.phx.gbl!TK2MSFTNGXA06.phx.gbl!TK2MSFT NGXA05。 phx.gbl!TK2MSFTNGP0

8.phx.gbl!TK2MSFTNGP09.phx.gblXref:cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:189441
X-Tomcat-NG :microsoft.public.dotnet.languages.csharp

我知道不允许以下内容,但不应该这样吗? sharedObject是Derived的一部分,应该能够在构造函数中设置 - 不是吗? tia

公共抽象类Base1
{
受保护的readonly对象sharedObject;

}
public class Derived:Base1
{

公共派生()
{
sharedObject = new object();
}

}

-
William Stacey,MVP
From: "William Stacey" <st*****@mvps.org>
Subject: Set readonly in constructor of a derived class.
Date: Tue, 7 Oct 2003 00:10:08 -0400
Lines: 23
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.3790.0
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
Message-ID: <Or**************@TK2MSFTNGP09.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: 66.188.59.114.bay.mi.chartermi.net 66.188.59.114
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGXA06.phx.gbl!TK2MSFT NGXA05.phx.gbl!TK2MSFTNGP0
8.phx.gbl!TK2MSFTNGP09.phx.gblXref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:189441
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

I know the following is not allowed, but shouldn''t it be? sharedObject is
part of Derived and should be able to be set in the constructor - no? tia

public abstract class Base1
{
protected readonly object sharedObject;

}
public class Derived : Base1
{

public Derived()
{
sharedObject = new object();
}

}

--
William Stacey, MVP






William Stacey< st ***** @ mvps。有机>写道:
William Stacey <st*****@mvps.org> wrote:
我知道以下是不允许的,但不应该吗? sharedObject是Derived的一部分,应该能够在构造函数中设置 - 不是吗?
I know the following is not allowed, but shouldn''t it be? sharedObject is
part of Derived and should be able to be set in the constructor - no?




< snip>


除了Peter给出的答案之外,我还建议将任何

字段非私有至少*通常是一个坏主意。对于一次性课程来说,这不是很糟糕

,或者可能是嵌套课程只能看到封闭课程的嵌套课程,但是我会避免使用它们其他。


-

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

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



<snip>

As well as the answer given by Peter, I''d also suggest that making any
fields non-private is at least *usually* a bad idea. It''s not so bad
for throwaway classes, or perhaps nested classes that are only visible
to the enclosing class, but I''d steer away from using them elsewhere.

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


谢谢彼得。我理解readonly的正常用法。不过我要问b $ b问一个不同的问题。由于Derived继承了sharedObject,而Base1

是抽象的,因此不是sharedObject。 Derived类的成员?如果是这样的话,

为什么Derived中的*构造函数不能在其构造函数中设置值?


-

William Stacey,MVP

" Peter Huang [MSFT]" <,V - ****** @ online.microsoft.com>在消息中写道

news:bt ************** @ cpmsftngxa06.phx.gbl ...
Thanks Peter. I understand the normal usage of readonly. However I am
asking a different question. As Derived inherits "sharedObject", and Base1
is abstract, isn''t "sharedObject" a member of the Derived class? And if so,
why can''t the *constructor in Derived set the value in its'' constructor?

--
William Stacey, MVP

"Peter Huang [MSFT]" <v-******@online.microsoft.com> wrote in message
news:bt**************@cpmsftngxa06.phx.gbl...
Hi William,

readonly关键字是一个可以在字段上使用的修饰符。当
字段声明包含一个只读修饰符时,声明引入的字段的赋值只能作为声明的一部分出现,或者在同一类的构造函数中出现。
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
这是详细信息的链接。
http://msdn.microsoft.com/ library / de ... us / csref / html / vclrfreadonlypg.asp

问候,
Peter Huang
微软在线合作伙伴支持
获得安全保障! www.microsoft.com/security
此帖子提供为as as是"没有保证,也没有赋予任何权利。

--------------------
Hi William,

The readonly keyword is a modifier that you can use on fields. When a field declaration includes a readonly modifier, assignments to the fields
introduced by the declaration can only occur as part of the declaration or
in a constructor in the same class.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Here is the link for detail information.
http://msdn.microsoft.com/library/de...us/csref/html/ vclrfreadonlypg.asp

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
来自:William Stacey ; < st ***** @ mvps.org>
主题:在派生类的构造函数中设置只读。
日期:2003年10月7日星期二00:10:08 -0400
行数:23
X-Priority:3
X-MSMail-Priority:正常
X-Newsreader:Microsoft Outlook Express 6.00.3790.0
X-MimeOLE:由Microsoft MimeOLE V6制作.00.3790.0
消息ID:<或************** @ TK2MSFTNGP09.phx.gbl>
新闻组:microsoft.public.dotnet.languages。 csharp
NNTP-Posting-Host:66.188.59.114.bay.mi.chartermi.net 66.188.59.114
路径:
From: "William Stacey" <st*****@mvps.org>
Subject: Set readonly in constructor of a derived class.
Date: Tue, 7 Oct 2003 00:10:08 -0400
Lines: 23
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.3790.0
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
Message-ID: <Or**************@TK2MSFTNGP09.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: 66.188.59.114.bay.mi.chartermi.net 66.188.59.114
Path:



cpmsftngxa06.phx.gbl!TK2MSFTNGXA06 .phx.gbl!TK2MSFT NGXA05.phx.gbl!TK2MSFTNGP0 8.phx.gbl!TK2MSFTNGP09.phx.gbl


cpmsftngxa06.phx.gbl!TK2MSFTNGXA06.phx.gbl!TK2MSFT NGXA05.phx.gbl!TK2MSFTNGP0 8.phx.gbl!TK2MSFTNGP09.phx.gbl

Xref:cpmsftngxa06.phx.gbl
microsoft.public.dotnet.languages .csharp:189441X-Tomcat-NG:microsoft.public.dotnet.languages.csharp

我知道不允许以下内容,但不应该这样吗? sharedObject
是Derived的部分,应该能够在构造函数中设置 - 不是吗?
tia
公共抽象类Base1
{
受保护的readonly对象sharedObject;

}
公共类派生:Base1
{

公共派生()
{
sharedObject = new object();
}

}

-
William Stacey,MVP
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:189441X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

I know the following is not allowed, but shouldn''t it be? sharedObject ispart of Derived and should be able to be set in the constructor - no? tia
public abstract class Base1
{
protected readonly object sharedObject;

}
public class Derived : Base1
{

public Derived()
{
sharedObject = new object();
}

}

--
William Stacey, MVP



这篇关于在派生类的构造函数中设置只读。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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