• 面向对象总复习


    面向过程编程思想:
        核心是 过程 二字,过程指的是解决问题的步骤,即先干嘛再干嘛
        基于编程思想编写程序,相当于一条流水线,一种机械式的思维方式


    面向对象的编程思想
        核心是 对象 二字  是个容器
        对象是指的是数据与方法的集合体,也就是一个容器
        基于该编程的思想编写程序,就好比是创造世界,一种上帝式的思维方式。

    一、类与对象
    对象:特征与技能的集合体,即数据与方法
    类:一系列的相似特征与技能的集合体

    二、面向对象的三大特征
        封装:把一堆数据属性与方法属性整合到对象内,
        隐藏:
            __+属性:-----》把属性变形了,即影藏起来

            封:封起来
            装:装起来

        继承:
            父类是一系列类的共有属性与方法的结合体
            子类是可以继承父类的属性和方法的结合体


            组合:1、继承父类的属性,且不与父类耦合在一起
                 2、把一个对象放到另外一个对象内部
                 组合是继承的一种应用

        多态:
            1.抽象类
            2.鸭子类型

      1 # class People:
      2 #     def __init__(self, name, age, sex):
      3 #         self.name = name
      4 #
      5 #
      6 # class Student(People):
      7 #     pass
      8 
      9 #
     10 #
     11 # class Course:
     12 #     def __init__(self, name):
     13 #         self.name = name
     14 #
     15 #
     16 # class Student:
     17 #     def __init__(self, name, age, sex):
     18 #         self.name = name
     19 #         self.age = age
     20 #         self.sex = sex
     21 #         self.course = []
     22 #
     23 # course = Course('python')
     24 # # print(course.name)
     25 #
     26 # student = Student('张全蛋', 18, 'male')
     27 # student.course.append(course)
    View Code

    三、绑定方法
        对象的绑定方法:有对象来调用,会自动把对象本身当成参数传递给self,谁调用就传谁

      1 '''
      2 @classmethod
      3 '''
      4 
      5 class People:
      6     country = 'china'
      7 
      8     def __init__(self, name):
      9         self.name = name
     10 
     11     def run(self):
     12         print('%s running...' % self.name)
     13 
     14     @classmethod
     15     def eat(cls):
     16         print(cls)
     17 
     18 # p1 = People('l_egon')
     19 #
     20 # p1.eat()
     21 # People.eat()
     22 
     23 
     24 # p2 = People('jason')
     25 #
     26 # print(p1.run)  # 0x0000026EFFE77F60
     27 # print(p2.run)  # 0x000001EAAB3AC710
     28 
     29 
     30 
     31 '''
     32 # staticmethod: 非绑定方法
     33 '''
     34 
     35 # class People:
     36 #     country = 'china'
     37 #
     38 #     def __init__(self, name):
     39 #         self.name = name
     40 #
     41 #     def run(self):
     42 #         print('%s running...' % self.name)
     43 #
     44 #     @staticmethod
     45 #     def eat():
     46 #         print('eat....')
     47 #
     48 # p = People('tank')
     49 # p.eat()
     50 # People.eat()
     51 
     52 
     53 '''
     54 # property:
     55 '''
     56 # class People:
     57 #     country = 'china'
     58 #
     59 #     def __init__(self, name, height, weight):
     60 #         self.name = name
     61 #         self.height = height
     62 #         self.weight = weight
     63 #
     64 #     def run(self):
     65 #         print('%s running...' % self.name)
     66 #
     67 #     @property
     68 #     def bmi(self):
     69 #         return self.weight / (self.height ** 2)
     70 #
     71 #
     72 # p = People('tank', 1.8, 65)
     73 # # print(p.bmi()())
     74 #
     75 # print(p.bmi)  # p.bmi()# student.course.append(course)
    View Code

    四、classmethod,staticmethod ,property

    classmethod :本质上是装饰器,装饰给类里面的方法
    被装饰的方法,类的绑定方法

    staticmethod: 非绑定方法,装饰成普通的函数

    @property:在类内部,把一个函数装饰成一个数据属性

    六 面向对象:高级
    1、反射:通过字符串操作类与对象的属性
        hasattr:判断对象或者类的属性是否存在

        getattr:获取对象或者类的属性
                参数1:对象
                参数2:'属性名'
                参数3:默认值

        setattr:设置对象或者类的属性

        delattr:删除对象或者类的属性

    2、类的内置方法:
    __init__():调用类的时候自动触__init__
    __str__():打印对象的时候触发此方法,此方法内必须return一个字符串
    __del__():在对象的占用空间被释放,会自动触发__del__()方法的运行
    __steattr__:修改属性会自动触发
    __delattr__:删除属性会自动触发
    __call__:调用对象的时候触发

      1 class People(object):
      2     school = 'oldboy'
      3 
      4     # def __del__(self):
      5     #     print('对象已被清除!')
      6     #
      7     def __init__(self, name):
      8         self.name = name
      9     #
     10     # def __str__(self):
     11     #     return '%s from %s' % (self.name, self.school)
     12 
     13     def __setattr__(self, key, value):
     14         self.__dict__[key] = value
     15         # print(key, value)
     16 
     17     # def __call__(self):
     18     #     print('调用对象的时候会自动此方法')
     19     #     print(self.school)
     20 
     21 p = People('tank')
     22 # print(p)
     23 # p.age = 18
     24 # p.name = 'jason'
     25 #
     26 # # print(p.__dict__)
     27 # print(People.__dict__)
     28 
     29 p()
    View Code
  • 相关阅读:
    BurpSuite—-Spider模块(蜘蛛爬行)
    BurpSuite系列(一)----Proxy模块(代理模块)
    hadoop HA集群搭建步骤
    HBase详解
    MapReduce两种执行环境介绍:本地测试环境,服务器环境
    HBase性能优化方法总结
    HDFS原理解析
    ZooKeeper 典型应用场景
    Redis总结
    基于Apache Curator框架的ZooKeeper使用详解
  • 原文地址:https://www.cnblogs.com/king-home/p/10778844.html
Copyright © 2020-2023  润新知