在为书中版本是3.X的,但2.X不太支持直接调用。

所以,在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中的嵌套类(内部类调用外部类中的方法函数)的更多相关文章

  1. Python内部类,内部类调用外部类属性,方法

    一 Python中内部类 典型定义: class MyOuter: age=18 def __init__(self,name): self.name=name class MyInner: def ...

  2. Java嵌套类,内部类和外部类

    1.嵌套类,内部类 嵌套类是指被定义在一个类内部的类: JAVA的嵌套类有很多种类:1.静态成员类:2.非静态成员类:3.匿名类:4.局部类:其中,除了静态成员类之外,其他的都是内部类,因为静态成员类 ...

  3. “全栈2019”Java第八十二章:嵌套接口能否访问外部类中的成员?

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  4. Java调用外部类定义的方法(Static与无Static两种)

    首先定义方法 public class Dy { public int Add(int x,int y){ //定义Add(),该方法没有被static修饰 return x+y; } public ...

  5. 【转】C++之内部类(嵌套类)与外部类及友元

    [转]http://baike.baidu.com/link?url=Md223wQoT5s-3cZ5xRnj1pGmvm310DKAuh-HDrcEdc2l24rwobHrdEc_Mi4Z3BGP0 ...

  6. Java内部类引用外部类中的局部变量为何必须是final问题解析

    今天编写一个多线程程序,发现在方法内定义内部类时,如果内部类调用了方法中的变量,那么该变量必须申明为final类型,百思不得其解,后来想到应该是生命周期的原因,因为方法内定义的变量是局部变量,离开该方 ...

  7. C++之内部类(嵌套类)与外部类及友元

    本人能力.精力有限,所言所感都基于自身的实践和有限的阅读.查阅,如有错误,欢迎拍砖,敬请赐教——博客园:钱智慧. 先上代码: class Outer { public: Outer(){m_outer ...

  8. kotlin中的嵌套类与内部类

    Java中的内部类和静态内部类在Java中内部类简言之就是在一个类的内部定义的另一个类.当然在如果这个内部类被static修饰符修饰,那就是一个静态内部类.关于内部类 和静态内部类除了修饰符的区别之外 ...

  9. C++嵌套类(内部类与外部类)

    在一个类中定义的类被称为嵌套类,定义嵌套类的类被称为外部类.; //不能访问 mytest::i = 10;//不能访问 } private: class mytest { int i; int j; ...

随机推荐

  1. iOS 定位服务、通讯录、日历、提醒事项、照片、蓝牙共享、麦克风、相机等授权检测

    金田 iOS系统版本的不断升级的前提,伴随着用户使用设备的安全性提升,iOS系统对于App需要使用的硬件限制也越来越严格,App处理稍有不妥,轻则造成功能不可用用户还不知道,重则会造成App Cras ...

  2. 【转】Android UI系列-----时间、日期、Toasts和进度条Dialog

    原文网址:http://www.cnblogs.com/xiaoluo501395377/p/3421727.html 您可以通过点击 右下角 的按钮 来对文章内容作出评价, 也可以通过左下方的 关注 ...

  3. linux patch

    作为程序员,了解diff&patch命 令是非常必要的.比如说我们发现某个项目有bug代码,而自己又没有svn的提交权限,那么此时最合适的解决方法就是用diff命令做一个补丁发给项目成 员.项 ...

  4. 设置MyEclipse的右击新建后面的选项

    转自:http://blog.sina.com.cn/s/blog_929b09ac010197g6.html,做了修改 最近发现在MyEclipse右击后,新建后面的菜单选项太少了,新建一个文件,还 ...

  5. asp.net mvc cooike 购物车 如何实现

    先上代码: 1. ShoppingCartService 类 using System; using System.Collections.Generic; using System.Linq; us ...

  6. Redmine backlogs 升级

    刚装完1.0.3两天,1.0.4发布了,乘项目还没有开始,赶快升级.升级过程 1.设置环境变量: RAILS_ENV=production export RAILS_ENV 2. 获取最新代码: cd ...

  7. Qt 之容器内的控件全屏

    m_label = new QLabel(); ui->stackedWidget->addWidget(m_label); ui->stackedWidget->setCur ...

  8. linux/unix运行级别

    在SYSTEM V 风格的UNIX系统中,系统被分为不同的运行级别,这和BSD分支的UNIX有所不同,常用的为0~6七个级别:0关机 1单用户 2不带网络的多用户 3带网络的多用户 4保留,用户可以自 ...

  9. Android/Linux boot time优化

    基于analyze_boot.py分析Android/Linux的kernel boot时间 1.修改HiKey的BoardConfig.mk文件,使能initcall_debug,增加dmesg b ...

  10. (转)js 正则表达式之test函数讲解

    该方法的返回值是布尔值,通过该值可以匹配字符串中是否存在于正则表达式相匹配的结果,如果有匹配内容,返回ture,如果没有匹配内容返回false,该方法常用于判断用户输入数据的合法性,比如检验Email ...