zoukankan      html  css  js  c++  java
  • 面向对象和类

    面向对象

    一、.面向对象和面向过程优缺点

    面向过程 面向对象
    优点 复杂问题流程化,进而简单化 可扩展性高
    缺点 可扩展性差 编写复杂

    二、类与对象

    2.1定义类(类名建议用驼峰命名)

    ​ 一系列共同的属性和方法。 dg: 人类

    class 关键字 类名:
        pass
    
    class Student:
        school='oldboy'    #变量表示属性
        def choose(self):       # 函数表示方法
            print("选课....")
        def study(self):
            print('学习')
    

    2.2 定义对象

    对象:属性和方法的结合体

    现实生活中:先有对象再有类程序中:先有类再有对象

    2.3生成一个对象

    # 类加括号,生成对象
    stu1=Student()
    
    • python为类内置的特殊属性

    类名.name# 类的名字(字符串)
    类名.doc# 类的文档字符串
    类名.base# 类的第一个父类(在讲继承时会讲)
    类名.bases# 类所有父类构成的元组(在讲继承时会讲)
    类名.dict# 类的字典属性
    类名.module# 类定义所在的模块
    类名.class# 实例对应的类(仅新式类中)

    2.3#获取本身属性和方法 通过 . 获取

    class Student:
        #变量表示属性
        school='oldboy'
        # 函数表示方法
        def choose(self):
            print("选课....")
        def study(self):
            print('学习')
    stu1=Student()
    #获取属性和方法  通过 . 获取
    print(stu1.school)   #点出来他的属性,直接用到
    print(stu1.choose)   #点出来它的方法,而方法又是一个函数,所以当它不加括号时是一个函数名的内存地址
    print(stu1.choose())
    

    oldboy
    <bound method Student.choose of <main.Student object at 0x00000293B30FDE10>>
    选课....

    2.3获取类的属性和方法

    类.__dict__
    
    # 接上面代码打印
    print(Student.__dict__)
    

    {'module': 'main', 'school': 'oldboy', 'choose': <function Student.choose at 0x00000192A8300D08>, 'study': <function Student.study at 0x00000192A8300D90>, 'dict': <attribute 'dict' of 'Student' objects>, 'weakref': <attribute 'weakref' of 'Student' objects>, 'doc': None}

    2.4 类来调用属性和方法——通过dict来调用(复杂一般不用)

    print(Student.__dict__)    #获取到的是一个字典
    print(Student.__dict__['school'])  #获取到的是学校这个属性
    print(Student.__dict__['choose'])   # 获取到的是一个函数地址,因为没有传参
    Student.__dict__['choose'](123)   #调用了choose函数 ,后面必须要有参数。
    

    1.{'module': 'main', 'school': 'oldboy', 'choose': <function Student.choose at 0x0000017A47001620>, 'sutdy': <function Student.sutdy at 0x0000017A4E213C80>, 'dict': <attribute 'dict' of 'Student' objects>, 'weakref': <attribute 'weakref' of 'Student' objects>, 'doc': None}
    2. oldboy
    3. <function Student.choose at 0x0000017A47001620>
    4. 选课....

    2.5这种显然是太烦了,我们还可以这样做(类名.属性/函数)

    print(Student.school)
    Student.choose(123)  #函数一定要传参
    

    oldboy
    选课....

    2.6 .1对象获取属性和方法

    对象.__dict__
    
    stu1=Student()  #产生对象
    print(stu1.__dict__)  #此处为空字典是因为他没有是有的属性,还没有定义属性,所以是空字典
    

    {}

    2.6.2对象调用属性和方法

    对象.属性/函数  #虽然字典不显示,但是可以点出来
    
    stu1=Student()
    print(stu1.school)
    stu2=Student()
    print(stu2.school)
    

    oldboy
    oldboy

    2.7对象自己的属性和方法

    当对象更改的是对象中没有的而类中的有的属性的话,会在对象自己的名称空间中创建一个

    stu1=Student()
    stu1.name='wangw'   #对象自己定义的属性,也就是对象赋属性
    stu1.school='xxxx'    #对象自己定义的属性,也就是对象赋属性
    print(stu1.name)
    print(stu1.school)
    print(stu1.__dict__)   #此时查看就不会为空字典
    

    wangw
    xxxx
    {'name': 'wangw', 'school': 'xxxx'}

    三、对象的绑定方法

    3.1 属性查找顺序

    先从对象自身找------>类中找----->报错

    3.2 对象中放属性

    • 方式一
    stu1=Student()
    stu1.name='wangwenbin' #此时在对象中已经添加好了
    
    • 方式二(通过__init__方法) # 原理是在类中修改添加,然后在产生对象
    • -在类中定义该方法,方法上写一些参数
      -在对象实例化产生对象时,在括号中传的值,会被传到__init__中
    class Student:
        school='oldboy'
        def __init__(self,name):
            self.name=name
        def choose(self):
            print("选课....")
        def study(self):
            print('学习')
    #产生对象
    stu1=Student()
    stu1=Student('wangbinbin')
    #内部帮我们做了一些事:当我在实例化产生对象的时候,会自动调用__init__方法,完成对象的初始化
    print(stu1.name)
    stu2=Student('yang')
    print(stu2.name)
    

    wangbinbin
    yang

    3.3绑定方法

    ​ -定义在类内部的方法:

    ​ -类来调用:就是一个普通函数,有几个参数就需要传几个参数

    ​ -对象来调用:它叫对象的绑定方法,第一个参数不需要传,自动传递

    #定义类
    class Student:
        school = 'oldboy'  #变量表示属性
        # __init__ 看好名字,不是__int__
        def __init__(self,name):
            self.name=name
        def choose(self):
            print("这是一个选课的方法")
        def sutdy(self):
            print("这是一个学习的方法")
    
            
    stu1=Student('李铁蛋')
    stu1.study()
    

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

    3.4 一切皆对象

    python中,字典,列表字符串....都是对象

    类即类型 eg:字典类

    #类实例化产生对象
    ll=list([1,2,3])
    #对象调用对象的绑定方法,修改对象自己
    ll.append(5)
    print(ll)
    print(type(ll))
    

    [1, 2, 3, 5]
    <class 'list'>

    四、人狗大战小游戏

    #定义两个类,人和狗
    #人和狗的共同属性  1.姓名2.攻击力3.生命值
    #人和狗的方法     咬这个动作,可以定义为一个函数
    
    #定义一个狗类
    class Dog:
        type_dog = '藏獒'
        def __init__(self,name,atk,hp):
            self.name = name
            self.atk = atk
            self.hp = hp
    
        def fight(self,target):
            target.hp -= self.atk  #攻击目标的生命值等于攻击目标原本的生命值减去攻击者的攻击力
    
            print(f"{self.type_dog}狗{self.name},攻击了{target.name},掉血{self.atk},血量剩余为{target.hp}")
    
    #定义一个人类
    class Human:
        type_dog = '男'
    
        def __init__(self, name, atk, hp):
            self.name = name
            self.atk = atk
            self.hp = hp
    
        def fight(self, target):
            target.hp -= self.atk
    
            print(f"{self.type_dog}人{self.name},攻击了{target.name},掉血{self.atk},血量剩余为{target.hp}")
            
    #产生对象
    Hum1 = Human('ypp',50,100)
    Dog1 = Dog('毛毛',20,500)
    
    #对象调用方法
    Hum1.fight(Dog1)
    Dog1.fight(Hum1)
    Hum1.fight(Dog1)
    Dog1.fight(Hum1)
    ----------------------------------------------------------------------------------------
    男人ypp,攻击了毛毛,掉血50,血量剩余为450
    藏獒狗毛毛,攻击了ypp,掉血20,血量剩余为80
    男人ypp,攻击了毛毛,掉血50,血量剩余为400
    藏獒狗毛毛,攻击了ypp,掉血20,血量剩余为60
    
  • 相关阅读:
    Flutter 布局类组件:简介
    Flutter 基础组件:进度指示器
    Flutter 基础组件:单选框和复选框
    Flutter 基础组件:图片和Icon
    Flutter 基础组件:按钮
    Flutter 基础组件:文本、字体样式
    Flutter 基础组件:状态管理
    Flutter 基础组件:Widget简介
    网络编程之TCP
    入门多线程
  • 原文地址:https://www.cnblogs.com/wwbplus/p/11429912.html
Copyright © 2011-2022 走看看