嵌套类 [英] Nested Classes

查看:74
本文介绍了嵌套类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否无法在单独的文件中声明嵌套类来自

其父" class - 即类似于在多个文件中传播
名称空间的想法?

解决方案

AFAIK ,不,没有。属于同一命名空间的所有成员的类

彼此相等,因此Class1不依赖于Class2。但是,你将
拉出嵌套类,并将其作为自己的类并相应地引用它或者使用继承或接口来实现类似的/>
效果。

" C#Learner" < CS **** @ learner.here>在消息中写道

news:ei ******************************** @ 4ax.com ...

是否无法在单独的文件中声明嵌套类,而不是父。 class - 即类似于在多个文件上传播名称空间的想法?



" C#Learner" < CS **** @ learner.here>在消息中写道

news:ei ******************************** @ 4ax.com ...

是否无法在单独的文件中声明嵌套类,而不是父。 class - 即类似于在多个文件上传播名称空间的想法?




C#Learner,


不在C#v1.0中。但是,根据C#v2.0规范,在C#v2.0中它可能是



Joe

-
http://www.csharp-station.com


" William Ryan" <做******** @ nospam.comcast.net>写道:

AFAIK,没有没有。属于同一名称空间的所有成员的类彼此相等,因此Class1不依赖于Class2。但是,您可以将嵌套类拉出来并使其成为自己的类并相应地引用它或使用继承或接口来实现类似的效果。




我想到的情况是这样的:


我有一个类(MyClass)变得越来越大(在线代码)和

我想把它分解成几部分。我立刻就知道我可以在哪里赚取一个对象。它的一些功能。


现在,这个新对象只需要被MyClass实例知道,并且

不在其他地方。我不希望这个新课程可见。任何事情

但MyClass。


因此,我决定让它成为一个嵌套类。但是,这个嵌套的

类本身相当大,并且必须在其

parent中声明它。 class,在同一个文件中,有点负担。


首先,嵌套类的所有方法都从一个额外的

缩进级别开始。另一方面,整个文件(包括MyClass和

,它的嵌套类)仍然很长。


我想我只需要另外一堂课与...相同的命名空间
MyClass ...虽然,在我看来,这有点像黑客。


干杯


Is it not possible to declare a nested class in a seperate file from
its "parent" class -- i.e. in a similar way to the idea of spreading
namespaces over more than one file?

解决方案

AFAIK, no there isn''t. Classes that are all members of the same namespace
are ''equal'' to each other so Class1 doesn''t depend on Class2. However, you
could pull the nested class out and make it it''s own class and reference it
accordingly or use inheritance or an interface to accomplish a similar
effect.
"C# Learner" <cs****@learner.here> wrote in message
news:ei********************************@4ax.com...

Is it not possible to declare a nested class in a seperate file from
its "parent" class -- i.e. in a similar way to the idea of spreading
namespaces over more than one file?



"C# Learner" <cs****@learner.here> wrote in message
news:ei********************************@4ax.com...

Is it not possible to declare a nested class in a seperate file from
its "parent" class -- i.e. in a similar way to the idea of spreading
namespaces over more than one file?



Hi C# Learner,

Not in C# v1.0. However, according to the C# v2.0 specification, it will be
possible in C# v2.0.

Joe
--
http://www.csharp-station.com


"William Ryan" <do********@nospam.comcast.net> wrote:

AFAIK, no there isn''t. Classes that are all members of the same namespace
are ''equal'' to each other so Class1 doesn''t depend on Class2. However, you
could pull the nested class out and make it it''s own class and reference it
accordingly or use inheritance or an interface to accomplish a similar
effect.



The situation I have in mind is something like this:

I have a class (MyClass) which is getting big (in lines of code) and
I''d like to break it down into parts. Immediately I see where I could
make an "object" out of some of its functionality.

Now, this new object only needs to be known by MyClass instances, and
nowhere else. I don''t want this new class to be "visible" to anything
but MyClass.

Therefore, I decide to make it a nested class. However, this nested
class is fairly big itself, and having to declare it within its
"parent" class, in the same file, is a bit of a burden.

For one thing, all methods of the nested class start at an extra
indentation level. For another, the whole file (including MyClass and
its nested class) remains long.

I guess I''ll just have to make another class in the same namespace as
MyClass... Though, in my mind, this is a bit of a "hack".

Cheers


这篇关于嵌套类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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