zoukankan      html  css  js  c++  java
  • day_18(类初识 继承)

    面向对象编程

    对象是什么
        对象是数据与功能的容器(集合).是一系列存在相关联系的数据与功能的集合.
    面向过程
      设计一系列流水线机械式的思维方式
      优点: 将复杂的过程流程化,
      缺点: 拓展性差.一套流程只能用于一个场景(常用于不需要拓展维护的场景)

    面向对象
      建立一个个对象,通过对象之间的互动完成程序运算
    缺点: 1 编程的复杂度远远大于面向过程编程.不了解面向对象而立即上手极其容易出现设计过度的问题,
          2 无法像面向过程一样设计流水线式的精准的预测处理流程与结果,面向对象的程序一旦开始就由对象之间交互解决问题.
    优点: 解决了程序的可拓展性,对某一个对象的单独修改会反映到整个体系当中,
    应用场景: 需求经常变化的软件.解决复杂问题.

    # 类 == 具有相同特征的对象的概念(概括)
    # 编程中现有类 ==>在实例化出对象
    # 物理结构 类 == 数据属性 + 函数属性
    # 功能组成 类 == 不同对象由同一个类实例化而成() ==> 一系列拥有相同属性的对象(数据+函数)的集合
    #
    # 创建
    class Teacher:
        school = 'oldboy'
        def __init__(self,name,age,sex):
            self.name = name
            self.age = age
            self.sex = sex
        def teach(self):
            print('{} is teaching'.format(self.name))
    tea_1 = Teacher('egon',18,'male')
    tea_2 = Teacher('zhao',24,'female')
    tea_1.teach()
    tea_2.teach()
    Teacher.teach(tea_2)
    Teacher.teach(tea_1)
    
    # __init__()
    # 初始化
    # 当实例化时自动调用__init__方法
    # 当与每个实例相关的函数或者数据变量都要__init__中定义或操作
    # __init__没有返回值.因为每次实例化(调用__init__)都会返回一个对象.
    # 在__init__方法中可以操作类中的方法  通过类名.变量名
    #
    class Teacher:
        '''老师类连续'''
        school = 'oldboy'
        count = 0
        def __init__(self,name,age,sex):
            self.name = name
            self.age = age
            self.sex = sex
            Teacher.count += 1
        def teach(self):
            print('taeching')
        def lazy(self):
            print('lazy')
        def inde(self):
            print(Teacher.count)
    ter1 = Teacher('egon',28,'')
    ter1.inde()
    ter2 = Teacher('alex',28,'')
    ter2.inde()
    # 延伸思考
    # 在对象中调用函数可以通过类名加变量名来进行操作
    
    #对象的查找
    # 先找自己再找类再找父类
    # 类中的函数对于类来说叫做函数 == 通过函数形式调用 ; 而对于对象而言叫做绑定方法 == 通过方法的形式调用
    ter1 = Teacher('egon',28,'')
    ter1.inde()
    ter2 = Teacher('alex',28,'')
    ter2.inde()
    print(ter1.school)
    # 类的内置方法
    Teacher.__bases__
    Teacher.__base__
    Teacher.__dict__
    Teacher.__name__
    Teacher.__basicsize__
    """总结练习"""
    class Hero:
        def __init__(self,name,hp,atk,attack_speed):
            self.name =name
            self.hp = hp
            self.atk = atk
            # self.armo = armor
            # self.spell_resistance = spell_resistance
            self.attack_speed = attack_speed
        def attack(self,other):
            print('{} attack {}'.format(self.name,other.name))
            other.hp -= self.atk
    
    class Mondor(Hero):
        def skill_q(self,other):
            print('{} attack {} use skill_q'.format(self.name,other.name))
            other.hp -= self.hp*0.8
        def skill_r(self):
            print('{} use skill_r'.format(self.name))
            self.hp += self.hp*0.3
    class Raven(Hero):
        def skill_q(self,other):
            print('{} attack {} use skill_q'.format(self.name,other.name))
            other.hp -= self.atk*0.8
            other.hp -= self.atk*0.8
            other.hp -= self.atk*0.7
        def skill_r(self):
            print('{} use skill_r'.format(self.name))
            self.atk *= 2
    mon_1 = Mondor('llk',1000,158,0.89)
    
    raven = Raven('kkl',1000,158,0.89)
    mon_1.attack(raven)
    print(raven.hp)
    
    print(Raven.__bases__)
    print(Raven.__base__)
    print(Raven.__dict__)
    print(Raven.__name__)
    print(Raven.__basicsize__)

    类的继承

    # 继承
    # 什么是继承.
    # 通过拓展已存在的类来建立新的类.==已存在的类(父类,基类,超类)  拓展(继承 修改) 新的类(子类)
    # 为什么要有继承
    # 加强程序的可拓展性   减少代码量
    # 怎么继承
    class Hero:
        def __init__(self,name,hp,atk,attack_speed):
            self.name =name
            self.hp = hp
            self.atk = atk
            # self.armo = armor
            # self.spell_resistance = spell_resistance
            self.attack_speed = attack_speed
        def attack(self,other):
            print('{} attack {}'.format(self.name,other.name))
            other.hp -= self.atk
    
    class Mondor(Hero):
        def skill_q(self,other):
            print('{} attack {} use skill_q'.format(self.name,other.name))
            other.hp -= self.hp*0.8
        def skill_r(self):
            print('{} use skill_r'.format(self.name))
            self.hp += self.hp*0.3
    class Raven(Hero):
        def skill_q(self,other):
            print('{} attack {} use skill_q'.format(self.name,other.name))
            other.hp -= self.atk*0.8
            other.hp -= self.atk*0.8
            other.hp -= self.atk*0.7
        def skill_r(self):
            print('{} use skill_r'.format(self.name))
            self.atk *= 2
    # 经典类 新式类
    # 经典类 : 在python2 中没有显式的继承Objict类或者其子类的类叫做 经典类
    # 新式类 : 继承Objict类或者其子类的类叫做 新式类 在python3 中都是新式类
    # 继承中的调用顺序 自己 ==> 父类
    # 子类重调父类方法
    # 一(直接通过调用父类的函数的形式来在子类调用父类方法)
    #
    class Hero:
        def __init__(self,name,hp,atk,attack_speed):
            self.name =name
            self.hp = hp
            self.atk = atk
            # self.armo = armor
            # self.spell_resistance = spell_resistance
            self.attack_speed = attack_speed
        def attack(self,other):
            print('{} attack {}'.format(self.name,other.name))
            other.hp -= self.atk
    class Xerath(Hero):
        def __init__(self,name,hp,atk,attack_speed,mp):
            Hero.__init__(self,name,hp,atk,attack_speed)
            self.mp = mp
        def attack(self,other):
            self.mp += 12
            Hero.attack(self,other)
        def skill_q(self,other):
            print('{} attack {} use skill_q'.format(self.name, other.name))
            other.hp -= self.atk * 1.6
            self.mp -= 80
    llk = Xerath('llk',1000,158,0.89,700)
    xxx = Xerath('XXX',1000,158,0.89,700)
    llk.attack(xxx)
    print(llk.mp)
    print(xxx.hp)
  • 相关阅读:
    CentOS7上安装Pycharm
    一个苹果证书怎么多次使用(授权Mac开发)——导出p12文件
    MAC上搭建Jenkins + Android + IOS自动开发部署环境
    Pivotal Cloud Foundry学习笔记(1)
    Ubuntu上部署tomcat后无法访问8080端口问题
    Ubuntu上部署Jenkins
    Ubuntu输入命令无效的问题
    python中发送post请求时,报错“Unrecognized token 'xxxx': was expecting ('true', 'false' or 'null')”
    pycharm的断点调试【转自https://blog.csdn.net/weixin_39198406/article/details/78873120】
    Python中的logging模块【转】https://www.cnblogs.com/yelin/p/6600325.html
  • 原文地址:https://www.cnblogs.com/lee1225/p/12643033.html
Copyright © 2011-2022 走看看