界面,抽象类,两者,你推荐在这里使用? [英] Interface, abstract class, both, which do you recommend using here ?

查看:67
本文介绍了界面,抽象类,两者,你推荐在这里使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我会用一个例子对我的问题做一个简单的描述,

请不要笑。

我只是相信我将不得不使用接口或抽象

类,但我从来没有写过接口,虽然我知道基础




接口。


所以,让我们说我有一个班级RoboDriver,这是一个机器人,它是专门的b
任务是驾驶车辆。

所以,我将有一辆车辆。也是如此。


为了正确编程RoboDriver,我需要

车辆提供一些信息,比如车辆类型(车,< br $>
摩托车,


卡车),我也需要这些实例告诉我是否车辆是自动的或者是b $ b标准传输,如果是汽车或

卡车,


然后,询问齿轮数量。


如果我编写了一个IVehicle界面,我如何设法知道继承的

类类型?我的意思是,我不能列举所有类型的车辆,也不会确定我是否知道我是否发明了新型车型但是它们具有相似之处(和兼容性)b $ b b



我的RoboDriver类仍然能够知道如何处理它。


另外,我如何要求可选属性?我的意思是,让我们说我

将不得不询问某些车辆的传动类型

类型,


我没有找到像bool

IsManualTransmision这样的属性的优雅,因为,如果实现者硬编码

" true"返回,然后当我打开PutGear1时,我得到一个未实现的异常?


你明白了吗?哪种方式比较合适?

欢迎任何类型的评论。

解决方案

< blockquote> cr************@hotmail.com 写道在

新闻:11 ********************** @ f14g2000cwb.googlegr psps.com:



我将用一个例子来简单描述我的问题,请不要笑。
我只是相信我必须使用接口或
抽象类,但我还没有写过接口,虽然我知道接口的基础。




Craig,


我恐怕没有一个简单的答案给你。你似乎很清楚问题领域的问题。但我认为你还需要获得面向对象设计的深度知识。


< preaching_mode> :-)


任何非平凡的类层次结构的设计需要大量的b
。不仅因为涉及的复杂性,还因为你将不得不在未来版本的

你的应用程序中使用设计。一个好的设计使未来的代码库修改更容易,而一个糟糕的设计可能使它们变得不可能。


< / preaching_mode>


我建议你写一本关于设计基础知识的好书。我购买的非常高兴的是在
UML中的面向对象设计的基础。作者:Meilir Page-Jones:

http://www.amazon.com/exec/obidos/AS...110421120/sr=2

-1 / ref = pd_bbs_b_2_1 / 103- 3345454-2395801



http://tinyurl.com/64y6q


-

希望这会有所帮助。


Chris。

-------------

CR Timmons Consulting,Inc。
http://www.crtimmonsinc.com/


在你描述的情况,我只会让车辆成为

RoboDriver的财产。除非RoboDriver按照自己的定义,暗示了一个专门的车辆,你不需要一个接口或一个抽象类。


总线可能是使用抽象类Vehicle的候选者。公共汽车是一辆

的专用车辆。


如果车辆定义了固定的属性和程序,例如Drive,Brake,

转,所有车辆都会以同样的方式实施,并且专门的

车辆也不需要从另一个级别派生,然后使用静态的
级别。


如果你打算买一辆摩托车,它实际上转过来,而不是通过旋转车轮来转向
,那么车辆可能是

a是接口的良好候选者,因为你不会指定

转换中的步骤,或任何其他方法。您只需指定

车辆界面的用户必须指定它的转动方式。当然,你仍然可以使用

一个抽象类,摩托车可以覆盖RotateWheel()的基础Turn()

方法。并使用

LeanLeft()实现其Turn()版本;或者LeanRight();.


对于像Transmission这样的属性,创建一个包含选项的枚举,并设置

传输属性类型为TransmissionTypes枚举。 br />
HTH


DalePres

MCAD,MCDBA,MCSE


< cr * ***********@hotmail.com>在消息中写道

news:11 ********************** @ f14g2000cwb.googlegr oups.com ...



我将用一个例子对我的问题做一个简单的描述,
请不要笑。
我只是相信我必须使用接口或抽象类,但我还没有写过接口,虽然我知道

接口的基础。

所以,让我们说我有一个班级RoboDriver,这是一个专门负责驾驶车辆的机器人。
所以,我将有一个车辆。也是如此。

为了对RoboDriver进行正确的编程,我需要
车辆提供一些信息,比如车辆类型(汽车,摩托车,
卡车),我也需要这些实例能够告诉我车辆是自动还是标准传输,如果是汽车或
卡车,

如果我写一个IVehicle界面,我怎么设法知道继承的类类型?我的意思是,我不能列举所有类型的车辆,也不会知道我是否发明了新型车型但是有相似之处(兼容性)
我的RoboDriver级别仍然能够知道如何处理它。

另外,我如何要求可选属性?我的意思是,让我们说我只需要询问某些车辆类型的变速器类型,

我不觉得有类似属性的优雅bool
IsManualTransmision,因为,如果实现者硬编码
true,那该怎么办呢?返回,然后当我调用PutGear1时,我得到一个未实现的异常?

你明白了吗?哪种方式比较合适?
任何类型的评论都是受欢迎的。



我说要使用静态类车辆,但我的意思是一个抽象的课程...


DalePres


DalePres <别 - 叔 - 温泉 - 间 - 我@ LEA-VE-ME-A-孤 - COM>。在消息中写道

news:uk ************** @ TK2MSFTNGP12.phx.gbl ...

在你描述的情况下,我只是让车辆成为RoboDriver的财产。除非RoboDriver按照自己的定义去暗示一个专门的车辆,否则你不需要一个接口或一个抽象类。

总线可能是使用抽象类车辆的候选者。公交车是一种专用车辆。

如果车辆定义了固定的属性和程序,例如Drive,Brake,
Turn,所有车辆都将以相同的方式实施,并且专业化
车辆也不需要从另一个类派生,然后使用
静态类。

如果你要有一辆车摩托车,它实际上是由 Turn();或者LeanRight();

对于像Transmission这样的属性,用选项创建一个枚举,然后将Transmission属性类型设置为TransmissionTypes枚举。

HTH

DalePres
MCAD,MCDBA,MCSE

< cr ************ @ hotmail.com>在消息中写道
新闻:11 ********************** @ f14g2000cwb.googlegr oups.com ...



我将用一个例子对我的问题做一个简单的描述,
请不要笑。
我只是相信我必须使用接口或抽象类,但我还没有写过接口,虽然我知道

接口的基础。

所以,让我们说我有一个班级RoboDriver,这是一个专门负责驾驶车辆的机器人。
所以,我将有一个车辆。也是如此。

为了对RoboDriver进行正确的编程,我需要
车辆提供一些信息,比如车辆类型(汽车,摩托车,
卡车),我也需要这些实例能够告诉我车辆是自动还是标准传输,如果是汽车或
卡车,

如果我写一个IVehicle界面,我怎么设法知道继承的类类型?我的意思是,我不能列举所有类型的车辆,也不会知道我是否发明了新型车型但是有相似之处(兼容性)
我的RoboDriver级别仍然能够知道如何处理它。

另外,我如何要求可选属性?我的意思是,让我们说我只需要询问某些车辆类型的变速器类型,

我不觉得有类似属性的优雅bool
IsManualTransmision,因为,如果实现者硬编码
true,那该怎么办呢?返回,然后当我调用PutGear1时,我得到一个未实现的异常?

你明白了吗?哪种方式更合适呢?
欢迎任何类型的评论。




Hi,

I will use an example to do a simplified description of my problem,
please don''t laugh.
I just "believe" I would have to use either interface or abstract
classes, but I''ve not ever write an interface, though I know the basis
of

interfaces.

So, let''s say I have a class "RoboDriver", which is a robot which
dedicated task is to drive vehicles.
So, I will have an "Vehicle" class, too.

In order for do the proper programming of RoboDriver, I need that
vehicle provides some information, like type of vehicle (car,
motorcycle,

truck), also I would need the instances to be able to tell me whether
the vehicle is automatic or standard transmition, in case of car or
trucks,

then, to ask the number of gears.

If I write an IVehicle interface, how do I manage to know the inherited
classes type ? I mean, I can''t enumerate all types of vehicles, nor I
will

know if I new type is invented but due similarities (and compatibility)
my RoboDriver class will be still able to know how to handle it.

Also, how do I ask for the optional properties ? I mean, let''s say I
will have to ask about the transmission type only for certain vehicle
types,

I don''t find elegant to have properties like "bool
IsManualTransmision", because, what if the implementor hardcode a
"true" return and then when I

call PutGear1, I get a notimplemented exception ?

Do you get my idea ? Which is the more appropiate way to go with this ?
Any comments of any kind are welcome.

解决方案

cr************@hotmail.com wrote in
news:11**********************@f14g2000cwb.googlegr oups.com:

Hi,

I will use an example to do a simplified description of my
problem, please don''t laugh.
I just "believe" I would have to use either interface or
abstract classes, but I''ve not ever write an interface, though I
know the basis of interfaces.



Craig,

I''m afraid I don''t have a simple answer for you. You seem to know
the problem domain well. But I think you also need to gain an in-
depth knowledge of object-oriented design.

<preaching_mode> :-)

The design of any non-trivial class hierarchy takes a great deal of
thought. Not only because of the complexities involved, but also
because you will have to live with the design in future versions of
your application. A good design makes future codebase modifications
much easier, whereas a bad design can make them impossible.

</preaching_mode>

I suggest getting a good book on design fundamentals. One that I''m
very glad I purchased is "Fundamentals of Object-Oriented Design in
UML" by Meilir Page-Jones:

http://www.amazon.com/exec/obidos/AS...110421120/sr=2
-1/ref=pd_bbs_b_2_1/103-3345454-2395801

or

http://tinyurl.com/64y6q

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/


In the case you describe, I would just make Vehicle a property of
RoboDriver. Unless RoboDriver is going to, by its own definition, imply a
specialized vehicle, you would not want an interface or an abstract class.

Bus might be a candidate for using an abstract class Vehicle. Bus is a
specialized vehicle.

If vehicle defines fixed properties and procedures, such as Drive, Brake,
Turn, that all vehicles will implement in the same way, and specialized
vehicles do not need to derive from another class as well, then use a static
class.

If you are going to have a vehicle Motorcycle, which actually turns by
leaning into the turn rather than by rotating a wheel, then Vehicle might be
a good candidate for an Interface because you won''t specify the steps in
turning, or any other method. You will only specify that the user of your
Vehicle interface must specify how it turns. Of course, you could still use
an abstract class and the Motorcycle could just override the base Turn()
method that says RotateWheel(); and implement its version of Turn() with
LeanLeft(); or LeanRight();.

As for properties like Transmission, create a enum with the options and set
the Transmission property type to be TransmissionTypes enum.
HTH

DalePres
MCAD, MCDBA, MCSE

<cr************@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...

Hi,

I will use an example to do a simplified description of my problem,
please don''t laugh.
I just "believe" I would have to use either interface or abstract
classes, but I''ve not ever write an interface, though I know the basis
of

interfaces.

So, let''s say I have a class "RoboDriver", which is a robot which
dedicated task is to drive vehicles.
So, I will have an "Vehicle" class, too.

In order for do the proper programming of RoboDriver, I need that
vehicle provides some information, like type of vehicle (car,
motorcycle,

truck), also I would need the instances to be able to tell me whether
the vehicle is automatic or standard transmition, in case of car or
trucks,

then, to ask the number of gears.

If I write an IVehicle interface, how do I manage to know the inherited
classes type ? I mean, I can''t enumerate all types of vehicles, nor I
will

know if I new type is invented but due similarities (and compatibility)
my RoboDriver class will be still able to know how to handle it.

Also, how do I ask for the optional properties ? I mean, let''s say I
will have to ask about the transmission type only for certain vehicle
types,

I don''t find elegant to have properties like "bool
IsManualTransmision", because, what if the implementor hardcode a
"true" return and then when I

call PutGear1, I get a notimplemented exception ?

Do you get my idea ? Which is the more appropiate way to go with this ?
Any comments of any kind are welcome.



I said to use a static class for vehicle but I meant an abstract class...

DalePres

"DalePres" <don-t-spa-m-me@lea-ve-me-a-lone--.com> wrote in message
news:uk**************@TK2MSFTNGP12.phx.gbl...

In the case you describe, I would just make Vehicle a property of
RoboDriver. Unless RoboDriver is going to, by its own definition, imply a
specialized vehicle, you would not want an interface or an abstract class.

Bus might be a candidate for using an abstract class Vehicle. Bus is a
specialized vehicle.

If vehicle defines fixed properties and procedures, such as Drive, Brake,
Turn, that all vehicles will implement in the same way, and specialized
vehicles do not need to derive from another class as well, then use a
static class.

If you are going to have a vehicle Motorcycle, which actually turns by
leaning into the turn rather than by rotating a wheel, then Vehicle might
be a good candidate for an Interface because you won''t specify the steps
in turning, or any other method. You will only specify that the user of
your Vehicle interface must specify how it turns. Of course, you could
still use an abstract class and the Motorcycle could just override the
base Turn() method that says RotateWheel(); and implement its version of
Turn() with LeanLeft(); or LeanRight();.

As for properties like Transmission, create a enum with the options and
set the Transmission property type to be TransmissionTypes enum.
HTH

DalePres
MCAD, MCDBA, MCSE

<cr************@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...

Hi,

I will use an example to do a simplified description of my problem,
please don''t laugh.
I just "believe" I would have to use either interface or abstract
classes, but I''ve not ever write an interface, though I know the basis
of

interfaces.

So, let''s say I have a class "RoboDriver", which is a robot which
dedicated task is to drive vehicles.
So, I will have an "Vehicle" class, too.

In order for do the proper programming of RoboDriver, I need that
vehicle provides some information, like type of vehicle (car,
motorcycle,

truck), also I would need the instances to be able to tell me whether
the vehicle is automatic or standard transmition, in case of car or
trucks,

then, to ask the number of gears.

If I write an IVehicle interface, how do I manage to know the inherited
classes type ? I mean, I can''t enumerate all types of vehicles, nor I
will

know if I new type is invented but due similarities (and compatibility)
my RoboDriver class will be still able to know how to handle it.

Also, how do I ask for the optional properties ? I mean, let''s say I
will have to ask about the transmission type only for certain vehicle
types,

I don''t find elegant to have properties like "bool
IsManualTransmision", because, what if the implementor hardcode a
"true" return and then when I

call PutGear1, I get a notimplemented exception ?

Do you get my idea ? Which is the more appropiate way to go with this ?
Any comments of any kind are welcome.




这篇关于界面,抽象类,两者,你推荐在这里使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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