上一页 1 2 3 4 5 6 7 8 9 ··· 14 下一页
摘要: (1)使用type直接创建类 Test = type('Test01', (object,), {'name': 'haha', 'age': 18})t1 = Test()print(type(t1))print(Test.__dict__) (2)继承type,用继承类创建类 class MyT 阅读全文
posted @ 2022-04-23 18:24 狒狒桑 阅读(17) 评论(0) 推荐(0) 编辑
摘要: (1)描述器类 class Field: # 设置描述器类对象属性时调用 def __set__(self, instance, value): print(' __set__ ') self.value = value # 获取描述器对象属性时调用 def __get__(self, instan 阅读全文
posted @ 2022-04-23 11:15 狒狒桑 阅读(17) 评论(0) 推荐(0) 编辑
摘要: class MyClass(object): # 设置对象属性时调用 def __setattr__(self, key, value): print(' __setattr__ ') super().__setattr__(key, value) # 访问属性不存在,__getattribute_ 阅读全文
posted @ 2022-04-23 10:58 狒狒桑 阅读(17) 评论(0) 推荐(0) 编辑
摘要: (1)私有属性 class MyClass: attr1 = 'attr1' # 共有属性 _attr2 = '_attr2' # 私有属性,可直接访问 __attr3 = '__attr3' # 私有属性,改变了属性名:_MyClass__attr3m1 = MyClass()print(m1.a 阅读全文
posted @ 2022-04-22 22:49 狒狒桑 阅读(20) 评论(0) 推荐(0) 编辑
摘要: class Animal: def run(self): print(' animal run ')class Cat(Animal): def run(self): print(' cat run ')class Dog(Animal): def run(self): print(' dog ru 阅读全文
posted @ 2022-04-22 22:39 狒狒桑 阅读(23) 评论(0) 推荐(0) 编辑
摘要: class MyOperator: def __init__(self, data): self.data = data def __add__(self, other): return self.data + other.data def __sub__(self, other): return 阅读全文
posted @ 2022-04-22 21:56 狒狒桑 阅读(23) 评论(0) 推荐(0) 编辑
摘要: (1)文件读取--上下文管理器 with open(filename,'w') as f : class MyOpen: def __init__(self, filename, mode, encoding='utf-8'): self.filename = filename self.mode 阅读全文
posted @ 2022-04-22 21:46 狒狒桑 阅读(21) 评论(0) 推荐(0) 编辑
摘要: class Decorator: def __init__(self, func): self.func = func def __call__(self, *args, **kwargs): print(' call ') return self.func(*args, **kwargs)@Dec 阅读全文
posted @ 2022-04-22 18:58 狒狒桑 阅读(12) 评论(0) 推荐(0) 编辑
摘要: (1)不用装饰器实现 class MyClass2(object): __instance = None def __new__(cls, *args, **kwargs): if not cls.__instance: cls.__instance = object.__new__(cls) re 阅读全文
posted @ 2022-04-22 18:47 狒狒桑 阅读(15) 评论(0) 推荐(0) 编辑
摘要: 类方法和静态方法的区别: (1)类方法可以操作类属性,静态方法不能操作类属性 (2)类方法】静态方法可以用 类.类方法调用;实例方法只能通过实例对象调用 class MyClass: def __init__(self, name): self.name = name # 设置类方法 @classm 阅读全文
posted @ 2022-04-22 17:11 狒狒桑 阅读(43) 评论(0) 推荐(0) 编辑
上一页 1 2 3 4 5 6 7 8 9 ··· 14 下一页