为什么没有结构继承? [英] why no struct inheritance?

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

问题描述

不允许结构分层的技术原因是什么? WPF有一个很好的Point3D结构。我想添加一些

的额外构造函数来转换我的数据类型。

What''s the technical reason for not allowing structs to be
hierarchical? WPF has a nice Point3D struct. I wanted to add some
additional constructors to it for converting from my data types.

推荐答案

C#扩展方法对你来说可能至少是部分解决方案,如果你使用C#3.0可以使用C#3.0,这对于几个月的生产位来说是不可能的。然而:

http://bobondevelopment.com/2007/04/...nsion-methods/

不允许你添加构造函数,但是它允许你

以其他方式扩展现有的课程(包括那些你没有来源的课程)。


在此期间,也许一个静态帮助器方法来转换你的
参数并返回所需的结构也同样适用。


--Bob


not_a_commie写道:
C# extension methods may be at least a partial solution for you, if you
can live with using C# 3.0, which won''t be to production bits for a few
months yet:

http://bobondevelopment.com/2007/04/...nsion-methods/

That won''t allow you to add a constructor, but it will allow you to
extend existing classes (including those you don''t have source to) in
other ways.

In the meantime, perhaps a static helper method to convert your
arguments and return the desired struct will serve just as well.

--Bob

not_a_commie wrote:

不允许结构分层的技术原因是什么? WPF有一个很好的Point3D结构。我想为它添加一些

的额外构造函数,以便从我的数据类型转换。
What''s the technical reason for not allowing structs to be
hierarchical? WPF has a nice Point3D struct. I wanted to add some
additional constructors to it for converting from my data types.


not_a_commie< no *** *****@gmail.com写道:
not_a_commie <no********@gmail.comwrote:

不允许结构化的技术原因是什么?b
hierarchical?
What''s the technical reason for not allowing structs to be
hierarchical?



嗯,允许继承是合理可行的,只要

你不想要多态方法而且没有添加任何成员

变量。你看,如果我宣布:


struct Point

{

int x;

int y;


//其他操作

}


然后代码中的其他地方:


点数;


然后是点变量有8个字节的空间 - x为4,y为4。如果

我们能够做到:


点数=新的ExtendedPoint();


然后:

a)运行时无法知道值的实际类型,因为

没有任何空间

b)那里不会有任何额外成员变量的空间

ExtendedPoint

我敢肯定会有这样的方法,这是值类型继承已经

从一开始就是理想的,但目前的设计并不允许它在

全部。


正如Bob所说,C#中的扩展方法3可能会帮到你。


-

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

如果回复对于小组,请不要给我发邮件

Well, it would be reasonably feasible to allow inheritance so long as
you didn''t want polymorphic methods and didn''t add any member
variables. You see, if I declare:

struct Point
{
int x;
int y;

// Other operations
}

and then elsewhere in your code:

Point point;

then the "point" variable has room for 8 bytes - 4 for x, 4 for y. If
we were able to do:

Point point = new ExtendedPoint();

then:
a) the runtime couldn''t know the actual type of the value, because
there isn''t any room for it
b) there wouldn''t be room for any extra member variables of
ExtendedPoint
I''m sure there would be ways round this is value type inheritance had
been desired from the start, but the current design doesn''t allow it at
all.

As Bob said, extension methods in C# 3 may well help you.

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


可以你只是提供你的新类型的演员定义吗?


Marc

Could you just provide a cast definition from your new types?

Marc


这篇关于为什么没有结构继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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