面向对象编程

面向对象编程:

优点:可扩展性高

缺点:编写复杂

学生1:
    学校:老男孩
    姓名:李铁蛋
    性别:女
    年龄:18
    方法:
        选课
        学习
学生1:
    学校:老男孩
    姓名:Nick
    性别:女
    年龄:48
    方法:
        选课
        学习

绑定方法:定义在类内部的方法:如果类来调用:就是一个普通函数,有几个参数就需要传几个参数对象来调用:它叫对象的绑定方法,第一个参数不需要传,自动传递

class Student:
    #变量表示属性
    school='oldboy'
    #__init__ 看好名字,不是__int__
    def __init__(x,name):
        x.name=name
    def choose(self):
        print("选课....")
    def study(self):
        print('%s学会了python'%self.name)

stu1=Student('nick')
stu1.study()
stu1=Student('李铁蛋')
stu1.study()



nick学会了python
李铁蛋学会了python

super()方法是严格按照mro()列表顺序寻找父类的

小游戏:

import time
class River:
    def __init__(self,name,atk,armor,hp,money,suck = 0):
        self.name = name
        self.atk = atk
        self.armor = armor
        self.hp = hp
        self.money = money
        self.suck = 0
    def attack(self,target):
        target.hp -= self.atk - target.armor
        self.hp += (self.atk - target.armor)*self.suck
        if (self.atk - target.armor)*self.suck == 0:
            print(f'{self.name}攻击了{target.name},{target.name}的生命值为{target.hp}')
        else:
            print(f'{self.name}攻击了{target.name},{target.name}的生命值为{target.hp}  {self.name}回了{(self.atk - target.armor)*self.suck}血')

class Gailun:
    def __init__(self,name,atk,armor,hp,money,suck = 0):
        self.name = name
        self.atk = atk
        self.armor = armor
        self.hp = hp
        self.money = money
        self.suck = 0
    def attack(self,target):
        target.hp -= self.atk - target.armor
        self.hp += (self.atk - target.armor) * self.suck
        if (self.atk - target.armor) * self.suck == 0:
            print(f'{self.name}攻击了{target.name},{target.name}的生命值为{target.hp}')
        else:
            print(
                f'{self.name}攻击了{target.name},{target.name}的生命值为{target.hp}  {self.name}回了{(self.atk - target.armor)*self.suck}血')

class Duolan:
    def __init__(self,atk = 7,hp = 100,money = 475):
        self.atk = 7
        self.hp = 100
        self.money = 475
    def updata(self,target):
        target.atk += 7
        target.hp += 100
class Blood_jian:
    def __init__(self, atk=60, suck=0.3, money=3000):
        self.atk = atk
        self.suck = 0.3
        self.money = 3000

    def updata(self, target):
        target.atk += 60
        target.suck += self.suck


blood_jian = Blood_jian()
river = River('river',54,12,414,3000)
gailun = Gailun('gailun',56,19,455,2500)
duolan = Duolan()

i = 0
while True:
    print(f'回合{i}')
    if river.money >= blood_jian.money:
        river.money -= blood_jian.money
        blood_jian.updata(river)
        print('river购买了饮血剑(攻击力+60,吸血+30%)')
    if gailun.money >= blood_jian.money:
        gailun.money -= blood_jian.money
        blood_jian.updata(gailun)
        print('gailun购买了饮血剑(攻击力+60,吸血+30%)')
    if river.money >= duolan.money:
        river.money -= duolan.money
        duolan.updata(river)
        print('river购买了多兰剑(攻击力+7,生命值+100)')
    if gailun.money >= duolan.money:
        gailun.money -= duolan.money
        duolan.updata(gailun)
        print('gailun购买了多兰剑(攻击力+7,生命值+100)')
    river.attack(gailun)
    gailun.attack(river)
    time.sleep(1)
    river.money += 20
    gailun.money +=20
    if river.hp<= 0 or gailun.hp<=0:
        break
    i = i +1
if river.hp > 0:
    print('river胜利')
else:
    print('gailun胜利')
    
    
#### 赋予瑞文饮血剑和5把多兰剑的德玛对A    
   
   
   C:UsersAdministratorAppDataLocalProgramsPythonPython36python.exe D:/python/8.23面向对象/test.py
回合0
river购买了饮血剑(攻击力+60,吸血+30%)
gailun购买了多兰剑(攻击力+7,生命值+100)
river攻击了gailun,gailun的生命值为460  river回了28.5血
gailun攻击了river,river的生命值为391.5
回合1
gailun购买了多兰剑(攻击力+7,生命值+100)
river攻击了gailun,gailun的生命值为465  river回了28.5血
gailun攻击了river,river的生命值为362.0
回合2
gailun购买了多兰剑(攻击力+7,生命值+100)
river攻击了gailun,gailun的生命值为470  river回了28.5血
gailun攻击了river,river的生命值为325.5
回合3
gailun购买了多兰剑(攻击力+7,生命值+100)
river攻击了gailun,gailun的生命值为475  river回了28.5血
gailun攻击了river,river的生命值为282.0
回合4
gailun购买了多兰剑(攻击力+7,生命值+100)
river攻击了gailun,gailun的生命值为480  river回了28.5血
gailun攻击了river,river的生命值为231.5
回合5
river攻击了gailun,gailun的生命值为385  river回了28.5血
gailun攻击了river,river的生命值为181.0
回合6
river攻击了gailun,gailun的生命值为290  river回了28.5血
gailun攻击了river,river的生命值为130.5
回合7
river攻击了gailun,gailun的生命值为195  river回了28.5血
gailun攻击了river,river的生命值为80.0
回合8
river攻击了gailun,gailun的生命值为100  river回了28.5血
gailun攻击了river,river的生命值为29.5
回合9
river攻击了gailun,gailun的生命值为5  river回了28.5血
gailun攻击了river,river的生命值为-21.0
gailun胜利

Process finished with exit code 0

原文地址:https://www.cnblogs.com/oxtime/p/11418744.html