zoukankan      html  css  js  c++  java
  • 面向对象(高级)1

    '''
    面向对象的三大特性:封装、继承和多态
    类的成员:字段,方法,属性
    '''
    '''
    字段:普通字段,静态字段,它们在定义和使用中有所区别,而最本质的区别是内存中保存的位置不同
    1.普通字段属于对象
    2.静态字段属于类
    
    '''
    class Foo():
        # 静态字段
        cap='D'
        def __init__(self,name):
            # 普通字段
            self.name=name
    obj=Foo('苍井空')
    print(obj.name)
    print(obj.cap)
    print(Foo.cap)
    '''
    由上述,可以看出普通字段需要通过对象来访问,而静态字段两种方法都可以。
    静态字段在内存只保存一份
    普通字段在每个对象都要保存一份
    应用场景:通过类创建对象时,如果每个对象都具有相同的字段,那么就使用静态字段
    '''
    class Animals():
        def __init__(self,name):
            self.name=name
        def eat(self):
            '''至少一个self参数'''
            print('普通方法')
        @classmethod
        def class_menthod(cls):
            '''定义类方法,至少一个cls参数'''
            print('类方法')
        @staticmethod
        def static_menthod():
            '''定义静态方法,无默认参数'''
            print('静态方法')
    #调用普通方法
    f=Animals('')
    f.class_menthod()
    f.static_menthod()
    Animals.eat(f)
    #调用类方法
    Animals.class_menthod()
    #调用静态方法
    Animals.static_menthod()
    
    
    '''
    
    方法:普通方法,静态方法和类方法,三种方法在内存都归属于类,区别在于调用方式
        普通方法:由对象调用,至少一个参数self,执行普通方法时,自动将调用该方法
                  的对象赋值给self
        类方法:由类调用;至少一个cls参数;执行类方法时,自动将调用该方法的类复制给cls
        静态方法:由类调用;无默认参数
    相同点:对所有方法而言,均属于类(非对象)中,所以,在内存中也只保存一份
    不同点:方法调用者不同,调用方法时自动传入的参数不同
    '''
    class Dog():
        def func(self):
            print('方法')
        #定义属性
        @property
        def prop(self):
            a='属性'
            return a
    d=Dog()
    d.func()
    print(d.prop)
    '''
    定义与调用属性时注意:
        定义时:在普通方法的基础上添加@property装饰器;
        定义时,属性仅有一个self参数
        调用时,无需括号
    属性存在的意义;
        访问属性时可以制造出和访问字段完全相同的假象,属性由方法变种而来,如果
        Python中没有属性,方法可以完全代替其功能
    '''
    
    class Pager:
        def __init__(self,current_page):
            #用户当前请求的页码
            self.current_page=current_page
            #每页显示10条
            self.per_items = 10
    
        @property
        def start(self):
            val=(self.current_page-1)
            return val
        @property
        def end(self):
            val=self.current_page * self.per_items
            return val
    p=Pager(10)
    # print(p.start)
    # print(p.end)
    '''
    属性的功能是:属性进行一系列的逻辑计算,最终计算结果返回
    属性定义的两种方式:
        1.装饰器:在方法上应用装饰器
        2.静态字段:在类中定义值为property对象的静态字段
    '''
    class Goods():
        @property
        def price(self):
            return 'dog'
        @price.setter
        def price(self,value):
            print('@price.setter')
            print(value)
        @price.deleter
        def price(self):
            print('@price.deleter')
    
    b=Goods()
    print(b.price)
    b.price='wupeiqi'
    print(b.price)
    del b.price
    print(b.price)
  • 相关阅读:
    如何上架一个iOS APP(2020年最新版教程)
    可以在Windows申请iOS证书上架iOS APP---Appuploader
    上传了ipa但iTunes Connect没有构建版本问题
    iOS证书类型介绍及申请教程
    APP专用密码app-specific password怎么设置
    Windows电脑申请iOS证书教程及工具分享
    【2020】申请iOS个人开发者账号流程
    【分享】使用免费的苹果开发者账号申请iOS证书打包测试教程
    iOS证书及描述文件制作流程详解
    linux /bin/bash^M: bad interpreter的解决办法
  • 原文地址:https://www.cnblogs.com/ldq1996/p/8046339.html
Copyright © 2011-2022 走看看