嵌套类......或者其他东西。 [英] nested classes... or something.

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

问题描述

我只是想做点什么,并意识到我不确定是否正确(如果

那里* IS *是正确的)方法。我正在设计一个有很多

方法/属性的课程。我想将它们中的一些组合在一起,以便您访问它们。

如MyObject.Commands.blah1,MyObject.Commands.blah2等...所以有些

方法在命令下分组。


我的第一个想法是嵌套类,但嵌套的

命令对象中的方法如何(轻松)访问MyObject的数据成员?我真的不希望
想要创建一个命令实例并传递一堆数据

来初始化它。我想做的就是MyObject的组方法/属性

所以这个类的用户不需要处理大量的方法,而不是来自

的根。对象。


我在想错误吗?有没有其他/更好/更合适的

方式来做到这一点?或者我是否因为想要做这么奇怪的事而疯狂?

:)

解决方案

在VB.NET中:


公共类MyClass


私有m_InnerClass为新MyInnerClass


公共只读属性InnerClass()作为MyInnerClass

获取

返回m_InnerClass

结束获取

结束物业


结束班级


创建MyClass实例时,例如


Dim mc as New MyClass

你可以通过内部课程获得


mc.InnerClass.SomeMethod


HTH

Charles

nobody <无**** @ no.server>在消息中写道

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

我刚刚尝试做一些东西,并意识到我不确定正确的(如果有*是*适当的)方法来做到这一点。我正在设计一个有很多方法/属性的课程。我想将它们中的一些组合在一起,以便您可以访问它们,例如MyObject.Commands.blah1,MyObject.Commands.blah2等......因此,一些方法被分组在命令下。

我的第一个想法是嵌套类,但是
嵌套的Commands对象中的方法如何(轻松地)访问MyObject的数据成员?我真的
不想创建一个命令实例并传递一堆数据来初始化它。我想做的只是
MyObject的组方法/属性,所以该类的用户不必处理对象根目录下的大量方法。
而疯狂? :)



在VB.NET中:


公共类MyClass


私有m_InnerClass为新MyInnerClass


公共只读属性InnerClass()为MyInnerClass

获取

返回m_InnerClass

结束获取

结束物业


结束课程


创建时MyClass的一个实例,例如


Dim mc as New MyClass


你可以通过
获得内部课程>
mc.InnerClass.SomeMethod


HTH


Charles

" nobody" <无**** @ no.server>在消息中写道

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

我刚刚尝试做一些东西,并意识到我不确定正确的(如果有*是*适当的)方法来做到这一点。我正在设计一个有很多方法/属性的课程。我想将它们中的一些组合在一起,以便您可以访问它们,例如MyObject.Commands.blah1,MyObject.Commands.blah2等......因此,一些方法被分组在命令下。

我的第一个想法是嵌套类,但是
嵌套的Commands对象中的方法如何(轻松地)访问MyObject的数据成员?我真的
不想创建一个命令实例并传递一堆数据来初始化它。我想做的只是
MyObject的组方法/属性,所以该类的用户不必处理对象根目录下的大量方法。
而疯狂? :)



是的,但是m_InnerClass的方法和属性如何访问MyClass中的

数据?我想要放入MyInnerClass

的方法和属性都是需要处理MyClass中数据的方法和属性。

我真的不想拥有当它实例化时,MyClass将它的数据传递给m_InnerClass。这听起来很蹩脚,更不用说有两个

的数据副本真正(对于用户到MyClass)只有一个

对象。我不希望方法和属性分开。来自

MyClass,只是分组不知怎的,所以没有那么多它们离开了MyClass对象的根源。但我不认为有一种方法可以做到这一点

而不将方法和属性从MyClass中分离出来。 :(


" Charles Law" bl *** @ nowhere.com>在留言中写道

news:eb ****** ******** @ TK2MSFTNGP10.phx.gbl ...

在VB.NET中:

Public Class MyClass

私有m_InnerClass作为新的MyInnerClass

公共只读属性InnerClass()作为MyInnerClass
获取
返回m_InnerClass
结束获取
结束财产
结束课程

当你创建一个MyClass的实例时,例如

Dim mc as New MyClass

你可以通过

mc.InnerClass.SomeMethod

HTH

查尔斯

" nobody"< no **** @ no.server>在消息中写道
新闻:uF ************** @ TK2MSFTNGP12.phx.gbl ...

我只是试图做事情并意识到我不确定是否正确(如果有*是*正确的)这样做的方式。我正在设计一个有很多的课程。方法/属性。我想将其中的一些分组,以便您访问
,例如MyObject.Commands.blah1,MyObject.Commands.blah2等...因此,一些
的方法被分组在命令下。

我的第一个想法是嵌套类,但


嵌套

命令对象(轻松)访问数据成员的方法如何?为MyObject?我真的


不要

想要创建一个命令实例并传递一堆
数据来初始化它。我想做的就是

的组方法/属性
MyObject

所以这个类的用户不需要处理大量的方法,而不是
对象的根源。

我在想错误的路线吗?有没有其他/更好/更多
这样做的正确方法?或者我是否因为想要做这么奇怪的


而感到疯狂?

:)




I just tried to do something and realized I''m not sure of the proper (if
there *IS* a proper) way to do it. I''m designing a class with a lot a
methods/properties. I would like to group some of them so you access them
like MyObject.Commands.blah1, MyObject.Commands.blah2, etc... So some of
the methods are grouped under "Commands".

My first thought was a nested class, but how would the methods in the nested
Commands object (easily) access the data members of MyObject? I realy don''t
want to have to create an instance of Commands and pass it a bunch of data
to initialize it. All I want to do is group methods/properties of MyObject
so the user of the class doesn''t have to deal with tons of methods off the
root of the object.

Am I thinking along the wrong lines? Is there another/better/more proper
way to do this? Or am I crazy for even wanting to do such a strange thing?
:)

解决方案

In VB.NET:

Public Class MyClass

Private m_InnerClass As New MyInnerClass

Public Readonly Property InnerClass() As MyInnerClass
Get
Return m_InnerClass
End Get
End Property

End Class

When you create an instance of MyClass, e.g.

Dim mc as New MyClass

you can get at the inner class by

mc.InnerClass.SomeMethod

HTH

Charles
"nobody" <no****@no.server> wrote in message
news:uF**************@TK2MSFTNGP12.phx.gbl...

I just tried to do something and realized I''m not sure of the proper (if
there *IS* a proper) way to do it. I''m designing a class with a lot a
methods/properties. I would like to group some of them so you access them
like MyObject.Commands.blah1, MyObject.Commands.blah2, etc... So some of
the methods are grouped under "Commands".

My first thought was a nested class, but how would the methods in the nested Commands object (easily) access the data members of MyObject? I realy don''t want to have to create an instance of Commands and pass it a bunch of data
to initialize it. All I want to do is group methods/properties of MyObject so the user of the class doesn''t have to deal with tons of methods off the
root of the object.

Am I thinking along the wrong lines? Is there another/better/more proper
way to do this? Or am I crazy for even wanting to do such a strange thing? :)



In VB.NET:

Public Class MyClass

Private m_InnerClass As New MyInnerClass

Public Readonly Property InnerClass() As MyInnerClass
Get
Return m_InnerClass
End Get
End Property

End Class

When you create an instance of MyClass, e.g.

Dim mc as New MyClass

you can get at the inner class by

mc.InnerClass.SomeMethod

HTH

Charles
"nobody" <no****@no.server> wrote in message
news:uF**************@TK2MSFTNGP12.phx.gbl...

I just tried to do something and realized I''m not sure of the proper (if
there *IS* a proper) way to do it. I''m designing a class with a lot a
methods/properties. I would like to group some of them so you access them
like MyObject.Commands.blah1, MyObject.Commands.blah2, etc... So some of
the methods are grouped under "Commands".

My first thought was a nested class, but how would the methods in the nested Commands object (easily) access the data members of MyObject? I realy don''t want to have to create an instance of Commands and pass it a bunch of data
to initialize it. All I want to do is group methods/properties of MyObject so the user of the class doesn''t have to deal with tons of methods off the
root of the object.

Am I thinking along the wrong lines? Is there another/better/more proper
way to do this? Or am I crazy for even wanting to do such a strange thing? :)



Right, but how would the methods and properties of m_InnerClass access the
data in MyClass? The methods and proprties I want to put in MyInnerClass
are all methods and properties that need to work with the data in MyClass.
I realy don''t want to have MyClass pass it''s data into m_InnerClass when it
instantiates it. That just sounds so kludgy, not to mention having two
copies of the data for what is really (to the user to MyClass) just one
object. I don''t want the methods and properties to be "separate" from
MyClass, just "grouped" somehow so there aren''t so many of them off the root
of the MyClass object. But I don''t think there is a way I can do that
without separating the methods and properties from MyClass. :(

"Charles Law" <bl***@nowhere.com> wrote in message
news:eb**************@TK2MSFTNGP10.phx.gbl...

In VB.NET:

Public Class MyClass

Private m_InnerClass As New MyInnerClass

Public Readonly Property InnerClass() As MyInnerClass
Get
Return m_InnerClass
End Get
End Property

End Class

When you create an instance of MyClass, e.g.

Dim mc as New MyClass

you can get at the inner class by

mc.InnerClass.SomeMethod

HTH

Charles
"nobody" <no****@no.server> wrote in message
news:uF**************@TK2MSFTNGP12.phx.gbl...

I just tried to do something and realized I''m not sure of the proper (if
there *IS* a proper) way to do it. I''m designing a class with a lot a
methods/properties. I would like to group some of them so you access them like MyObject.Commands.blah1, MyObject.Commands.blah2, etc... So some of the methods are grouped under "Commands".

My first thought was a nested class, but how would the methods in the


nested

Commands object (easily) access the data members of MyObject? I realy


don''t

want to have to create an instance of Commands and pass it a bunch of data to initialize it. All I want to do is group methods/properties of


MyObject

so the user of the class doesn''t have to deal with tons of methods off the root of the object.

Am I thinking along the wrong lines? Is there another/better/more proper way to do this? Or am I crazy for even wanting to do such a strange


thing?

:)




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

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