更改派生类中的访问修饰符级别 [英] Changing access modifier levels in a derived class

查看:80
本文介绍了更改派生类中的访问修饰符级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,可以通过using

关键字来更改派生类中的访问级别。因此,例如,基类中的公共方法可以在派生类中实现
private。有没有办法在C#中这样做?

In C++ one can change the access level in a derived class by the "using"
keyword. So, for instance, a public method in a base class can be made
private in a derived class. Is there any way of doing this in C# ?

推荐答案



" Edward Diener" <二*************** @ ORsoftware.com>写在消息

新闻:O8 ************** @ TK2MSFTNGP12.phx.gbl ...

"Edward Diener" <di***************@ORsoftware.com> wrote in message
news:O8**************@TK2MSFTNGP12.phx.gbl...
在C ++中可以改变通过using
关键字在派生类中的访问级别。因此,例如,基类中的公共方法可以在派生类中变为私有。有没有办法在C#中这样做?
In C++ one can change the access level in a derived class by the "using"
keyword. So, for instance, a public method in a base class can be made
private in a derived class. Is there any way of doing this in C# ?




不,C#不允许更改方法的可访问性(不确定

然而,关于IL。


出于好奇,这会允许什么样的场景?除了设计糟糕的基类之外,是否有一个

使用,这是我能想到的唯一的东西?



No, C# does not allow for the changing of accessibility of methods(not sure
about IL, however).

Out of curiosity, what kind of scenarios would this allow? Does it have a
use outside of maneveruing around badly designed base classes, which is the
only thing I can think of off hand?


不正确。


C#允许您更改

派生类中基类项的可访问性,但需要注意。


首先,(可能很明显)你可以扩大访问范围。您可以覆盖

保护的基类项目,并在

派生类中声明覆盖公共。


其次,(和我认为这就是爱德华所要求的,你可以使用

" new"一个关键字,表示您的派生类对基类项具有新的

实现。 新字样派生项目可以有任何访问权限,包括(通常)较窄的访问权限。因此,如果您的基类中有受保护的方法,则可以使用new声明相同的方法

。派生类中的关键字并将其设为私有。


然而,它并非那么简单。


因为新字样派生类方法不是覆盖(它是
重定义),如果有人将你的派生类对象强制转换为它的基类

类,那么基类方法将是调用而不是派生的

类方法。请记住:新派生类方法不是

覆盖并且不会创建多态行为。例如:


A级

{

public virtual int Add(int a,int b)

{...}

}


B级:A

{

public new int Add(int a,int b)

{...}

}


B aB = new B ();

int x = aB.Add(1,2);

A anA = aB;

int y = anA.Add (1,2);


在这个例子中,对aB.Add()的第一次调用将调用B.Add。

秒呼叫anA.Add将调用A.Add()。如果B.Add已被宣布

公共覆盖而不是public new,两个调用都会调用

B.Add()。


当然,您可以声明B.Add" ;私人新或者给它任意

你喜欢的访问级别。

Not true.

C# does allow you to change accessibility of base class items in a
derived class, but with caveats.

First, (perhaps obviously) you can widen access. You can override a
protected base class item and declare the override public in the
derived class.

Second, (and I think this is what Edward was asking), you can use the
"new" keyword to indicate that your derived class has a new
implementation for a base class item. The "new" derived item can have
any access at all, including (usually) narrower access. So if you have
a protected method in your base class, you can declare that same method
with the "new" keyword in your derived class and make it private.

However, it''s not that simple.

Because the "new" derived class method is not an override (it''s a
redefinition), if someone casts your derived class object to its base
class, then the base class method will be called instead of the derived
class method. Remember: the "new" derived class method is not an
override and does not create polymorphic behaviour. For example:

class A
{
public virtual int Add(int a, int b)
{ ... }
}

class B : A
{
public new int Add(int a, int b)
{ ... }
}

B aB = new B();
int x = aB.Add(1, 2);
A anA = aB;
int y = anA.Add(1, 2);

In this example, the first call to aB.Add() would call B.Add. The
second call, anA.Add, would call A.Add(). If B.Add had been declared
"public override" instead of "public new", both calls would have
invoked B.Add().

You can, of course, declare B.Add "private new" or give it whatever
access level you like.




" Bruce Wood" <峰; br ******* @ canada.com>在消息中写道

news:11 ********************** @ c13g2000cwb.googlegr oups.com ...

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
不正确。

C#允许您更改
派生类中基类项的可访问性,但需要注意。

首先,(也许很明显)你可以扩大访问范围。您可以覆盖
受保护的基类项,并在
派生类中声明覆盖公共。
Not true.

C# does allow you to change accessibility of base class items in a
derived class, but with caveats.

First, (perhaps obviously) you can widen access. You can override a
protected base class item and declare the override public in the
derived class.




我很乐意看到这样的例子。我只是尝试了它并得到了这个错误:


''Test1.Test.TestMethod()'':覆盖时无法更改访问修饰符

''protected ''继承成员''Test.Program.TestMethod()''



I''ve love to see an example of this. I just tried it and got this error:

''Test1.Test.TestMethod()'': cannot change access modifiers when overriding
''protected'' inherited member ''Test.Program.TestMethod()''


这篇关于更改派生类中的访问修饰符级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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