Python 简单单继多继承尝试

单继承

class Point():
    def __init__(self, x, y):
        self.x = x
        self.y = y
        print('Point constructor')
    def ToString(self):
        return "{{X: {},Y: {}}}".format(self.x, self.y)

–注意format时 ‘{{‘打印’{‘,’}}’打印’}’

'{{ {} {} {} }}'.format(1,2,3)
'{ 1 2 3 }'
p1 = Point(0.0, 0.0)
p1.ToString()
Point constructor





'{X: 0.0,Y: 0.0}'
class Circle(Point):
    '''
    A circle is a point with radius.
    '''
    def __init__(self, x, y, radius):
        super().__init__(x, y)    # call father's constructor
        self.radius = radius
        print('Circle constructor')

    def ToString(self):
        return super().ToString() + '{{RADIUS: {}}}'.format(self.radius)
c1 = Circle(1,1,5)
Point constructor
Circle constructor
c1.ToString()
'{X: 1,Y: 1}{RADIUS: 5}'

多继承

class Size():
    def __init__(self, width, height):
        self.width = width
        self.height = height
        print("Size constructor")

    def ToString(self):
        return "{{WIDTH: {}, HEIGHT: {}}}".format(self.width, self.height)
class Rectangle(Point, Size):
    def __init__(self, x, y, width, height):
        # call ancestors' constructors
        Point.__init__(self, x, y)
        Size.__init__(self, width, height)
        print('Rectangle constructor')

    def ToString(self):
        return Point.ToString(self) + Size.ToString(self)
rect1 = Rectangle(5,5, 20, 30)
Point constructor
Size constructor
Rectangle constructor
rect1.ToString()
'{X: 5,Y: 5}{WIDTH: 20, HEIGHT: 30}'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值