Python第一阶段06
1.面向对象编程:
class Dog: def __init__(self, name): self.name = name def bulk(self): print("%s 汪汪汪。。。"%self.name) d1 = Dog("aaa") d2 = Dog("bbb") d3 = Dog("ccc") d1.bulk() d2.bulk() d3.bulk()
2.实例变量和类变量:
类变量:大家共用的属性,节省开销;
析构函数:
3.继承:
class People(): def __init__(self,name): self.name = name def talk(self): print("talk...") def eat(self): print("eat...") class Man(People): def drink(self): print("drink...") m1 = Man("sisi") m1.drink()
4.多态:
一种接口,多种实现;
# Author:SiSi class Animal(): def __init__(self, name): self.name = name def talk(self): print("talk...") def animal_talk(obj): obj.talk() class Dog(Animal): def talk(self): print("wangwang...") class Cat(Animal): def talk(self): print("miaomiao...") d = Dog("aaa") # d.talk() d1 = Cat("bbb") # d1.talk() a = Animal("sisi") Animal.animal_talk(d) Animal.animal_talk(d1)
5.静态方法,类方法,属性方法:
静态方法只是名义上归类管,实际上在静态方法里访问不了类和实例中的任何属性和方法;
类方法只能访问类变量,不能访问实例变量;
属性方法:
# Author:SiSi class Dog(object): def __init__(self, name): self.name = name @staticmethod # 实际上和类没什么关系了 def eat(self): print("%s is eating..."%(self.name)) @classmethod def drink(self): print("我说类方法") @property def log(self): print("我是属性方法。。。") @eat.setter def log(self, food): print("log:",food) @eat.deleter def log(self): # del self._food print("删完了。。。") d = Dog("sisi") # Dog.eat() d.eat(d)
6.反射详解:
7.异常处理:
names = ['aaa', 'bbb'] data = {} try: names[3] data['name'] except {KeyError, IndexError} as e: print("没有这个key",e) except IndexError as e: print("列表操作错误",e) except Exception as e: print("未知错误",e) else: print("一切正常") finally: print("不管有错没错,我都执行")
8.Socket通信: