python笔记3
1.类的方法
(1)普通方法:show()
class Person(object): city='西安市' def __init__(self,name,age): self.name=name self.age=age
def show(self): print('my name is {0},and my age is {1},and my city is {2}'.format(self.name,self.age,self.city))
obj=Person(name='liuxun' age=21)
obj.show()
(2)特性方法:info
class Person(object): city='西安市' def __init__(self,name,age): self.name=name self.age=age
@property def info(self): print('只读属性') obj=Person(name='liuxun',age=21) obj.info
(3)静态方法:static()
class Person(object): city='西安市' def __init__(self,name,age): self.name=name self.age=age
@staticmethod def static(): print('静态方法') obj=Person(name='liuxun',age=21) obj.static()
(4)类方法
class Person(object): city='西安市' def __init__(self,name,age): self.name=name self.age=age
@classmethod def lx(cls): print('类的方法') obj=Person(name='liuxun',age=21) obj.lx()
2.单继承实例
class Login(object): def __init__(self,username,password): self.username=username self.password=password
def login(self,nick): if self.username=='liuxun' and self.password=='0223': print('我的昵称是:{0}'.format(nick)) return 'liuxun0223' else: print('您的帐户或密码错误')
def profile(self,token): #查看主页 if token=='liuxun0223': print('显示主页信息') else: print('请登录') #动态参数,*:元组,**:字典 def func(self,*args,**kwargs): print(args,kwargs) objLogin=Login(username='liuxun',password='0223') objLogin.profile(token=objLogin.login(nick='刘勋')) objLogin.func(name='liuxun',age=21,address='xian')
3.类的继承
(1)单继承:当子类重写了父类的方法后,子类实例化后,调用重写的方法,优先考虑的是子类的方法
继承里面的第一个原则:
前提:单继承
原则:当实例化子类后,子类对象调用的方法和重写的方法重名,优先考虑的是子类的方法
class Person(object): def __init__(self,name,age): self.name=name self.age=age
def show(self): print('name is {0},and age is {1}'.format(self.name,self.age)) class Work(Person): def __init__(self,name,age,salary): #super()的方法继承父类的实例属性
方法一: super().__init__(name,age)
方法二: Person.__init__(self,name,age) self.salary = salary
def show(self): print('name is {0},and age is {1},and my salary is {2}'.format(self.name, self.age,self.salary)) objWork=Work(name='liuxun',age=21,salary=5000) objWork.show()
(2)从左到右的原则:
前提:子类继承了N个父类,子类没有重写父类的方法
执行结果:实例化子类后,调用父类的方法,它的查找顺序是从左到右,先到第一个去找,没有找到合要求的,接着去第二个查找,直到找到符合要求的
class Father(object): def show(self): print('father show') class Mother(object): def show(self): print('mother show') class Son(Father,Mother): pass son=Son() son.show()
(3)从上到下的原则:
前提:子类继承了N个父类,子类重写了父类的方法
执行结果:实例化子类后,调用具体的方法,优先考虑子类的方法
class Father(object): def show(self): print('father show') class Mother(object): def show(self): print('mother show') class Son(Father,Mother): def show(self): print('son show') son=Son() son.show()
(4)多继承实战
class Person(object): def __init__(self,name,age): self.name=name self.age=age class Student(Person): def __init__(self,name,age,score): super().__init__(name,age) self.score=score class TestWork(Student): def __init__(self,name,age,score,salary): super().__init__(name,age,score) self.salary=salary
def info(self): print('name:{0},age:{1},score:{2},salary:{3}'.format(self.name,self.age,self.score,self.salary)) obj=TestWork(name='liuxun',age=21,score=89,salary=5000) obj.info()
4.类的实战
import json import sys class Actual(object): def out(self): username=input('请输入用户名:\n') password=input('请输入密码:\n') return username,password #处理文件可以使用json.dump()和json.load()来编码和解码JSON数据 def register(self): username,password=self.out() temp=username+'|'+password json.dump(temp,open('user.md','w')) def login(self): username,password=self.out()
#split()将json.load(open())读取到的数据转化成字符串并用'|'分隔符分离 lists=str(json.load(open('user.md'))).split('|') if username==lists[0] and password==lists[1]: return True else: print('请检查你的账户或者密码') def profile(self): lists=str(json.load(open('user.md'))).split('|') if self.login(): print('欢迎访问个人主页,我的昵称为:{0}'.format(lists[0])) def main(self):
#whIle循环语句,True是判断条件 while True:
#try...except:异常处理 try: f=int(input('1、注册 2、登录 3、查看主页 4、退出\n')) if f==1: self.register() elif f==2: self.login() elif f==3: self.profile() elif f==4:
#break:退出循环;continue:跳过该次循环 break else:continue except Exception as e:continue #搜索路径和包 if __name__ == '__main__': obj=Actual() obj.main()
5..继承的实现原理
MRO的解析顺序规则:从左到右开始查找基类,如果找到第一个匹配的属性类,就会停止查找,如果没有,那就继续查找,
直到查找到符合要求的为止。MRO其实就是通过一个C3线性化算法来实现的,它的核心思想为:
1.子类会先于父类被检查
2.多个父类会根据它们在列表中的顺序被依次检查
3.如果对下一个类存在两个合法的选择,选择第一个父类
class Person(object): def info(self): print('a')
def info(self,name): print('b',name)
def info(self,name,age): print('c',name,age) class Father(Person): pass class Son(Father): def info(self): pass print(Son.mro()) Son().info()