C#覆盖限制..为什么? [英] C# Override limitation..why?

查看:68
本文介绍了C#覆盖限制..为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能用C#


公共类A

{

public a virtual whatever( A)

{

}

}


公共等级B:A
{

公共B覆盖任何(B b)

{

}

}


为什么覆盖的签名必须完全匹配基础

版本?


看起来像是一个这样做非常安全,为什么我不能这样做?在

的某些情况下,它会通过重复的方法节省大量的演员。


Cliff

Why can''t I do this in C#

public class A
{
public A virtual whatever( A a )
{
}
}

public class B : A
{
public B override whatever( B b )
{
}
}

Why does the signature of the override have to match exactly the base
version?

It seems like a perfectly safe thing to do so why can''t I do it? In
some situations it saves lots of casting with duplicate methods.

Cliff

推荐答案

< Cl ********** @ hotmail.com> schrieb im Newsbeitrag

新闻:11 ********************** @ g43g2000cwa.googlegr oups.com ...
<Cl**********@hotmail.com> schrieb im Newsbeitrag
news:11**********************@g43g2000cwa.googlegr oups.com...
为什么我不能用C#

公共课A
公开虚拟任何(A a)
{
}
}

公共课B:A
公共B覆盖任何东西(B b)

}
>}

为什么覆盖的签名必须与基本
版本完全匹配?

这样做似乎是一件非常安全的事情为什么可以'我做到了吗?在某些情况下,它会通过重复的方法节省大量的演员。

Cliff
Why can''t I do this in C#

public class A
{
public A virtual whatever( A a )
{
}
}

public class B : A
{
public B override whatever( B b )
{
}
}

Why does the signature of the override have to match exactly the base
version?

It seems like a perfectly safe thing to do so why can''t I do it? In
some situations it saves lots of casting with duplicate methods.

Cliff




喜欢Cliff,


考虑以下代码:

A a = new B();

a.whatever(new A());


B中覆盖方法的参数b不是B型。

那是第一个问题。


Christof



Hi Cliff,

consider following code:
A a = new B();
a.whatever (new A());

the parameter b of the override method in B would not be of type B.
That''s th problem.

Christof


这究竟是一件安全的事情?考虑


a a = new B();


a.whatever()的签名是什么?


" Cl ********** @ hotmail.com"写道:
how exactly is it a safe thing to do? consider

A a = new B();

what would the signature of a.whatever() be?

"Cl**********@hotmail.com" wrote:
为什么我不能用C#

公共课A
公开A虚拟(A) a)
{
}
}
公共课B:A
公共B覆盖任何东西(B b)
{
}
}

为什么覆盖的签名必须与基本
版本完全匹配?

好像一个非常安全的事情,为什么我不能这样做?在某些情况下,它可以通过重复的方法节省大量的演员。

Cliff
Why can''t I do this in C#

public class A
{
public A virtual whatever( A a )
{
}
}

public class B : A
{
public B override whatever( B b )
{
}
}

Why does the signature of the override have to match exactly the base
version?

It seems like a perfectly safe thing to do so why can''t I do it? In
some situations it saves lots of casting with duplicate methods.

Cliff



Cliff,


正如其他人已经指出的那样,编译器不知道在所有情况下使用什么

签名。但是,你可以做的是使用

显式接口实现来获得你想要的b / b
的至少一部分。


公共界面A

{

A a(A a);

}


公共课B:A

{

公共B无论如何(B b)

{

Console.WriteLine(" B");

返回b;

}


//显式接口实现

A A.whatever(A a)

{

Console.WriteLine(" A");

返回a;

}

}


static void Main()

{

B b =新B();

A a = b;


//b.whatever(a); //编译错误!

b.whatever(b);

a.whatever(a);

a.whatever(b);

}


这里要注意的重要事项是B仍然需要A'的任何方法

签名以便正确当你使用A

引用类型时调用方法。


Brian

Cl ********** @ hotmail.com 写道:
Cliff,

As others have already pointed out it the compiler doesn''t know what
signature to use in all cases. What you can do, however, is use
explicit interface implementation to get at least part of what you
want.

public interface A
{
A whatever(A a);
}

public class B : A
{
public B whatever(B b)
{
Console.WriteLine("B");
return b;
}

// Explicit Interface Implementation
A A.whatever(A a)
{
Console.WriteLine("A");
return a;
}
}

static void Main()
{
B b = new B();
A a = b;

//b.whatever(a); // compile error!
b.whatever(b);
a.whatever(a);
a.whatever(b);
}

The important thing note here is that B still needs A''s whatever method
signature so that the right method is called when you''re using A
reference types.

Brian

Cl**********@hotmail.com wrote:
为什么不能我在C#

公共课A中这样做。
公开虚拟的任何事情(A a)


}
} />
公共课B:A
公共B覆盖任何(B b)
{
}
}
为什么覆盖的签名必须与基本版本完全匹配?

这样做似乎是一件非常安全的事情,为什么我不能这样做呢?在某些情况下,它会通过重复的方法节省大量的演员。

Cliff
Why can''t I do this in C#

public class A
{
public A virtual whatever( A a )
{
}
}

public class B : A
{
public B override whatever( B b )
{
}
}

Why does the signature of the override have to match exactly the base
version?

It seems like a perfectly safe thing to do so why can''t I do it? In
some situations it saves lots of casting with duplicate methods.

Cliff






这篇关于C#覆盖限制..为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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