python中的嵌套类(内部类调用外部类中的方法函数)
2015-01-03 22:53
501 查看
在为书中版本是3.X的,但2.X不太支持直接调用。
所以,在PYTHON2.X中,要在内部类中调用外部类的方法,就必须得实例化外部类,然后,传入实例进行调用。
花了我两个小时啊,资料没找到,自己一个一个想法调试,真的失败了三四十次,PRINT了N多~~~:)
关键代码如下:
self.superclass = DiagramFactory()
self.width = width
self.height = height
self.diagram = DiagramFactory._create_rectangle(self.superclass,self.width, self.height, DiagramFactory.BLANK)
。。。。
def _create_rectangle(self, width, height, fill):
。。。。。
所以,在PYTHON2.X中,要在内部类中调用外部类的方法,就必须得实例化外部类,然后,传入实例进行调用。
花了我两个小时啊,资料没找到,自己一个一个想法调试,真的失败了三四十次,PRINT了N多~~~:)
class DiagramFactory: @classmethod def make_diagram(Class, width, height): return Class.Diagram(width, height) @classmethod def make_rectangle(Class, x, y, width, height, fill="white", stroke="black"): return Class.Rectangle(x, y, width, height, fill, stroke) @classmethod def make_text(Class, x, y, text, fontsize=12): return Class.Text(x, y, text, fontsize) BLANK = " " CORNER = "+" HORIZONTAL = "-" VERTICAL = "|" class Diagram: def __init__(self, width, height): self.superclass = DiagramFactory() self.width = width self.height = height self.diagram = DiagramFactory._create_rectangle(self.superclass,self.width, self.height, DiagramFactory.BLANK) def add(self, component): for y, row in enumerate(component.rows): for x, char in enumerate(row): self.diagram[y + component.y][x + component.x] = char def save(self, filenameOrFile): file = (None if isinstance(filenameOrFile, str) else filenameOrFile) try: if file is None: file = open(filenameOrFile, "w") for row in self.diagram: print >>file, "".join(row) finally: if isinstance(filenameOrFile, str) and file is not None: file.close() class Rectangle: def __init__(self, x, y, width, height, fill, stroke): self.superclass = DiagramFactory() self.x = x self.y = y self.rows = DiagramFactory._create_rectangle(self.superclass, width, height, DiagramFactory.BLANK if fill == "white" else "%") class Text: def __init__(self, x, y, text, fontsize): self.x = x self.y = y self.rows = [list(text)] def _create_rectangle(self, width, height, fill): rows = [[fill for _ in range(width)] for _ in range(height)] for x in range(1, width - 1): rows[0][x] = DiagramFactory.HORIZONTAL rows[height - 1][x] = DiagramFactory.HORIZONTAL for y in range(1, height - 1): rows[y][0] = DiagramFactory.VERTICAL rows[y][width - 1] = DiagramFactory.VERTICAL for y, x in ((0, 0), (0, width - 1), (height - 1, 0), (height - 1, width -1)): rows[y][x] = DiagramFactory.CORNER return rows
关键代码如下:
self.superclass = DiagramFactory()
self.width = width
self.height = height
self.diagram = DiagramFactory._create_rectangle(self.superclass,self.width, self.height, DiagramFactory.BLANK)
。。。。
def _create_rectangle(self, width, height, fill):
。。。。。
相关文章推荐
- Python内部类,内部类调用外部类属性,方法
- 内部类的方法(外部调用内部类的方法)
- 内部类的方法(外部调用内部类的方法)
- Python中实现结构相似的函数调用方法
- 在Angular外部使用js调用Angular控制器中提供的函数方法或变量
- python文件中的__name__=='__main__'的使用及调用其他py文件中的函数方法
- 类的外部调用私有函数的方法
- c++中调用python脚本提示 error LNK2001: 无法解析的外部符号 __imp_Py_Initialize等错误的解决方法
- python中的函数调用绑定,静态方法和类方法
- 内部类的方法(外部调用内部类的方法)
- python嵌套函数使用外部函数变量的方法(Python2和Python3)
- Python调用外部程序的四种方法
- 根据字符串名称动态调用Python的函数和对象方法
- VB动态调用外部函数的方法
- VB动态调用外部函数的方法
- 黑马程序员-主函数中调用内部类方法的问题
- VB动态调用外部函数的方法
- Python私有方法不能外部调用怎么理解?
- Java 内部类调用外部类方法的一个注意点
- python调用C动态库导出函数的返回值为指针类型时,在64位python环境下被截断解决方法