• 面向对象之类的组合代码注释部分


    class Person:
    def __init__(self,name,age,sex):
    self.name = name
    self.age = age
    self.sex = sex
    def shangshan(self):
    print('%s,%s岁,%s,上山去砍柴'%(self.name,self.age,self.sex))
    def kaiche(self):
    print('%s,%s岁,%s,开车去东北'%(self.name,self.age,self.sex))
    def zuiai(self):
    print('%s,%s岁,%s,最爱大保健'%(self.name,self.age,self.sex))
    ming = Person('小明','10','男')
    ming.shangshan()
    ming.kaiche()
    ming.zuiai() #通过类造了三个行为模子,只要下面实例化一个人就可以让一个别的人来进行三个行为

    #圆的周长和面积
    from math import pi
    class Circle:
    def __init__(self,r):
    self.r = r
    def area(self):
    return '面积是%s'%(pi*(self.r**2))
    def perimeter(self):
    return '周长是%s'%(2*pi*self.r)
    c1 = Circle(3)
    print(c1.area())
    print(c1.perimeter())

    #正方形的面积
    class Square:
    def __init__(self,length):
    self.length = length
    def zhouchang(self):
    return '周长是%s'%(4*self.length)
    def mianji(self):
    return '面积是%s'%(self.length**2)
    s = Square(3)
    print(s.mianji())
    print(s.zhouchang())

    #每实例化一个对象就计数一次
    # class lei:
    # count = 0
    # def __init__(self):
    # lei.count+=1
    # f1=lei()
    # f2=lei()
    # print(f1.count)


    #通过组合来完成功能
    # from math import pi
    #圆形类
    # class Circle:
    # def __init__(self,r):
    # self.r = r
    # def area(self):
    # return self.r**2 * pi
    # def perimeter(self):
    # return 2*pi*self.r
    #圆环类
    # class Ring:
    # def __init__(self,outside_r,inside_r):
    # self.outside_c = Circle(outside_r)
    # self.inside_c = Circle(inside_r)
    # def area(self):
    # return self.outside_c.area() - self.inside_c.area()
    # def perimeter(self):
    # return self.outside_c.perimeter()+self.inside_c.perimeter()
    # ring = Ring(20,10)
    # print(ring.area())
    # print(ring.perimeter())


    # 创建一个老师类
    # 老师有生日
    # 生日也可以是一个类
    # 组合
    #生日类
    # class Birthday:
    # def __init__(self,year,month,day):
    # self.year = year
    # self.month = month
    # self.day = day
    #生日时间类
    # class Course:
    # def __init__(self,course_name,period,price):
    # self.name = course_name
    # self.period = period
    # self.price = price
    # 老师类
    # class Teacher:
    # def __init__(self,name,age,sex,birthday):
    # self.name = name
    # self.age = age
    # self.sex = sex
    # self.birthday =birthday
    # self.course = Course('python','6 month',2000)
    # b = Birthday(2018,1,16)
    # egg = Teacher('egon',0,'女',b)
    # print(egg.name)
    # print(egg.birthday.year)
    # print(egg.birthday.month)
    # print(egg.course.price)


    # 面向对象的三大特性 : 继承 多态 封装
    # 组合
    # 人狗大战
    # class Dog:
    # def __init__(self,name,aggr,hp,kind): #创建一个狗类
    # self.name = name
    # self.aggr = aggr
    # self.hp = hp
    # self.kind = kind
    # def bite(self,person):
    # person.hp -= self.aggr
    # class Person:
    # def __init__(self,name,aggr,hp,sex): #一个人类
    # self.name = name
    # self.aggr = aggr
    # self.hp = hp
    # self.sex = sex
    # self.money = 0
    # def attack(self,dog):
    # dog.hp -= self.aggr
    # def get_weapon(self,weapon):
    # if self.money >= weapon.price:
    # self.money -= weapon.price
    # self.weapon = weapon
    # self.aggr += weapon.aggr
    # else:
    # print("余额不足,请先充值")
    # class Weapon:
    # def __init__(self,name,aggr,njd,price): #一个武器类
    # self.name = name
    # self.aggr = aggr
    # self.njd = njd
    # self.price = price
    # def hand18(self,person): #武器自带的技能,降龙十八掌
    # if self.njd > 0:
    # person.hp -= self.aggr * 2
    # self.njd -= 1
    # gelan = Person('gelan',0.5,100,'不详')
    # tai = Dog('泰日天',100,500,'teddy')
    # w = Weapon('打狗棒',100,3,998)
    # # gelan装备打狗棒
    # gelan.money += 1000
    # gelan.get_weapon(w)
    # print(gelan.weapon)
    # print(gelan.aggr)
    # gelan.attack(tai)
    # print(tai.hp)
    # gelan.weapon.hand18(tai)
    # print(tai.hp)

    # 组合 :一个对象的属性值是另外一个类的对象
    # gelan.weapon 是 Weapon类的对象
  • 相关阅读:
    SVN cleanup操作反复失败解决办法
    mysql常用命令之-用户密码修改
    properties 配置文件中值换行的问题
    在每一行行尾添加内容
    Java 毫秒转换为日期类型、日期转换为毫秒
    SimpleDateFormat 12小时制以及24小时制的写法
    java校验时间格式 HH:MM
    ClassLoader 详解及用途(写的不错)
    ObjectInputStream类和ObjectInputStream类的使用
    logback 详解
  • 原文地址:https://www.cnblogs.com/ddjl/p/8329400.html
Copyright © 2020-2023  润新知