缺少Python中的接口...... [英] Missing interfaces in Python...

查看:74
本文介绍了缺少Python中的接口......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自Java背景,所以请不要惹我生气......


我看到Python缺少接口。接口的概念

是Java中良好的编程设计的关键,但我读过他们在Python中并不是真的需要
。我想知道我可以用什么技术在Python中使用
来获得与程序设计相同的好处,我会用Java接口来获得它们。


例如,如果我想要一个带有Car对象的程序,以及一个总线

对象。我希望这两个对象都提供一组可以由Mechanic对象使用的公共方法,但是可以由Driver对象使用的方法略有不同。

方法。 br />

在Java中,我将通过定义一个由Car和Bus对象实现的
的IFixable接口来实现这一目标。机械对象

可以与任何实现此接口的对象一起使用。


如何在Python中解决这个问题?我想我会使用一个

抽象类而不是IFixable的接口,因为Python

支持多重继承,但我不确定这是否正确。 />

感谢您的任何建议。


Scott Huey

I''m coming from a Java background, so please don''t stone me...

I see that Python is missing "interfaces". The concept of an interface
is a key to good programming design in Java, but I''ve read that they
aren''t really necessary in Python. I am wondering what technique I can
use in Python to get the same benefits to a program design that I would
get with interfaces in Java.

For example, if I want to have a program with a Car object, and a Bus
object. I want both of these objects to present a common group of
methods that can be used by Mechanic objects, but slightly different
methods that can be used by Driver objects.

In Java I would accomplish this by defining an IFixable interface that
would be implemented by both the Car and Bus objects. Mechanic objects
would work with any object implementing this interface.

How would I approach this problem in Python? I think I would use an
abstract class instead of an interface for IFixable, since Python
supports multiple inheritance, but I''m not sure this is correct.

Thanks for any suggestions.

Scott Huey

推荐答案

re *************** *@gmail.com 启发我们:
我看到Python缺少接口。


不,不是。它只是没有它们。

接口的概念是Java中良好编程设计的关键,但我读过它们并不是真的必要在Python中。


在Java中,我将通过定义一个由Car和Bus对象实现的IFixable接口来实现这一目标。机械师
对象可以与任何实现此接口的对象一起使用。
I see that Python is missing "interfaces".
No it isn''t. It just hasn''t got them.
The concept of an interface is a key to good programming design in
Java, but I''ve read that they aren''t really necessary in Python.
In Java I would accomplish this by defining an IFixable interface
that would be implemented by both the Car and Bus objects. Mechanic
objects would work with any object implementing this interface.




在Python中,您只需调用所需的函数即可。无需

制作严格定义的东西。


Sybren

-

问题与世界是愚蠢的。并不是说应该对愚蠢的死刑进行处罚,但为什么我们不要仅仅拿掉

安全标签来解决问题呢? br />
Frank Zappa



In Python, you would simply call the functions you need. No need to
make things that rigidly defined.

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don''t we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa


#在Python中,您只需调用所需的函数即可。不需要

#制作严格定义的东西。


除非你需要处理异常,否则这些方法不会是b $ b存在。我认为接口肯定是有用的。


-

Jonathan Daugherty
http://www.parsed.org
# In Python, you would simply call the functions you need. No need to
# make things that rigidly defined.

Except when you need to handle exceptions when those methods don''t
exist. I think interfaces can definitely be useful.

--
Jonathan Daugherty
http://www.parsed.org


Jonathan Daugherty写道_
Jonathan Daugherty wrote_
#在Python中,您只需调用所需的函数即可。无需#制作严格定义的东西。

除非您需要处理异常,否则这些方法不存在。我认为界面肯定是有用的。
# In Python, you would simply call the functions you need. No need to
# make things that rigidly defined.

Except when you need to handle exceptions when those methods don''t
exist. I think interfaces can definitely be useful.




所以有了接口,丢失的方法会突然出现空间?
air?


< / F>



so with interfaces, missing methods will suddenly appear out of thin
air ?

</F>


这篇关于缺少Python中的接口......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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