グラフを実装していて、Route クラスを Edge クラスのサブクラスにしました。Route クラスに「距離」機能を追加しようとしたため、Route クラスのコンストラクターが Edge クラスのコンストラクターをオーバーライドします。
それらは異なるモジュールにあるため、Route クラスで「from Edge import *」を使用しました。ただし、プログラム (PyDev) は引き続きエラー Undefined variable: Edge をスローします。
これが私の実装です:
'''
An (directed) edge holds the label of the node where the arrow is coming from
and the label of the node where the arrow is going to
'''
class Edge:
'''
Constructor of the Edge class
'''
def __init__(self, fromLabel, toLabel):
self.__fromLabel = fromLabel
self.__toLabel = toLabel
'''
Get the label of the node where the arrow is coming from
@return the label of the node where the arrow is coming from
'''
def getFromLabel(self):
return self.__fromLabel
'''
Get the label of the node where the arrow is going to
@return the label of the node where the arrow is going to
'''
def getToLabel(self):
return self.__toLabel
from Edge import *
'''
A Route is inherited from the Edge class and is an edge specialized for the
CSAirGraph class
'''
class Route(Edge):
'''
Constructor of the Route class
'''
def __init__(self, fromLabel, toLabel, distance):
Edge.__init__(self, fromLabel, toLabel)
self.__distance = distance
'''
Get the distance between two adjacent cities
@return: the distance between two adjacent cities
'''
def getDistance(self):
return self.__distance