继承设计问题 [英] Inheritance design issue

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

问题描述

我想要一些关于使用vb.net实现继承的建议。


我有一个用vb6编写的应用程序,我正在从头开始重写

在vb.net中充分利用.net,但我遇到了设计实现继承的问题。


为了解释我的问题,我会用一个例子。我有一个Person类,带有

属性,如ID,Name,Phone,Address等。然后我有一个经理

类继承Person并添加其他属性,如分部,




我将数据库中的所有人加载到数组列表中作为

Persons类的一部分。


现在,我想从包含其ID的表中加载Managers,在Manager对象中创建

,并引用Person数据,以及然后加载

经理的具体细节。


例如。


Dim oManager作为新经理

oManager = Persons.Find(ID:= 1234)

oManager.Division = ...

....

Managers.Add(oManager)

MsgBox(oManager.Phone.ToString)''显示基地人员的电话号码

对象


现在,这不起作用,因为我得到一个无效的强制转换异常(如预期的那样)。


我不想要的是创建一个新的经理,然后加载来自

人物对象的数据

例如。


Dim oPerson as Person = Persons.Find(ID:= 1234)

将oManager视为新经理

使用oManager

.ID = oPerson.ID

.Name = oPerson.Name

...

.Division = ...

结束时

MsgBox(oManager.Phone.ToString)''在Manager对象中显示电话号码


如果在Person对象中更改了Phone,则不会在
经理对象。


在我看来,这将是OOP面临的常见情况,所以任何

建议如何我可以获得一个继承的对象来维护对

基础对象的数据的引用,或者如何最好地设计解决方案将是最好的b
appriciated。


在此先感谢,


Mark O''Flynn

I would like some advice regarding implementing inheritance using vb.net.

I have an application written in vb6 that I am rewritting from the ground up
in vb.net to take full advantage of .net, but I have encountered a design
issue implementing inheritance.

To explain my question, I will use an example. I have a Person class, with
properties such as ID, Name, Phone, Address etc. I then have a Manager
class that inherits Person and adds additional properties such as Division,
etc.

I load all the people from a database into an array list as part of a
Persons class.

Now, I want to load the Managers from a table that contains their ID, create
the Manager object with a reference to the Person data, and then load the
manager specific detail.

eg.

Dim oManager as New Manager
oManager = Persons.Find(ID:=1234)
oManager.Division = ...
....
Managers.Add(oManager)
MsgBox(oManager.Phone.ToString) '' Show phone number from the base Person
object

Now, this doesnt work as I get an invalid cast exception (as expected).

What I dont want is to create a new Manager, then load it with the data from
the Person Object

eg.

Dim oPerson as Person = Persons.Find(ID:=1234)
Dim oManager as New Manager
With oManager
.ID = oPerson.ID
.Name = oPerson.Name
...
.Division = ...
End With
MsgBox(oManager.Phone.ToString) '' Show phone number from the Manager object

If Phone is changed in the Person object, this will not be reflected in the
Manager object.

It seems to me that this would be a common situation faces in OOP, so any
advice on how I can get an inheritied object to maintain a reference to the
base object''s data, or how best to design a solution would be most
appriciated.

Thanks in advance,

Mark O''Flynn

推荐答案

" Mark O''Flynn" ; <毫安********* @ ozemail.com.au>写在

新闻:DI **************** @ nnrp1.ozemail.com.au:
"Mark O''Flynn" <ma*********@ozemail.com.au> wrote in
news:DI****************@nnrp1.ozemail.com.au:
如果在Person对象中更改Phone,则不会在Manager对象中反映出来。

在我看来,这将是OOP中常见的情况,所以
关于如何获得继承对象以维护对基础对象数据的引用的任何建议,或者如何最好地设计解决方案
将是最受关注的。
If Phone is changed in the Person object, this will not be reflected
in the Manager object.

It seems to me that this would be a common situation faces in OOP, so
any advice on how I can get an inheritied object to maintain a
reference to the base object''s data, or how best to design a solution
would be most appriciated.



我想你会想要将你的经理手机属性声明为Shadows

手机。这允许你创建一个名为

手机的同名的proprety,它不会覆盖基类属性:


Shadows

可选。指示此属性在基类中隐藏同名的

编程元素或一组重载元素。你

可以影响任何其他类型的声明元素。如果你使用另一个属性遮蔽一个属性,那么参数和返回

类型不必与基类属性中的那些匹配。在派生类中隐藏它的阴影

元素不可用,除非阴影元素不可访问,否则
,例如如果它是

私有。

注意您不能在相同的

属性声明中指定Overloads和Shadows。


访问bass类数据你需要提供一个属性来

访问低音类对象(即GetParentObject将返回

Mybase)或提供一个属性来访问低音类属性(即

GetPersonPhone)。


MyBase

MyBase关键字的行为类似于指向基础的对象变量

类当前实例的类。 MyBase通常用于
访问基类成员,这些成员在派生的

类中被覆盖或隐藏。特别是,MyBase.New用于从派生类构造函数显式调用基类

构造函数。


使用MyBase调用MustOverride无效基本方法。

这些只是我头脑中的想法......并且未经测试。也许

有更多OO经验的人可以提供更好的解决方案。

-

Lucas Tam(RE ******** @ rogers.com)

请删除删除来自回复时的电子邮件地址。
http://成员。 ebay.com/aboutme/coolspot18/




" Mark O''Flynn" <毫安********* @ ozemail.com.au>写了

"Mark O''Flynn" <ma*********@ozemail.com.au> wrote
Dim oManager作为新经理
oManager = Persons.Find(ID:= 1234)
oManager.Division = ...
.. 。
Managers.Add(oManager)
MsgBox(oManager.Phone.ToString)''显示基地人物
对象的电话号码

现在,这不起作用因为我得到了一个无效的强制转换异常(正如预期的那样)。
Dim oManager as New Manager
oManager = Persons.Find(ID:=1234)
oManager.Division = ...
...
Managers.Add(oManager)
MsgBox(oManager.Phone.ToString) '' Show phone number from the base Person
object

Now, this doesnt work as I get an invalid cast exception (as expected).




它在哪里不足?如果Manager继承自Person,则Msgbox

应该显示电话号码。


如果它在Persons.Find()上失败,可能是因为该函数返回一个Person

对象而不是manager对象。如果是这种情况,您可以将一个共享

函数(FromPerson)添加到manager类,以将person对象转换为

manager对象,或者您可能会重载构造函数允许一个人

对象来初始化新的经理对象。


上面列出了我自己的偏好顺序....


HTH

LFS



Where does it fall short? If Manager inherits from Person, then the Msgbox
should show the Phone number.

If it fails on Persons.Find() it may be because that function returns a Person
object instead of a manager object. If that is the case you might add a Shared
function (FromPerson) to the manager class to convert a person object to a
manager object, or you might overload your constructor to allow a person
object to initialize the new manager object.

The above were listed in my own order of preference....

HTH
LFS


它在哪里不足?如果Manager继承自Person,则Msgbox
应该显示电话号码。

当多态地使用经理作为Person时,它就会失败。使用他拥有的设计更改经理电话号码

,它不会反映在* Base *中,反之亦然,因为

基本上是

他所做的只是创建一个浅拷贝。

如果它在Persons.Find()上失败,可能是因为该函数返回一个Person
对象而不是一个manager对象。
Where does it fall short? If Manager inherits from Person, then the Msgbox
should show the Phone number.
It falls short when Polymorphically using a manager as a Person. Change the managers phone number
using the design he has and it will not be reflected in the *Base* person and vice versa because
essentially
all he has done is create a shallow copy.
If it fails on Persons.Find() it may be because that function returns a Person
object instead of a manager object.




它失败了,因为您无法使用扩展转换将Person转换为Manager。

Person类没有Manager类的概念,因为它是子人。


有时候作者等会谈论现实世界对象之后的OOP。看起来好像是一个自然的

级别的heirarchy让经理从Person下降,但有时这只是一个真实的

世界

幻觉混淆了设计概念。有时使用简单的

属性/属性可以更好地制作。


人与经理之间存在哪些行为差异?

Richard



It fails because you cant use a widening conversion to convert a Person to a Manager.
A Person class has no concept of a Manager class because it is sub-Person.

Sometimes an author etc will talk about the OOP following real world objects. It might seem like a
natural
class heirarchy to have Manager descend from Person but sometimes this just turns out to be a real
world
illusion tht confuses the design concept. Sometime the distinct is better made with a simple
attribute/property.

What behavioural differences are there between Person and Manager?

Richard


这篇关于继承设计问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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