找出OOP原则 [英] Figuring out OOP principles

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

问题描述

我正在努力开发一堆课程并希望以正确的方式完成它。

方式。


假设我有5个班级:


City,

邻里,

NeighborhoodCollection,

House,

HouseCollection


集合类是强类型集合,继承

System.Collections.CollectionBase


所以一次我实现了我需要的一切,我可以按如下方式使用它:


City.NeighborhoodCollection(2).Housecollection(3)。价值


所以这里是我的问题:


我在House类中有一个子类需要访问
City类。例如,当调用house.GetAverageIncome()时,

子需要知道房子所在的城市。有意义吗?


唯一的方法我能够弄清楚如何做到这一点是将

引用传递给每个子对象(当在City中调用sub new()时,

它填充了它的NeighborhoodCollection成员并创建了一个

NeighborhoodCollection.MyParent对自己的引用):


dim NeighborhoodToAdd as Neighborhood = new Neighborhood

NeighborhoodToAdd.MyParent =城市

City.NeighborhoodCollection.Add(NeighborhoodToAdd)

这很难看。我该怎样引用

类'父母的属性?


谢谢!


== ==============

乔丹鲍内斯

================


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

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

I''m trying to develop a bunch of classes and want to do it the right
way.

Say I have 5 classes:

City,
Neighborhood,
NeighborhoodCollection,
House,
HouseCollection

The collection classes are strongly typed collections which inherit
System.Collections.CollectionBase

So once I instanciate everything I need, I may use it as follows:

City.NeighborhoodCollection(2).Housecollection(3). value

So here''s my question:

I have a sub in the House class which needs to access some property of
the City class. For example, when calling house.GetAverageIncome(), the
sub needs to know which City the house is in. Make sense?

The only way I was able to figure out how to do this was to pass a
reference down to each child object (when sub new() was called in City,
it populated its NeighborhoodCollection members and created a
NeighborhoodCollection.MyParent reference to itself):

dim NeighborhoodToAdd as Neighborhood = new Neighborhood
NeighborhoodToAdd.MyParent = City
City.NeighborhoodCollection.Add(NeighborhoodToAdd)
This got ugly fast. How should I be referencing the properties in a
class'' parent?

Thanks!

================
Jordan Bowness
================

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

推荐答案

我知道你没有问这个,但为什么不建造一个拥有City

和邻里房产的House类。邻居物业可以从Enum持有一个价值

,你可以将每个房子放在一个Houses集合中。

Jordan Bowness <乔**** @ sapient.ca.nospam>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP09.phx.gbl ...
I know you didn''t ask this but why not make a House class that has a City
and a Neighborhood property. The Neighborhood property could hold a value
from an Enum and you could place each house in a Houses collection.
"Jordan Bowness" <jo****@sapient.ca.nospam> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
我'我试图开发一堆课程,并希望以正确的方式做到这一点。

说我有5个班级:

城市,
邻居,
NeighborhoodCollection,
House,
HouseCollection

集合类是强类型集合继承
System.Collections.CollectionBase
<因此,一旦我实现了我需要的一切,我可以按如下方式使用它:

City.NeighborhoodCollection(2).Housecollection(3)。价值

所以这里是我的问题:

我在House课程中有一个子课程需要访问City课程的一些属性。例如,当调用house.GetAverageIncome()时,
子需要知道房子所在的城市。有意义吗?

我能够弄清楚如何做的唯一方法这是为了将
引用传递给每个子对象(当在City中调用sub new()时,它填充了其NeighborhoodCollection成员并创建了一个
NeighborhoodCollection.MyParent对自身的引用):

昏暗的NeighborhoodToAdd为邻居=新邻居
NeighborhoodToAdd.MyParent =城市
City.NeighborhoodCollection.Add(NeighborhoodToAdd)

这个变得难看。我应该如何引用
类的父级中的属性?

谢谢!

============== ==
Jordan Bowness
================

***通过Developersdex发送 http://www.developersdex.com ***
不要只是参加USENET ...奖励它!
I''m trying to develop a bunch of classes and want to do it the right
way.

Say I have 5 classes:

City,
Neighborhood,
NeighborhoodCollection,
House,
HouseCollection

The collection classes are strongly typed collections which inherit
System.Collections.CollectionBase

So once I instanciate everything I need, I may use it as follows:

City.NeighborhoodCollection(2).Housecollection(3). value

So here''s my question:

I have a sub in the House class which needs to access some property of
the City class. For example, when calling house.GetAverageIncome(), the
sub needs to know which City the house is in. Make sense?

The only way I was able to figure out how to do this was to pass a
reference down to each child object (when sub new() was called in City,
it populated its NeighborhoodCollection members and created a
NeighborhoodCollection.MyParent reference to itself):

dim NeighborhoodToAdd as Neighborhood = new Neighborhood
NeighborhoodToAdd.MyParent = City
City.NeighborhoodCollection.Add(NeighborhoodToAdd)
This got ugly fast. How should I be referencing the properties in a
class'' parent?

Thanks!

================
Jordan Bowness
================

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



" Jordan Bowness" <乔**** @ sapient.ca.nospam>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP09.phx.gbl ...
"Jordan Bowness" <jo****@sapient.ca.nospam> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
我'我试图开发一堆课程,并希望以正确的方式做到这一点。

说我有5个班级:

城市,
邻居,
NeighborhoodCollection,
House,
HouseCollection

集合类是强类型集合继承
System.Collections.CollectionBase
<因此,一旦我实现了我需要的一切,我可以按如下方式使用它:

City.NeighborhoodCollection(2).Housecollection(3)。价值

所以这里是我的问题:

我在House课程中有一个子课程需要访问City课程的一些属性。例如,当调用house.GetAverageIncome()时,
子需要知道房子所在的城市。有意义吗?
I''m trying to develop a bunch of classes and want to do it the right
way.

Say I have 5 classes:

City,
Neighborhood,
NeighborhoodCollection,
House,
HouseCollection

The collection classes are strongly typed collections which inherit
System.Collections.CollectionBase

So once I instanciate everything I need, I may use it as follows:

City.NeighborhoodCollection(2).Housecollection(3). value

So here''s my question:

I have a sub in the House class which needs to access some property of
the City class. For example, when calling house.GetAverageIncome(), the
sub needs to know which City the house is in. Make sense?




这听起来像一个问题,为什么房子知道AverageIncome?那个

似乎不是我家可以为我提供的功能,你知道吗?这个

导致一个房子和整个社区之间的依赖关系。好像

我喜欢你应该把这个功能推到附近,然后

到这个城市。这样,它更有意义,如果它是一个房子的一部分,

唯一它*应该*平均值是居民的收入(拼写

check )。


如果将功能推回到它所属的位置,那么剩下的

代码将自动解决它的问题。

HTH,

Jeremy



This sounds like a problem, why does a house know about AverageIncome? That
doesnt seem like a function that my house can provide for me, you know? This
causes a dependency between a house and then entire neighborhood. Seems to
me like you should push this functionality up to the neighborhood, and then
to the city. This way, it makes more sense, if it is part of a house, the
only thing that it *should* average is the income of the inhabitances (spell
check).

If up push the functionality back where it belongs, then the rest of your
code will un-f*ck it''s self.
HTH,
Jeremy


公共级城市

私人mNeighborhoods作为NeighborhoodCollection

私有mName为字符串


公共ReadOnly属性邻域作为NeighborhoodCollection

获取

返回mNeighborhoods

结束获取

结束财产


公共财产名称为字符串

获取

返回mName

结束获取

设置(ByVal值为字符串)

mName = Value

结束子

结束物业


Public Sub New(ByVal CityName As String)

mNeighborhoods = New NeighborhoodCollection
mName = CityName

结束子

结束班


公共舱社区

私人mName为String = String.Empty

私人城市作为城市


公共财产名称为字符串

获取

返回mName

结束获取

设置(ByVal值为字符串)

mName = Value

结束套装

结束物业


Public ReadOnly物业城作为城市

获取

返回mCity < br $>
结束获取

结束物业


Public Sub New(ByVal City As City,ByVal NeighborhoodName As String)

mCity = City

Me.Name = NeighborhoodName

End Sub

结束班级


模块MainRoutine

Sub Main()

昏暗的社区作为邻居

昏暗的城市作为城市


city = New City(MyCity)

邻居=新邻居(城市,MyNeighborhood)

city.Neighborhoods.Add(city)

End Sub

结束模块

''---------- -------------------------------------------------- ---


上面的代码应该让你知道该做什么,在新闻组编辑器中自由使用

代码,这样就有了问题: )

希望它有所帮助

Mythran
Public Class City
Private mNeighborhoods As NeighborhoodCollection
Private mName As String

Public ReadOnly Property Neighborhoods As NeighborhoodCollection
Get
Return mNeighborhoods
End Get
End Property

Public Property Name As String
Get
Return mName
End Get
Set(ByVal Value As String)
mName = Value
End Sub
End Property

Public Sub New(ByVal CityName As String)
mNeighborhoods = New NeighborhoodCollection
mName = CityName
End Sub
End Class

Public Class Neighborhood
Private mName As String = String.Empty
Private mCity As City

Public Property Name As String
Get
Return mName
End Get
Set(ByVal Value As String)
mName = Value
End Set
End Property

Public ReadOnly Property City As City
Get
Return mCity
End Get
End Property

Public Sub New(ByVal City As City, ByVal NeighborhoodName As String)
mCity = City
Me.Name = NeighborhoodName
End Sub
End Class

Module MainRoutine
Sub Main()
Dim neighborhood As Neighborhood
Dim city As City

city = New City("MyCity")
neighborhood = New Neighborhood(city, "MyNeighborhood")
city.Neighborhoods.Add(city)
End Sub
End Module
'' ---------------------------------------------------------------

The above code should give you an idea of what to do, free-hand
code in newsgroup editor so it''s buggy :)
Hope it helps
Mythran


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

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