我想遵循良好的编程习惯,所以我有点卡在这个问题上:
假设我有类根,
Class root(Object):
def __init__(self):
self._root_tree = 'Base'
def __str__(self):
return self._root_tree
def _test(self):
return 'test'
假设我创建了一个名为 Oak 的类
Class Oak(root):
def __str__(self):
return 'Oak'
def _test(self):
return 'Oak_test'
def _new_fun(self):
return 'new_func_only_in_oak'
然后在 Class Cherry 中,我可以执行以下操作吗
Class Cherry(root):
def _grab_trees(self,another_tree): #another_tree is a Oak object
other_tree = another_tree.__str__() #this will return Oak
return 'The other three is: ' + other_tree
def _test2(self,another_tree):
return another_tree._test()
def _testing_new(self,another_tree):
return another_tree._new_fun()
基本上调用__str__()
_new_fun()
并_test()
在 Cherry 类中有效(良好做法)。