• 绑定方法和非绑定的方法


    绑定方法:

    对象绑定方法/类的绑定方法

    绑定方法:特殊之处,绑定给谁就是谁来调,并且会把自身传过来

    类的绑定方法:绑定给类的,类来调用,会把类自身传过来

    类的绑定方法用在什么地方?

    ​ --不需要通过对象,只需要通过类就能获取到一些东西的时候,用类的绑定方法

    ​ --类的绑定方法,可以由对象来调

    '''
    类中使用@classmethod修饰的方法就是绑定到类的方法,这类方法专门为类定制,通过类名调用绑定到类的方法时,会将类本身当做参数传给类方法的第一个参数
    '''
    class Operate_database():
        host='192.168.0.5'
        port='3306'
        user='lzs'
        password='1234'
        
        @classmethod
        def connect(cls):##约定俗成第一个参数名为cls,也可以定义为其他参数名
            print(cls)
            print(cls.host+":"+cls.port+' '+cls.user+'/'+cls.password)
    Operate_database.connect()
    
    ##<class '__main__.Operate_database'>
    ##192.168.0.5:3306 abc/123456
    

    非绑定方法:

    在类内部使用@staticmethod修饰的方法即为非绑定方法,这类方法和普通定义函数没有区别,不与类或对象绑定,谁都可以调用,且没有自动传值的效果

    import hashlib
    
    class Operate_database():
        def __init__(self,host,port,user,password):
            self.host=host
            self.port=port
            self.user=user
            self.password=password
            
            @staticmethod
            def get_password(salt,password):
                m=hashlib.md5(salt.encode('utf=8')) ##加盐处理
                m.update(password.encode('utf-8'))
                return m.hexdigest()
    hash_password=Operate_database.get_password('lala','123456') ##通过类来调用
    print(hash_password)
    ####f7a1cc409ed6f51058c2b4a94a7e1956
    
    
    p=Operate_database('192.168.0.5','3306','abc','123456')
    hash_password=p.get_password(p.user,p.password)  ##也可以通过对象调用
    print(hash_password)
    

    类的派生:

    继承父类的同时自己有init,然后也需要父类的init

    类的组合:

    类对象可以引用/当作参数传入/当做返回值/当做容器元素,类似于函数对象

    菱形继承问题:

    新式类:继承object的类,python3中全是新式类

    经典类:没有继承object的类,只有python2中有

    在菱形继承的时候,新式类的广度优先(树的顶点最后找);经典类深度优先(一路找到底,再找到底,再找旁边的)

    多态与多态性:

    一种事物的多种形态,动物---》人/猪/狗

    ##多态
    import abc
    
    class Animal(metaclass=abc.ABCmeta):
        @abc.abstractmethod
        def eat():
            print('eat')
    class People(Animal):
        def eat():
            pass
    class Pig(Animal):
        def eat():
            pass
        def run():###报错,因为规定了要有eat()属性
            def run():
                pass
            
    ###多态性
    peo=People()
    peo.eat()
    peo1=People()
    peo1.eat()
    pig=Pig()
    pig.eat()
    
    def func(obj):
        obj.eat()
    class Cat(Animal):
        def eat()
        pass
    cat=Cat()
    func(cat)
    

    鸭子类型:

    只要长得像鸭子,叫的的像鸭子,游泳像鸭子,就是鸭子

    类的封装:

    隐藏属性,只有类内部可以访问,类外部不可以访问

    class Foo():
        __count=0
        def get_count(self):
            return self._count
    f=Foo()
    f.__count   ##报错
    f._Foo__count   ##不能这样做
    

    类的property特性

    把方法变成属性引用

    class People():
        def __init__(self,height,weight):
            self.height=height
            self.weight=weight
            
        @property
        def bmi(self):
            return weight/(height**2)
        @bmi.setter
        def bmi(self,value)
        	print('setter')
        @bmi.deleter
        def bmi(self):
            print('delter')
    peo=People
    peo.bmi
    

    类与对象的绑定方法和非绑定方法:

    没有任何装饰器装饰的方法就是对象的绑定方法,类能调用,但是必须传参给self

    被@classmethod装饰器装饰的方法是类的绑定方法,参数写出cls,cls是类的本身,对象也能调用,参数cls还是类本身

    被@staticmethod装饰器装饰的方法就是非绑定方法,就是一个普通的

    既然选择了远方,只能风雨兼程
  • 相关阅读:
    骨灰级程序员20条编程经验
    js常用正则表达式
    css样式大全(整理版)
    ASP.NET中常用的26个优化性能方法
    C# 中的委托和事件
    Page的ResolveClientUrl与ResolveUrl读取路径
    层设定固定高度,内容超过高度,自动延伸
    Catalan数(卡塔兰数)
    称球问题
    zabbix分布式监控系统安装配置
  • 原文地址:https://www.cnblogs.com/lzss/p/11481874.html
Copyright © 2020-2023  润新知