OOP新手 [英] New to OOP

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

问题描述

我为产品列表创建了一个类,实际上并不太难!

我已经创建了产品名称,大小等属性。但是,每个产品

可能有一个或多个他们适合的类别,例如牙膏可能会被带入杂货物品中,并且可能属于药店物品。我是否为这些类别创建了一个

单独的类,并将这个类链接为

数据库,一对多?我怎么处理这个,也许是一个属性,因为

a列表会更好,我可以这样做吗?

I have created a class for a list of products, not too difficult actually!
I''ve created properties for product name, size, etc. However, each product
may have one or more categories they fit in to, such as a toothpaste may go
into grocery items and it may go under drugstore items. Do I create a
separate class for these categories, and link that class like you would the
database, a one to many? How else would I handle this, maybe a property as
a list would be better, can I do that?

推荐答案



" Kat" < Ka ********** @ yahoo.comwrote in message

新闻:O9 ************** @ TK2MSFTNGP03.phx.gbl ...

"Kat" <Ka**********@yahoo.comwrote in message
news:O9**************@TK2MSFTNGP03.phx.gbl...

>我已经为产品列表创建了一个类,实际上并不太难!
我已经创建了产品名称的属性但是,每种产品可能都有一个或多个适合的类别,例如牙膏可能会进入杂货物品,而且可能会进入药店物品。
我为这些类别创建了一个单独的类,并将该类链接为像数据库一样,一对多?我怎么办呢?
也许作为一个列表的财产会更好,我可以这样做吗?
>I have created a class for a list of products, not too difficult actually!
I''ve created properties for product name, size, etc. However, each
product may have one or more categories they fit in to, such as a
toothpaste may go into grocery items and it may go under drugstore items.
Do I create a separate class for these categories, and link that class
like you would the database, a one to many? How else would I handle this,
maybe a property as a list would be better, can I do that?



我会为类别创建一个单独的类,其中CategoryID字段

是主键。

然后为您的Product类添加一个属性,用于相应的

类别的实例,以及类别名称的属性。


Private _CategoryInstance作为类别

公共财产CategoryInstance()作为类别

获取

返回_CategoryInstance

结束获取

私人套装(ByVal值为类别)

_CategoryInstance = value

结束套件

结束财产

Public ReadOnly属性CategoryName()As String

获取

返回CategoryInstance.CategoryName

结束获取

结束物业


私有_CategoryID为整数

公共财产CategoryID()为整数

获取

返回_CategoryID

结束获取

私人套装(ByVal值为整数)

' '这是你的

''产品类中的CategoryID字段的设置。

''当这个设置时,去读数据库或其他什么和

''填写特定ID的Category实例。

''如果值没有改变,不要重新获取类别描述

如果_CategoryID< value则

_CategoryID = value

CategoryInstance = Category.Create(CategoryID)

结束如果

结束集

结束属性


在我的Category类中创建方法会创建一个新的

实例该ID的类别,并获取

的任何相应字段并填写它们。


希望这会有所帮助。

Robin S.

Ts''i mahnu uterna ot twan ot geifur hingts uto。


I would make a separate class for the categories, with a CategoryID field
being the primary key.

Then add a property to your Product class for a corresponding instance of
Category, and a property for the category name.

Private _CategoryInstance As Category
Public Property CategoryInstance() As Category
Get
Return _CategoryInstance
End Get
Private Set(ByVal value As Category)
_CategoryInstance = value
End Set
End Property

Public ReadOnly Property CategoryName() As String
Get
Return CategoryInstance.CategoryName
End Get
End Property

Private _CategoryID As Integer
Public Property CategoryID() As Integer
Get
Return _CategoryID
End Get
Private Set(ByVal value As Integer)
''This is the Set for the CategoryID field in your
'' Product class.
''When this is set, go read the database or whatever and
'' populate your Category instance for that specific ID.
''Don''t re-get the category description if the value hasn''t changed
If _CategoryID <value then
_CategoryID = value
CategoryInstance = Category.Create(CategoryID)
End If
End Set
End Property

The Create method in what would be my Category class would create a new
instance of Category for that ID, and go get any corresponding fields for
it and fill them in.

Hope this helps.
Robin S.
Ts''i mahnu uterna ot twan ot geifur hingts uto.



我不会'不用担心OOP


OOP让你的程序运行得更慢;这意味着你'不应该使用它''


-Aaron

I wouldn''t worry about OOP

OOP makes your programs run slower; that means you ''shoudln''t use it''

-Aaron




< aa ********* @ gmail.comwrote in message

news:11 ***************** ****@v33g2000cwv.googlegro ups.com ...

<aa*********@gmail.comwrote in message
news:11*********************@v33g2000cwv.googlegro ups.com...

>我不担心OOP


OOP使你的程序运行得更慢;这意味着你'不应该使用它''


-Aaron
>I wouldn''t worry about OOP

OOP makes your programs run slower; that means you ''shoudln''t use it''

-Aaron



天哪,我想你没有'不正确地写它们,否则它们会更快。为什么

你不发布你的一些代码,我们会帮你提高它的b / b
性能吗?


罗宾S.

Ts''i mahnu uterna ot twan ot geifur hingts uto。

Gosh, I guess you didn''t write them correctly, or they would be faster. Why
don''t you post some of your code, and we will help you improve its
performance?

Robin S.
Ts''i mahnu uterna ot twan ot geifur hingts uto.


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

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