• python面向对象整理



    一.面向对象引导

    ###########################################
    class Bar:
    def func(self,arg): #self代指的就是中间人
    print(self,self.name,self.age,arg)


    z = Bar() #中间人(对象) = class类
    z.name = "Python" #给中间人的属性赋值
    z.age = 23
    z.func("who you are?")

    print(z.name)

    ###########################################

    二.封装

    __init__: 构造方法,有类赋值给变量的时候,执行的初始话方法

    ##########################################
    class Bar:
    #自己创造的构造方法
    def __init__(self,name,age): #初始化方法
    self.name = name
    self.age = age
    #类的默认构造方法
    def __init__(self):
    pass

    def foo(self):
    print(self,self.name,self.age)

    a = Bar("python",23)
    a.foo()

    ###########################################

    class Person:
    def __init__(self,name,age,like='python'):
    self.name = name
    self.age = age
    self.like = like

    def show(self):
    print("%s-%s-%s" %(self.name,self.age,self.like))

    a1 = Person("python",23)
    print(a1.name,a1.age)
    a1.show()

    #############################################

    三.继承

    1.继承
    class father:
    pass
    class son(father):
    pass

    2.重写:为了不继承父类中的方法,重写一个和父类方法同名的方法
    3.self永远是执行方法的调用者
    4.调用父类方法的两种方式:
    (1) super(子类,self).父类中的方法(...)
    (2) 父类名.父类中的方法(self,....)
    5.继承多个父类class son(father,mother):
    a.左侧优先
    b.一条道走到黑
    c.同一个根,根最后执行
    6.在子类中调用父类的方法
    语法格式是: super(son,self).hello() #执行父类指定的方法

    ############################################
    class father:
    def __init__(self):
    pass
    def hello(self):
    print("father hello world")

    def world(self):
    print("father world is best")
    class mother:
    def hello(self):
    print("mother hello world!")
    def world(self):
    print("mother world is best!")
    class son(father,mother):
    def __init__(self):
    pass
    def hello(self): #重写父类的方法:不执行父类中的方法,执行重写后的方法
    #######执行父类的方法就这两种方法:
    # super(son,self).hello() #执行父类指定的方法
    # father.hello(self) #执行父类中的方法
    print("This is not hello")


    obj= son()
    obj.hello()
    obj.world()

    ##############################################

    三.多态

    python原生就是多态的,所以不用关注python的传参的参数类型

    四.面向对象的中高级部分


    普通字段:字段,属于对象的属性值,定义赋值的时候使用self,只能通过对象访问
    静态字段:在函数的之外的字段就是静态字段,静态字段属于类,内存中只保留一份
    访问的时候可以通过类名.静态字段名 访问 也可以通过 对象访问
    #######
    普通方法:保存在类中,使用对象调用,参数必须带self,self表示当前对象
    静态方法:保存在类中,静态方法不用创建对象,可以直接调用,相当与单独创建了一个函数
    静态方法中的self可以加也可以不加,不会影响静态方法本身
    静态方法使用 装饰器 @staticmethod 修饰
    类方法:保存在类中,又类直接调用
    类方法的参数写cls,cls表示当前类

    属性方法:定义在类中,使用对象调用,在方法的上面使用propety修饰
    属性方法的作用: 调用方法只为了获取参数值的时候,使用参数方法去除方法的小括号,只是为了好看(^*^)
    调用的时候使用 对象名.方法名[不加小括号]
    @方法名.setter 对应的是赋值操作,就是说当你赋值的时候 对象名.方法名= *** ,它自动调用被 方法名.setter修饰的方法
    @方法名.deleter 对应的是del 对象名.方法名 操作,就是说当你删除的时候,它自动调用呗方法名.deleter修饰的方法

    #################################################################


    class Foo:

    def __init__(self):
    pass

    def bar(self): #普通方法
    print("this IS a bar")

    @staticmethod #静态方法
    def test():
    print("this is a test")

    @staticmethod # 静态方法
    def test1(a,b):
    print("this is a test")
    @classmethod
    def test2(cls):
    print("类方法")


    @property
    def test3(self):
    print("property方法")
    @test3.setter
    def test3(self,val):
    print("赋值方法")
    print(val)
    @test3.deleter
    def test3(self):
    print("删除方法")


    foo = Foo()
    foo.test3
    foo.test3=1213
    del foo.test3
    ################################################

    实现简单分页的代码
    ————————————————————————————————————————————————
    class Pargination:

    def __init__(self,current_page):
    self.page = int(current_page)

    def start(self):
    val = (self.page - 1) * 10
    return val
    def end(self):
    val = self.page * 10
    return val


    li = []
    for i in range(1000):
    li.append(i)

    while True:
    p = input("请输入你的查看的页码:")


    page = Pargination(p)
    print(li[page.start():page.end()])

    #################################################

  • 相关阅读:
    springboot+https+http
    3.kettle-定时执行任务
    sqlserver清空删除日志
    C++学习(二)
    随笔(二) 安装Code::Blocks遇到的问题
    随笔(一) tensorflow环境的搭建
    C++学习(一)
    前端学习日记 (三)
    前端学习日记 (二)
    前端学习日记 (一)
  • 原文地址:https://www.cnblogs.com/aniuzaixian/p/8043778.html
Copyright © 2020-2023  润新知