为什么我不能继承DateTime? [英] Why can't I inherit from DateTime?

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

问题描述

任何想法?

解决方案

因为System.DateTime是一个密封的类,这意味着你无法扩展(b $ b)继承自它。


但是为什么你想在世界上扩展DateTime?


在最坏的情况下,创建一个类,祝你有一个DateTime字段。


希望这会有所帮助。

祝你好运。


2月22日星期二2005 18:25:32 -0800,Frank Rizzo< no ** @ none.com>写道:

任何想法?




-

问候,

Nurchi BECHED


PS

C让你轻松拍摄自己的脚;

C ++让它变得更难,但是当你这样做的时候,

它会吹走你的整条腿。

--Barne Stroustrup


? ???根据文档,DateTime是一个值类型,一个结构和你

不能扩展结构。


问候,

杰夫

因为System.DateTime是一个密封的类,这意味着你不能



扩展(继承)它。<


***通过开发人员指南 http://www.developersdex.com 发送* **

不要只是参加USENET ......获得奖励!


" Frank Rizzo" <无** @ none.com>在消息中写道

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

任何想法?




查看值类型和引用类型,在C#中称为结构与

类。

在很多方面结构与类完全相反。


以下是一些差异。


类实例在堆栈上分配引用或堆,

对象总是在堆上。 struct实例直接在堆栈上分配(对于

方法中的本地实例)或嵌入在堆上的对象中。


类是可继承的。结构被隐式密封,并始终从Object继承

。 (这回答了你的问题)


一个类可以强制转换为它自己以及它类型的任何超类。一个struct

只能装入Object并取消装入它的密封类型。


一个类支持多态性。由于您可以在Object类上覆盖或重新引入方法,因此结构对多态性的支持有限。

。但是,

抽象或虚拟关键字对值类型没有意义,因此在C#中禁止使用



A类实例是垃圾收集。当一个方法结束时,或者当一个

类实例被垃圾收集时,从堆中隐含地收集了一个堆栈上的结构实例。


>
课程是一件很美好的事情。结构糟透了! =)


希望这会有所帮助


- Michael S




Any ideas?

解决方案

Because System.DateTime is a sealed class which means that you
cannot extend (inherit from) it.

But why in the world would you want to extend DateTime?

In the worst case, create a class, and make a DateTime field.

Hope this helps.
Good luck.

On Tue, 22 Feb 2005 18:25:32 -0800, Frank Rizzo <no**@none.com> wrote:

Any ideas?



--
Regards,
Nurchi BECHED

P.S.
C makes it easy to shoot yourself in the foot;
C++ makes it harder, but when you do,
it blows away your whole leg."
--Bjarne Stroustrup


???? According to the docs DateTime is a value type, a struct and you
cannot extend a struct.

Regards,
Jeff

Because System.DateTime is a sealed class which means that you cannot


extend (inherit from) it.<

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!


"Frank Rizzo" <no**@none.com> wrote in message
news:On**************@TK2MSFTNGP12.phx.gbl...

Any ideas?



Have a look at value types and reference types, called structs versus
classes in C#.
In many ways, a struct is quite an opposite of a class.

Here are some differences.

A class instance is allocated with reference on stack or heap, with the
object always on heap. A struct instance is allocated directly on stack (for
local instances in methods) or embedded in an object on heap.

A class is inheritable. A struct is implicitly sealed and always inherits
from Object. (This answers your question)

A class can be cast to itself and to any superclass of it''s type. A struct
can only be boxed into Object and unboxed to it''s sealed type.

A class supports polymorhpism. Structs have limited support for polymorfism
as you can override or reindtroduce methods on the Object class. However,
the abstract or virtual keyword makes non sense on value types and are so
forbidden in C#.

A class instance is garbage collected. A struct instance on stack is simply
removed when a method ends or are implictly collected from heap when when an
class instance is garbage collected.

A class is a beautiful thing. structs sucks! =)

Hope this helps

- Michael S




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

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