python3 组合的个人理解

python的组合在我看来和函数的嵌套差不多,也不知道这样理解对还是不对,不对的希望各位大神帮我指正一下,谢谢了!

类的组合的主要作用是封装前一个类的作用,其他的和函数的套用差不多!我是按照我的例子来理解的!!!

class Wear:
    def __init__(self,hat,clothes,trousers,shos):
        self.hat=hat
        self.clothes=clothes
        self.trousers=trousers
        self.shos=shos

    def wear(self):
        return ("喜欢 白%s帽子 黑%s衣服 红%s裤子 蓝%s鞋子"%(self.hat,self.clothes,self.trousers,self.shos))

class person:
    def __init__(self,name):
        self.name=name
    def show(self,hat,clothes,trousers,shos):
        s1=Wear(hat,clothes,trousers,shos)#我不知道你的类(或是你的函数是怎么样的,我就只知道我在我的类里面调用你的方法来解决我的问题!)
        print(self.name,s1.wear())

a=person('小明')
a.show('平底锅','T恤','牛仔裤','绣花鞋')


猜你喜欢

转载自blog.51cto.com/853056088/2133803