zoukankan      html  css  js  c++  java
  • Python 学习day22

    python day22

    classmethod方法

    classmethod是一个装饰器,可以装饰给类内部的方法,使该方法绑定给类来使用

    ​ ---对象的绑定方法特殊之处

    ​ 由对象来调用,会将对象当做第一个参数传给该方法

    ​ ---类的绑定方法特殊之处

    ​ 由类来调用,会将类当做第一个参数传给方法

    class People:
        def __init__(self,name,age):
            self.name = name
            self.age = age
            
        @classmethod
        def tell_info(cls)  #cls = people
        	print('.....')
        	pass
    p = People('momo',24)
    p.tell_info()
    People.tell_info()
    

    staticmethod方法

    staticmethod是一个装饰器,可以装饰给类内部的方法,使该方法既不帮顶给对象,也不绑定给类

    class Teacher:
        def __init__(self,user,pwd):
            self.user = user
            self.pwd =pwd
            
        @staticmethod
        def create_id():
            uuid_obj = uuid.uuid4()
            md5 = hashlib.md5()
            md5.update(str(uuid_obj).encode('utf8'))
            return md5.hexdigest()
        
    print(Teacher.create_id())
    tea1 = Teacher('momo','123')
    print(tea1.create_id)
    

    面向对象高级

    isinstance

    ----isinstance(参数1,参数2):

    python内置的函数,可以传入两个参数,用于判断参数1是否是参数2的一个实例

    ​ (判断一个对象是否是一个类的实例)

    class Foo:
        pass
    
    class Goo(Foo):
        pass
    
    foo_obj = Foo()
    print(isinstance(foo_obj,Foo))
    

    issubclass

    ----issubclass(参数1,参数2):

    python内置的函数,可以传入两个参数,用于判断参数1是否是参数2的子类

    ​ (判断一个类是否是另一个类的子类)

    print(issubclass(Goo,Foo))
    

    反射

    反射 : 指的是通过'字符串'对 对象或类 的属性进行操作

    - hasatter : 通过字符串,判断该字符串是对象或类的属性
    - getatter : 通过字符串,获取对象或类的属性
    - setatter : 通过字符串,设置对象或类的属性
    - delatter : 通过字符串,删除对象或类的属性
    
    class People:
        country = 'China'
        
        def __init__(self,name,age,sex):
            self.name = name
            self.age = age
            self.sex = sex
            
    #普通方式
    p = People('momo',24,'female')
    print('name' in p.__dict__)
    #hasattr  # 通过字符串,判断该字符串是对象或类的属性
    print(hasattr(p,'name'))
    print(hasatter(People,'country'))
    
    #getattr 通过字符串,获取对象或类的属性
    print(getattr(p,'name','momo2'))
    print(getattr(People,'country2','China'))
    
    #setattr 通过字符串,设置对象或类的属性
    setattr(p,'sal','3.0')
    print(hasattr(p,'sal'))
    
    #delattr  通过字符串,删除对象或类的属性
    del p.sal
    print(hasattr(p,'sal'))
    
    ###小练习
    class Movie:
        def input_cmd(self):
            print('输入命令!')
            while True:
                cmd = input('请输入执行的方法名:').strip()
                
                if hasattr(self,cmd):
                    method = getattr(self,cmd)
                    method()
                else:
                    print('命令错误!')
               
        def upload(self):
            print('开始上传电影....')
            
        def download(self):
            print('开始下载电影....')
            
    movie_obj = Movie()
    movie_obj.input_cmd()
    

    魔法方法(类的内置方法)

    魔法方法:

    ​ 凡是在类内部定义,以__开头__结尾的方法都称之为魔法方法,又称'类的内置方法'

    ​ 魔法方法会在某些条件成立时触发

    #__init__ : 在调用类时触发
    #__str__ : 会在打印对象时触发
    #__del__ : 对象被销毁前执行该方法,该方法会在最后执行
    #__getattr__ : 会在对象 . 属性时,'属性没有'的情况下才会出发
    #__setattr__ : 会在'对象.属性 = 属性值'的情况下触发  此时该赋值作废
    #__call__ : 会在对象被调用时触发
    #__new__ : 会在__init__执行前触发
    

    单例模式

    单例模式指的是单个实例,实例指的是调用类产生的对象

    实例化多个对象会产生不同的内存地址,单例可以让所有调用者,在调用类产生对象的情况下都指向同一份内存地址

    单例的目的:

    ​ 为了减少内存的占用

    class File:
    
        __instance = None
    
        @classmethod
        def singleton(cls,file_name):
            if not cls.__instance:
                obj = cls(file_name)
                cls.__instance = obj
            return cls.__instance
    
        def __init__(self,file_name,mode='r',encoding='utf8'):
            self.file_name = file_name
            self.mode = mode
            self.encoding = encoding
    
    obj1 = File.singleton('test.txt')  
    obj2 = File.singleton('test.txt')  
    obj3 = File.singleton('test.txt')  
    print(obj1)
    print(obj2)
    print(obj3)
    
    
        # 单例方式2:
        def __new__(cls, *args, **kwargs):
            # cls.__new__(cls, *args, **kwargs)
            if not cls.__instance:
                cls.__instance = object.__new__(cls)
            return cls.__instance
    
  • 相关阅读:
    html5-移动端布局模板
    html5-figure和figcaption元素
    html5-hgroup和address元素
    html5-新元素新布局模板-完善中
    数据库操作符
    数据库操作,复制表
    数据库基本sql语句
    反射+工厂模型+Properties
    java :动态代理
    java 反射
  • 原文地址:https://www.cnblogs.com/samoo/p/11663625.html
Copyright © 2011-2022 走看看