我想为以下图形结构设置类:
这会导致循环导入,因为graph_data对象需要导入图形对象。有没有更好的方法来构建这个数据结构?显然这是一个非常淡化的例子,但我认为这是我的代码的关键问题。
以下是循环导入的示例:
#file main.py:
from graph import graph
myGraph = graph()
#file graph.py
import graph_node
class graph:
graph_nodes = []
def __init__(self, graph_nodes=None):
if graph_nodes == None:
self.graph_nodes = graph_nodes
else:
for i in range(5):
self.graph_nodes.append(graph_node.graph_node())
#file graph_node.py
import graph_node_data
class graph_node:
graph_data = None
def __init__(self):
self.graph_data = graph_node_data.graph_node_data()
#file graph_node_data.py
import graph
class graph_node_data:
value = None
def __init__(self):
self.value = graph.graph(graph_nodes = None)
这是一个图表,说明了这种结构可能有用的地方。
答案 0 :(得分:1)
只需将代码合并到一个文件中,运行正常:
int l = 6;
char[][] initialCharArray = new char[l][];
StringBuilder sb = new StringBuilder(l);
for (int i = 0; i < l; i++)
sb.Append('$');
char[] cArray = s.ToString().ToCharArray();
for (int x = 0; x < l; x++) initialCharArray[x] = cArray;
如果你真的需要将类保存在单独的文件中,则需要使用本地导入,即在函数中声明import语句,例如
class graph:
graph_nodes = []
def __init__(self, graph_nodes=None):
if graph_nodes == None:
self.graph_nodes = graph_nodes
else:
for i in range(5):
self.graph_nodes.append(graph_node.graph_node())
class graph_node:
graph_data = None
def __init__(self):
self.graph_data = graph_node_data.graph_node_data()
class graph_node_data:
value = None
def __init__(self):
self.value = graph.graph(graph_nodes = None)
myGraph = graph()