• 面向对象【林老师版】:属性查找(四)


     本节内容

    1、产生对象

    2、类中的数据属性

    3、类中的函数属性

    4、补充说明

    一、产生对象

    1、实现代码

    x='global'
    class LuffyStudent:
        school='luffycity'
    
        def __init__(self,name,sex,age):
            self.Name=name
            self.Sex=sex
            self.Age=age
    
        def learn(self,x):
            print('%s is learning %s' %(self.Name,x))
    
        def eat(self):
            print('%s is sleeping' %self.Name)
    
    stu1=LuffyStudent('王二丫','女',18)
    stu2=LuffyStudent('李三炮','男',38)
    stu3=LuffyStudent('张铁蛋','男',48)
    
    print(stu1.__dict__)
    print(stu2.__dict__)
    print(stu3.__dict__) 

    2、输出

    {'Name': '王二丫', 'Sex': '女', 'Age': 18}
    {'Name': '李三炮', 'Sex': '男', 'Age': 38}
    {'Name': '张铁蛋', 'Sex': '男', 'Age': 48}

    对象:特征与技能的结合体
    类:类是一系列对象相似的特征与相似的技能的结合体

    二、类中的数据属性

    1、代码

    print(LuffyStudent.school,id(LuffyStudent.school))
    
    print(stu1.school,id(stu1.school))
    print(stu2.school,id(stu2.school))
    print(stu3.school,id(stu3.school))
    

    2、输出

    luffycity 12235696
    luffycity 12235696
    luffycity 12235696
    luffycity 12235696
    

    类中的数据属性:是所有对象共有的是所有对象共有的

    三、类中的函数属性

    1、类中的函数属性:是绑定给对象使用的

    1、代码

    print(LuffyStudent.learn)
    LuffyStudent.learn(stu1)
    LuffyStudent.learn(stu2)
    LuffyStudent.learn(stu3)

    2、输出

    <function LuffyStudent.learn at 0x0000000001151268>
      File "F:/s13/day07/5-属性查找.py", line 21, in <module>
        LuffyStudent.learn(stu1)
    TypeError: learn() missing 1 required positional argument: 'x'
    

    2、绑定到不同的对象是不同的绑定方法

    1、代码

    print(stu1.learn)
    stu1.learn(1) #learn(stu1,1)
    print(stu2.learn)
    print(stu3.learn)
    

    2、输出

    <bound method LuffyStudent.learn of <__main__.LuffyStudent object at 0x00000000010E8A58>>
    王二丫 is learning 1
    <bound method LuffyStudent.learn of <__main__.LuffyStudent object at 0x00000000010E8A90>>
    <bound method LuffyStudent.learn of <__main__.LuffyStudent object at 0x00000000010E8AC8>>
    

    3、对象调用绑定方式时,会把对象本身当作第一个传入,传给self

    1、代码

    stu2.learn(2)
    stu3.learn(3)
    
    stu1.x='from stu1'
    LuffyStudent.x='from Luffycity class'
    
    print(stu1.__dict__)
    print(stu1.x)
    

    2、输出

    李三炮 is learning 2
    张铁蛋 is learning 3
    {'x': 'from stu1', 'Age': 18, 'Sex': '女', 'Name': '王二丫'}
    from stu1 

    4、小结

    1、是绑定给对象使用的,绑定到不同的对象是不同的绑定方法

    2、是绑定给对象使用的,绑定到不同的对象是不同的绑定方法,

    3、对象调用绑定方式时,会把对象本身当作第一个传入,传给self

    四、补充说明

    • 站的角度不同,定义出的类是截然不同的;
    • 现实中的类并不完全等于程序中的类,比如现实中的公司类,在程序中有时需要拆分成部门类,业务类等;
    • 有时为了编程需求,程序中也可能会定义现实中不存在的类,比如策略类,现实中并不存在,但是在程序中却是一个很常见的类。
  • 相关阅读:
    idea中git分支的使用
    常用的分布式事务解决方案
    分布式事务解决方案总结
    IDEA中Git的更新、提交、还原方法
    (超详细)使用git命令行将本地仓库代码上传到github或gitlab远程仓库
    Git 安装及用法 github 代码发布 gitlab私有仓库的搭建
    主机ping不通虚拟机,但是虚拟机能ping通主机
    Compile Graphics Magick, Boost, Botan and QT with MinGW64 under Windows 7 64
    windows
    mingw-w64线程模型:posix vs win32(posix允许使用c++11的std:: thread,但要带一个winpthreads,可能需要额外dll)
  • 原文地址:https://www.cnblogs.com/luoahong/p/9915651.html
Copyright © 2020-2023  润新知