1. 多态(多种类型)
多态的体现如下:
def func(arg): print(arg) func(1) func("alex") func([11,12,13]) #func可以是任何东西 #但是对于C#,java都不是这样,参数必须要指定类型
多态的缺点:在缺少注释的情况下,不知道arg是什么类型,它有什么用处。比如说如果确定好是个list的话,那就可以append()。但是不确定的时候就不知道怎么用了。
2. 面向对象中的类成员
1) 字段
- 静态字段: 代码加载时已经创建,属于类
- 普通字段: 有对象才有这个,属于对象
class Province: country = "中国" #静态字段,保存在类里面 def __init__(self,name): self.name = name #保存在对象里,这是普通字段,如果没有调用这个函数,那么这个就不在内存里
2) 方法(属于类)
- 普通方法: 定义必须有self,通过对象执行
- 静态方法: @staticmethod,去掉self,参数可有可无,由类执行
- 类方法: @classmethod,静态方法的一种特殊形式。自动传类参数,由类执行
class Province: def __init__(self,name): self.name = name def show(self): #普通方法 print(self.name) @staticmethod #静态方法,1.加这个,2.去掉self,3.参数可有可无 def f1(arg1): print(arg1) @classmethod #类方法,必须要有参数 def f2(cls):#这个参数是自动传递的,是类名。静态方法的一种。 print(cls) def f3(self): return self.name[1] # #方法是由对象调用的 # obj = Province("河南") # obj.show() # #普通方法由对象去调用执行(方法属于类) # Province.f1(111)#静态方法,只跟类有关,不需要对象,由类调用,跟函数一样的 # Province.f2()#类方法,参数是类名 # obj = Province("alex") # ret = obj.f3() # print(ret)
- 类方法使用举例,类既可以有方法的作用,又可以像字段一样被修改
#做个分页的类 class Pager: def __init__(self,all_count): self.all_count = all_count def all_pager(self): a1, a2 = divmod(self.all_count,10) #商,余数 if a2 == 0: return a1 else: return a1+1 p= Pager(101) result = p.all_pager() print(result)#这是用方法去写 #####用属性写一遍###### class Pager: def __init__(self,all_count): self.all_count = all_count @property def all_pager(self): a1, a2 = divmod(self.all_count,10) #商,余数 if a2 == 0: return a1 else: return a1+1 @all_pager.setter #设置,可以赋值 def all_pager(self,value): print(value) @all_pager.deleter #删除,其实可以自己定义,提供了一种关联方式而已 def all_pager(self): print("all_pager") p = Pager(101) result = p.all_pager#就不用括号了,跟用字段一样的 print(result)
- 属性也可以这样写
#属性也可以这样写 class Pager: def f1(self): return 123 def f2(self): pass def f3(self): pass foo = property(fget=f1,fset=f2,fdel=f3) moo = property(f1,f2,f3) p = Pager() m = p.foo n = p.moo print(m,n)
3) 成员修饰符
- 可以选择是公有成员还是私有成员,私有成员的话前面加__,
########成员修饰符########### class Foo: __cc = "123" #私有成员 def __init__(self,name,age): self.name = name #公有成员 self.__age = age #私有成员 def f1(self): print(self.name) def f2(self): print(self.__age) def f3(self): print(Foo.__cc) @staticmethod def f4(): print(Foo.__cc) def __f5(self): print("456") def f6(self): Foo.__f5(self) obj = Foo("alex",18) print(obj.name) #外部访问,age就访问不到 obj.f1() #内部访问 #同时能内部和外部访问,就是公共的,公有成员 #私有成员前面加两个__,只有内部能访问,即使继承也不行 obj.f2() #内部访问,私有成员age就被查到了 obj.f3() #内部访问,私有成员__cc就能,但是直接调__cc就不行了 Foo.f4() #这样就实现了不用对象就可以 obj.f6() #私有方法就这样被调出来了
- 一些特殊成员
#######特殊成员######## class Foo: def __init__(self): #构造方法,类后面加括号执行这个 self.name = "alex" def __del__(self): #析构方法 pass def __call__(self, *args, **kwargs):#对象后面加括号调用这个 print("call") def __str__(self): return "haha" #输出对象的时候输出的就是这个 p = Foo() print(p.__module__) print(p.__class__) p() #调用的就是__call___ Foo()() #跟上面那个p()一样 obj = Foo() print(obj) #输出的就是__str__里面的东西 print(obj.__dict__) #这个能返回对象中封装的所有数据 print(Foo.__dict__) #这个能返回类中封装的所有数据
- 像处理字典一样处理类
#######像处理字典一样处理类##### class Foo(object): def __getitem__(self, key): print("123") def __setitem__(self, key, value): print("456") def __delitem__(self, key): print("789") obj = Foo() result = obj['k1'] # 自动触发执行 __getitem__ obj['k2'] = 'wupeiqi' # 自动触发执行 __setitem__ del obj['k1'] # 自动触发执行 __delitem__
- 类迭代
######类迭代######## 列表迭代 m = [1,2,3,4] for item in m: print(item) #类迭代 #1. 方法1 class Foo: def __iter__(self): return iter([11,22,33,44]) obj = Foo() for item in obj: print(item)
#2. 方法2
class Foo:
def __iter__(self):
yield 1#生成器
yield 2
obj = Foo()
for item in obj:#obj必须可迭代
print(item)
- super的用法 - 执行父类
#####执行父类的方法####### class C1: def f1(self): print("c1f1") class C2(C1): def f1(self): super(C2,self).f1() print("c2f1") obj = C2() obj.f1()#执行了父类和子类的f1
- 有序的字典
#######有序的字典######### class MyDict(dict): def __init__(self): self.li = [] super(MyDict,self).__init__() def __setitem__(self, key, value): #使用字典那种方法 self.li.append(key) super(MyDict, self).__setitem__(key,value) def __str__(self): #print对象时出来的结果 temp_list = [] for key in self.li: value = self.get(key) temp_list.append("'{}':{}".format(key,value)) print(temp_list) temp_str = "{" + ",".join(temp_list) + "}" return temp_str obj = MyDict() obj["k1"] = 123 obj["k2"] = 456 print(obj,type(obj))
- 单例模式
##############单例模式################### class Foo: instance = None def __init__(self,name): self.name = name @classmethod def get_instance(cls): if cls.instance: return cls.instance else: obj = cls("alex") cls.instance = obj return obj obj = Foo.get_instance()#第一次,没有instance,赋值 obj2 = Foo.get_instance()#第二次,有了,用上次赋值的那个instnce
4) 异常处理
##############异常处理############## while True: num1 = input("1") num2 = input("2") try: result = int(num1)+ int(num2) except Exception as ex: print(ex) else: pass finally: pass
- 主动触发异常
try: raise Exception("主动错误") except Exception as Ex: print(Ex)
- 断言
assert 1 == 2