我想找一个python替代这个位代码,它使用显式接口实现。我知道在python中没有什么接口可以做,但是用python编写这个界面的最好方法是什么呢?
public class SomeObject: Iinterface
{
int Iinterface.someProperty
{
get { return 2;}
}
}
发布于 2018-08-02 14:35:24
Python中没有接口:Python使用继承代替它们。你的例子可以被改写为:
class Interface(object):
@property
def some_property(self):
raise NotImplementedError()
class SomeClass(Interface):
@property
def some_property(self):
return 2
raise NotImplementedError()
并不是必须的,但是这是一种常见的方法来显示这个类是抽象的,你需要在使用它之前对它进行子类化并实现这个方法。
https://stackoverflow.com/questions/-100001874
复制相似问题