首页 > 其他 > 详细

【03】类——拾遗

时间:2021-05-23 22:57:34      阅读:15      评论:0      收藏:0      [点我收藏+]

类的组合

比如有两个类A,类B,用类C把类A和类B组合起来,使得C有A和B的属性方法。

class Turtle:
    def __init__(self,x):
        self.num = x  

class Fish:
    def __init__(self,y):
        self.num = y  

class Pool:
    def __init__(self,x,y):
        self.turtle = Turtle(x)
        self.fish = Fish(y)
    def print_num(self):
        print("水池里有乌龟 %d 只,鱼 %d 只!"%(self.turtle.num,self.fish.num))  

pool = Pool(1,10)
pool.print_num()  

组合结果
水池里有乌龟 1 只,鱼 10 只!

【03】类——拾遗

原文:https://www.cnblogs.com/ua-21/p/14801674.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!