??结构上的“新”和“受保护”修饰符?? [英] ?? 'new' and 'protected' Modifiers on Structs ??

查看:65
本文介绍了??结构上的“新”和“受保护”修饰符??的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


有没有人看过C#规范的第18.1.1节?它表示''new''和

''protected''是结构声明的有效修饰符。首先,

''protected''如何在结构上有效,因为结构不能被继承?

编译器会出现错误(正如我预期的那样),如果您尝试这样做:


protected struct MyStruct {}


所以当我说''protected''是一个有效的

结构修饰符时,我想知道规格是否错误。


关于第18.1.1节的第二点是它表示''new''是结构上的一个允许修饰符。实际上,18.1.1表示,结构声明的修饰符

与类

声明的含义相同,请读者参阅第17.1节。 1上课声明

修饰符。


我查看了第17.1.1节,果然,它说:


"嵌套类允许使用new修饰符。

它指定该类以相同的名称隐藏继承的成员

,如第17.2.2节所述。它是一个编译时错误,新修饰符在一个不是嵌套类声明的类声明中出现




我的测试表明在类或结构声明中不允许''new''

(嵌套与否)。根据规范(第17.1.1节),这似乎是''新'应该如何用作类

修饰符:


class MyClass {

public virtual int M(){return 1; }

}


class Outer {

new class Inner:MyClass {// ERROR on''new''修饰符

public new int M(){return 2; }

}

}


上面的代码给出了内部声明的警告。

所以,我离开基地了吗? 18.1.1

和17.1.1是否有问题?


谢谢

-

Tom Baxter

解决方案

Tom Baxter写道:


大家好,


有没有人看过C#规范的第18.1.1节?它表示''new''

和''protected''是结构声明的有效修饰符。首先,

如何保护在结构上有效,因为结构不能被继承?

编译器发出错误(正如我预期的那样)你试试这个:


protected struct MyStruct {}


所以当我说保护时,我想知道规范是否错误''是一个

有效的struct-modifier。


第18.1.1节的第二点是它表示''new''是一个允许修饰符。实际上,18.1.1表示,结构声明的

修饰符与

类声明的含义相同,请读者参阅第17.1节。 1上课

声明修饰符。


我查看了第17.1.1节,果然,它说:


"嵌套类允许使用new修饰符。

它指定该类以相同的名称隐藏继承的成员

,如第17.2.2节所述。它是一个编译时错误,新修饰符在一个不是嵌套类声明的类声明中出现




我的测试表明,'new''不允许在类或结构上

声明(嵌套与否)。根据规范(第17.1.1节),这似乎应该是''新'应该如何使用

作为类修饰符:


class MyClass {

public virtual int M(){return 1; }

}


class Outer {

new class Inner:MyClass {// ERROR on''new''修饰符

public new int M(){return 2; }

}

}


上面的代码给出了内部声明的警告。


我想如果你改变你的定义来实际隐藏一个成员(M)你将会消除那个错误。

class MyClass

{

public virtual int M(){return 1; }

}


class Outer:MyClass

{

公共新类M:MyClass < br $>
{

}

}


class C2:MyClass

{

公共新结构M

{

}

}


并在受保护上侧面 - 它再次涉及子类的内容

包含类 - 即


公共类BaseClass {

void TestBase() {

NestedStruct ns;

}

private struct NestedStruct {int a;}

}

public class SubClass:BaseClass {

void TestSub(){

NestedStruct ns; // **错误

}

}


以上是非法的,因为SubClass看不到BaseClass.NestedStruct

(因为它是私人的);但是,更改NestedStruct是受保护的,

和任何BaseClass子类现在都能看到它。


Marc


谢谢,Marc !!我很抱歉!

" Marc Gravell" < ma ********** @ gmail.comwrote in message

news:11 ******************** **@50g2000hsm.googlegro ups.com ...


并在受保护上侧面 - 它再次涉及子类的内容

包含类 - 即


公共类BaseClass {

void TestBase() {

NestedStruct ns;

}

private struct NestedStruct {int a;}

}

public class SubClass:BaseClass {

void TestSub(){

NestedStruct ns; // **错误

}

}


以上是非法的,因为SubClass看不到BaseClass.NestedStruct

(因为它是私人的);但是,更改NestedStruct是受保护的,

和任何BaseClass子类现在可以看到它。


Marc



-

Tom Baxter


Hi everyone,

Has anyone looked at section 18.1.1 of the C# spec? It indicates ''new'' and
''protected'' are valid modifiers on struct declarations. First, how can
''protected'' be valid on a struct, since structs cannot be inherited? The
compiler gives an error (as I expect it should) if you try this:

protected struct MyStruct {}

so I''m wondering if the spec is wrong when it says ''protected'' is a valid
struct-modifier.

The second point about section 18.1.1 is that it indicates ''new'' is an
allowable modifier on a struct. Actually, 18.1.1 indicates, "The modifiers
of a struct declaration have the same meaning as those of a class
declaration", referring the reader to section 17.1.1 on class declaration
modifiers.

I looked at section 17.1.1 and sure enough, it says:

"The new modifier is permitted on nested classes.
It specifies that the class hides an inherited member
by the same name, as described in section 17.2.2. It
is a compile-time error for the new modifier to appear
on a class declaration that is not a nested class declaration."

My testing indicates ''new'' is not allowed on class nor struct declarations
(nested or not). It seems this is how ''new'' should be used as a class
modifier, according to the spec (sec. 17.1.1):

class MyClass {
public virtual int M() { return 1; }
}

class Outer {
new class Inner : MyClass { // ERROR on ''new'' modifier
public new int M() { return 2; }
}
}

The above code gives a warning on the declaration of Inner.

So, am I way off base here? Is there something wrong with sections 18.1.1
and 17.1.1?

Thanks
--
Tom Baxter

解决方案

Tom Baxter wrote:

Hi everyone,

Has anyone looked at section 18.1.1 of the C# spec? It indicates ''new''
and ''protected'' are valid modifiers on struct declarations. First, how
can ''protected'' be valid on a struct, since structs cannot be inherited?
The compiler gives an error (as I expect it should) if you try this:

protected struct MyStruct {}

so I''m wondering if the spec is wrong when it says ''protected'' is a
valid struct-modifier.

The second point about section 18.1.1 is that it indicates ''new'' is an
allowable modifier on a struct. Actually, 18.1.1 indicates, "The
modifiers of a struct declaration have the same meaning as those of a
class declaration", referring the reader to section 17.1.1 on class
declaration modifiers.

I looked at section 17.1.1 and sure enough, it says:

"The new modifier is permitted on nested classes.
It specifies that the class hides an inherited member
by the same name, as described in section 17.2.2. It
is a compile-time error for the new modifier to appear
on a class declaration that is not a nested class declaration."

My testing indicates ''new'' is not allowed on class nor struct
declarations (nested or not). It seems this is how ''new'' should be used
as a class modifier, according to the spec (sec. 17.1.1):

class MyClass {
public virtual int M() { return 1; }
}

class Outer {
new class Inner : MyClass { // ERROR on ''new'' modifier
public new int M() { return 2; }
}
}

The above code gives a warning on the declaration of Inner.

I think if you change your definition to actually hide a member (M) you
will remove that error.
class MyClass
{
public virtual int M() { return 1; }
}

class Outer : MyClass
{
public new class M : MyClass
{
}
}

class C2 : MyClass
{
public new struct M
{
}
}


And on the "protected" side - it again relates to things that subclass
the containing class - i.e.

public class BaseClass {
void TestBase() {
NestedStruct ns;
}
private struct NestedStruct { int a;}
}
public class SubClass : BaseClass {
void TestSub() {
NestedStruct ns; // **ERROR
}
}

The above is illegal, since SubClass cannot see BaseClass.NestedStruct
(since it is private); however, change NestedStruct to be protected,
and any sublasses of BaseClass can now see it.

Marc


Thanks, Marc!! I appeciate!
"Marc Gravell" <ma**********@gmail.comwrote in message
news:11**********************@50g2000hsm.googlegro ups.com...

And on the "protected" side - it again relates to things that subclass
the containing class - i.e.

public class BaseClass {
void TestBase() {
NestedStruct ns;
}
private struct NestedStruct { int a;}
}
public class SubClass : BaseClass {
void TestSub() {
NestedStruct ns; // **ERROR
}
}

The above is illegal, since SubClass cannot see BaseClass.NestedStruct
(since it is private); however, change NestedStruct to be protected,
and any sublasses of BaseClass can now see it.

Marc

--
Tom Baxter


这篇关于??结构上的“新”和“受保护”修饰符??的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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