ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

day23_python

2021-08-23 02:00:07  阅读:216  来源: 互联网

标签:__ name python money self day23 print def


参考Eva_J的博客,原文连接:https://www.cnblogs.com/Eva-J/p/7277026.html

命名空间

from math import pi


class Course:
    language = 'Chinese '

    def __init__(self, teacher, course, period, price) -> None:
        self.teacher = teacher
        self.name = course
        self.period = period
        self.price = price


Course.language = 'English'
# Course.__dict__['language']='Chinese'  # 会报错,无法修改,只能查看
print(Course.language)
python = Course('egon', 'math', '6 months', '20000')
# 类中的静态对象,可以被类和对象调用,类对象指针
# 对于不可变的数据库类型来说,类变量最好用类名操作
# 对于可变数据类型来说,修改是共享的,重新赋值独立的
# 模拟人生

类中变量值的修改

class Person():
    money = 0

    def work(self):
        self.money += 1000  # Person.money +=1000


mother = Person()
father = Person()
mother.money += 1000
father.money += 1000
print(Person.money)
# 上述例子表示mother和father的money都在自己的空间中,并未改变Person的值
# 修改
Person.money += 1000
Person.money += 1000
print(Person.money)
mother.work()
father.work()

每实例化一个对象就记录下来,最终所有的对象共享这个数据

class Foo:
    count = 0

    def __init__(self) -> None:
        Foo.count += 1


f1 = Foo()
f2 = Foo()
print(f1.count)
f3 = Foo()
print(f1.count)
# 对象调用方法会有绑定方法的概念
# 类里面没有的变量无法找到全局的变量

面向对象的三大特征:继承 多态 组合

组合

例1 装备武器
class Dog:
    def __init__(self, name, blood, aggr, kind):
        self.name = name
        self.blood = blood
        self.aggr = aggr
        self.kind = kind

    def bite(self, person):
        person.blood -= self.aggr


class Person:
    def __init__(self, name, blood, aggr, sex):
        self.name = name
        self.blood = blood
        self.aggr = aggr
        self.sex = sex
        self.money = 0

    def attack(self, dog):
        dog.blood -= self.aggr

    def get_weapon(self, weapon):
        if self.money >= weapon.price:
            self.money = weapon.price
            self.weapon = weapon
            self.aagr += weapon
        else:
            print('余额不足')


class Weapon:
    def __init__(self, name, aggr, njd, price) -> None:
        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


alex = Person('alex', 0.5, 100, 'None')
jin = Dog('金老板', 100, 500, 'teddy')
w = Weapon('打狗棒', 100, 3, 998)
alex.money += 1000
alex.get_weapon(w)
print(alex.weapon)
print(alex.aggr)
alex.attack(jin)
print(jin.blood)
alex.weapon.hand18(jin)
print(jin.blood)
# 装备的伤害值、血量
# 组合,在一个对象的属性值是另外一个类的对象
# alex.weapon 是类weapon的对象
例2 圆环周长和面积
class Circle:
    def __init__(self, r) -> None:
        self.r = r

    def area(self):
        return pi*(self.r**2)

    def perimeter(self):
        return 2*pi*self.r


class Ring:
    def __init__(self, out_r, in_r) -> None:
        self.out_c = Circle(out_r)
        self.in_c = Circle(in_r)

    def area(self):
        return self.out_c.area()-self.in_c.area()

    def perimeter(self):
        return self.out_c.perimeter()+self.in_c.perimeter()


ring = Ring(20, 10)
print(ring.area())
print(ring.perimeter())
例3 老师生日
class Birthday:
    def __init__(self, year, month, day) -> None:
        self.year = year
        self.month = month
        self.day = day


class Teacher:
    def __init__(self, name, age, sex, birthday) -> None:
        self.name = name
        self.age = age
        self.sex = sex
        self.birthday = birthday
        self.course = Course(self, 'python', '6 month', 2000)  # 也是一种组合,不过不具备通用性


b = Birthday(2018, 1, 16)
egg = Teacher('egon', 0, '女', b)
print(egg.name)
print(egg.birthday.year)
print(egg.course.price)

标签:__,name,python,money,self,day23,print,def
来源: https://www.cnblogs.com/Come3/p/15174270.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有