类的综合性代码例子

class Human1:
    def __init__(self, n, a):
        self.name = n
        self.age = a
        self.money = 0
        self.skill = []
    
    def teach(self, other, skill):                        #这里的other是指另一个人
        print(self.name, "",other.name, "", skill)
        other.skill.append(skill)
    
    def work(self, m):
        print(self.name, "上班赚了", m , "")
        self.money += m

    def borrow(self, other, m):
        if other.money > m:
            print(self.name, "", other.name, "", m)
            self.money += m
            other.money -= m
        else:
            print(other.name, "没有借钱给", self.name)

    def show_info(self):
        print(self.age,"岁 的", self.name, "有钱", self.money, "元,它学会了", " `".join(self.skill))
        

zhang3 = Human1("张三", 35)
li4 = Human1("李四", 8)
#张三教李四学python
zhang3.teach(li4, "python")
#李四教张三学王者荣耀
li4.teach(zhang3, "王者荣耀")
#张三上班赚了1000元
zhang3.work(1000)
#李四向张三借200元
li4.borrow(zhang3, 200)
zhang3.show_info()
li4.show_info()


输出结果:
D:\program\pycharm\student_init\venv\Scripts\python.exe D:/program/pycharm/exercise823.py
张三 教 李四 学 python
李四 教 张三 学 王者荣耀
张三 上班赚了 1000 元
李四 向 张三 借 200
35 岁 的 张三 有钱 800 元,它学会了 王者荣耀
8 岁 的 李四 有钱 200 元,它学会了 python

 

posted on 2018-08-23 23:13  zengsf  阅读(111)  评论(0编辑  收藏  举报

导航