zoukankan      html  css  js  c++  java
  • 类的综合性代码例子

    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:programpycharmstudent_initvenvScriptspython.exe D:/program/pycharm/exercise823.py
    张三 教 李四 学 python
    李四 教 张三 学 王者荣耀
    张三 上班赚了 1000 元
    李四 向 张三 借 200
    35 岁 的 张三 有钱 800 元,它学会了 王者荣耀
    8 岁 的 李四 有钱 200 元,它学会了 python
  • 相关阅读:
    团购倒计时
    折半查找
    比较函数
    行为驱动开发: Cucumber的目录结构和执行过程 (转载)
    ruby 方法查找 and执行方法
    Rubyinstance_variable_get(@xx)
    Ruby 模块
    散列
    ruby webdriver 启动firefox driver时,加载firebug的扩展
    git使用简介
  • 原文地址:https://www.cnblogs.com/zengsf/p/9527200.html
Copyright © 2011-2022 走看看