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

    面向对象 - 类:
    编程:语法 + 数据结构 + 算法
    编程范式:面向过程 面向对象
    1.面向过程:解决问题的步骤
    优点:复杂的问题流程化,进而简单化 应用在:程序不在需要扩展了
    缺点:可扩展性差,牵一发而动全身
    2.面向对象:特征与技能的结合体 一切皆对象 应用在:用户需求经常变化,互联网应用,游戏,企业内部应用
    优点:可扩展性高
    缺点:编程复杂度高
    3.类:一系列对象相似的特征与技能的结合体 站在不同的角度 得到的分类是不一样的
    现实中:先有对象后又类
    程序中:先有类后有对象
    特点:1.定义的时候就运行了!
    2.站的角度不同,定义出的类截然不同
    3.现实中的类并不完全等于程序中的类,比如现实中的公司类,在程序中有时需要拆分成部门类,业务类等
    4.有时为了编程需求,程序中也可能会定义现实中不存在的类,比如策略类,现实中并不存在,但是在程序中却是一个很常见的类
    5.python 一切皆对象,在python3里统一了类与类型(list dict)的概念 就是说: 类型也是类
    li = list([1,2,3,4]) == [1,2,3,4] li.append(5) list.append(li,6)
    dic=dict({'name':'alice'}) == {'name':'alice'} dic['age']=12 dict.setdefault(dic,'sex','female')
    用途:1.操作属性(数据属性、函数属性) 增删改查
    名称空间:LuffyStudent.__dict__
    增:LuffyStudent.name='alice'
    删:del LuffyStudent.school
    改:LuffyStudent.school='Luffy'
    查:LuffyStudent.school
    2.实例化:产生对象
    stu=LuffyStudent()
    4.对象: 类的实例化
    4.1.__init__ 定制对象自己独有的特征, 实例化对象时,会自动触发
    实例化的步骤: 1.产生一个空对象 2.触发函数LuffyStudent.__init__(stu,name,age)
    增: stu3.sex='female'
    删: del stu3.age del stu3.__dict__['name'] 注:不能删类中的属性 del stu3.school
    改: stu3.name='alex'
    查: stu3.name stu3.school
    4.2.属性查找
    类的数据属性--->是所有对象共有的--->stu1.school is stu2.school == True
    类的函数属性--->是绑定给对象使用的-->(bound method) stu1.learn is stu2.learn == False
    stu1.learn() == LuffyStudent.learn(stu1) 把对象本身当作第一个参数传给self
    stu1.learn(5) == LuffyStudent.learn(stu1,5) 可传其他参数
    属性查找--->顺序: 1.对象先在自己内部找-->2.类中找-->3.父类找-->4.不会去全局找

      1 # x='global'
      2 class LuffyStudent:
      3     school = 'luffycity'
      4 
      5     def __init__(self,name,age,sex):
      6         # print(self.__dict__)
      7         self.name=name
      8         self.age=age
      9         self.sex=sex
     10         # print(self.__dict__)
     11         # print(self.name,self.age)
     12 
     13     def learn(self,data):
     14         print('%s is learning'%self.name,data)
     15         print(self.name,self.age)
     16 
     17     def eat(self):
     18         print('is eatting')
     19 
     20     # print('定义的时候就运行了!')
     21 
     22 # stu1 = LuffyStudent       # 类
     23 # stu2 = LuffyStudent()     # 对象
     24 # print(stu1)               # <class '__main__.LuffyStudent'>
     25 # print(stu2)               #<__main__.LuffyStudent object at 0x00000225DEDD3F98>
     26 # print(stu2.school)        # luffycity
     27 # stu2.learn()              # is learning
     28 
     29 # print(LuffyStudent.__dict__)
     30 # print(LuffyStudent.school)
     31 # LuffyStudent.name='alice'
     32 # print(LuffyStudent.__dict__['school'])
     33 # # print(LuffyStudent.__dict__)
     34 # print(LuffyStudent.name)
     35 # del LuffyStudent.school
     36 # print(LuffyStudent.__dict__)
     37 # LuffyStudent.school='Luffy'
     38 # print(LuffyStudent.school)
     39 # print(LuffyStudent.learn)
     40 
     41 # stu4=LuffyStudent()
     42 # print(stu4.__dict__)
     43 # print(stu4.school)
     44 
     45 # stu3=LuffyStudent('alice',12)
     46 # print(stu3.school,stu3.name,stu3.age)
     47 # stu3.learn()
     48 # print(stu3.__dict__)
     49 # print((stu3.name,stu3.school,stu3.learn))
     50 # stu3.sex='female'
     51 # print(stu3.__dict__)
     52 # print(stu3.sex)
     53 # del stu3.__dict__['name']
     54 # del stu3.school
     55 # print(stu3.__dict__)
     56 # stu3.name='aaaa'
     57 # print(stu3.__dict__)
     58 # print(stu3.name,stu3.age,stu3.sex,stu3.school)
     59 # stu3.school='Luffy'
     60 # print(stu3.__dict__)
     61 # print(stu3.school)
     62 
     63 # stu1=LuffyStudent('alice',12)
     64 # stu2=LuffyStudent('alex',18)
     65 # print(stu1.__dict__)
     66 # # stu1.school='luffy'
     67 # print(stu1.school,stu1.name)
     68 # print(id(stu1.school),id(stu2.school))
     69 # print(stu1.school is stu2.school)
     70 # print(stu1.learn,stu2.learn)
     71 # print(stu1.learn is stu2.learn)
     72 # stu1.learn()
     73 
     74 # stu1.learn(3)
     75 # LuffyStudent.learn(stu1,5)
     76 
     77 # stu1.scho='luffy'
     78 # print(stu1.scho)
     79 # LuffyStudent.x='xxx'
     80 # print(LuffyStudent.__dict__)
     81 # stu1.x='sss'
     82 # print(stu1.x)
     83 
     84 # li = list([1,2,3,4])
     85 # li.append(5)
     86 # print(li)
     87 # dic=dict({'name':'alice'})
     88 # dic['age']=12
     89 # print(dic)
     90 # dict.setdefault(dic,'sex','female')
     91 #
     92 # print(dic)
     93 # print(type(list))
     94 #
     95 # li=list([1,2,3,4])
     96 # print(li)
     97 # li.append(5)
     98 # list.append(li,6)
     99 # print(li)
    100 
    101 stu1=LuffyStudent('alice',12,'female')
    102 print(stu1.sex)


  • 相关阅读:
    ubuntu安装后做得几件事情 【robby_chan】
    malloc函数的一种简单的原理性实现[转]
    了解B树 B+树
    win下格式转为utf8 编码 转码
    log4j2与slf4j日志桥接
    java获取当前行数
    java获取服务器ip地址解决linux上为127.0.0.1的问题
    log4j2的基本使用
    navicator使用之mysql
    log4j与log4j2日志文件的操作
  • 原文地址:https://www.cnblogs.com/alice-bj/p/8543596.html
Copyright © 2011-2022 走看看