pythonⅥ
'''
面向对象:
站在上帝的视角,世间万物一切皆是对象,我们把拥有共同属性分为一类,这就是class的由来
'''
#类的首字母必须是大写的,所有的类都会继承obje的类,object是所有类的祖先
#类里面编写的方法,其本质上等价于函数式编程里编写的函数
def show():
print('this is a pug')
class Person(object):
def show(self):
print('this is a class')
show()
'''
类里面的方法怎么调用
1、首先对类进行实例化处理
2、使用实例后的对象来调用处理类里面的方法
'''
obj=Person()
obj.show()

'''
方法执行的顺序:
1、限制性__init__方法
2、执行具体调用的方法
3、最后执行__del__方法
'''
class Person(object):
#数据属性
#全局变量
city='xian'
#初始化(针对类的初始化)
#初始化方法
def __init__(self,name,age):#属性(name,age)
#实例化属性的过程
self.name=name
self.age=age
print('开始执行')
def show(self,sex='boy'):
print('my name is {name},and my age is {age},所在城市{city},sex is {sex}'.format(name=self.name,age=self.age,city=self.city,sex=sex))
#析构方法
#清理
def __del__(self):
print('执行结束')
def info(self,**kwargs):
return kwargs
'''
数据属性属于类
实例属性属于对象
'''
#类实例化的过程也是针对类里面的__inot__方法初始化的过程
obj=Person(name='xc',age=18)
obj.show()
print(obj.city)
print(obj.info(name='xc',age=18,address='xian',work='test'))

class Person(object):
def show(self):
print('这是一个普通方法')
@property
def info(self):
print('特性方法是只读属性')
def login(self,username,password):
if username=='xc' and password=='xz':
return True
else:
return False
def profile(self,username,password):
if self.login(username=username,password=password):
print('访问个人主业成功')
obj=Person()
obj.show()
obj.info
obj.profile(username='xc',password='xz')

#封装:是通过数据属性以及实例属性来体现的
class Person(object):
def __init__(self,name,age):
self.name=name
self.age=age
def getName(self):
return self.name
def getAge(self):
return self.age
def setName(self,name):
if len(name)>2 and len(name)<6:
self.name=name
else:
print('名称长度不符合设置长度')
def setAge(self,age):
if age > 20 and age < 30:
self.age = age
else:
print('年轻人')
def show(self):
print(self.name,self.age)
obj=Person(name='xz',age=25)
print(obj.getName())
obj.setName(name='xc')
print(obj.getName())
obj.setAge(age=33)
print(obj.getAge())

面向对象的三大特性:
1、封装
2、继承
3、多态
所以在Python中,基于MRO的解析顺序规则,就会从左到右开始查找基类,如果找到第⼀个匹配的属性类,就会停⽌查找,如果没有,那就继续查找,直到查找到符合要求的为⽌。MRO其实就是通过⼀个C3线性化算法来实现的,它的核⼼思想为:
⼦类会优先于⽗类检查
多个⽗类会根据它们在列表中的顺序被依次检查
如果对下⼀个类存在两个合法的选择,只能选择第⼀个
把Python2的类叫经典类,Python3的类叫新式类,其实在Python2中,多个类的继承算
法叫深度优先,⽽Python3叫⼴度优先。
'''
基类(父类):被继承的类,子类继承父类后,会继承父类里面所有的属性(数据属性&实例属性)以及方法
派生类(子类):继承其他的类,子类继承父类后,能够继承父类里面所有的方法以及属性,当然也可以调用
继承的N个原则:
1、子类可以继承N个父类,但是建议不要超过2个,最多2个
2、上下原则---》如果子类没有重写父类的方法,那么使用的是上下原则
A、如果子类没重写父类的方法,调用父类的方法,输出的结果是父类里面方法的输出结果
B、如果子类重写了父类里面的方法,那么调用该重写的方法,输出的结果是子类里面方法的输出结果
3、从左到右原则:一个子类继承多个父类,那么调用方法后,从左到右按线性的方式来寻找
方法重写:子类重写了父类里面的方法,重写的目的是父类的方法没有办法满足子类的需求,那么就会重写
'''
class Person(object):
city='xian'
def __init__(self,name,age):
self.name=name
self.age=age
def show(self):
print('my name is {name},my age is {age}'.format(name=self.name,age=self.age))
class Student(Person):
def __init__(self,name,age,score):
#子类继承父类的实例属性
# Person.__init__(self,name,age)
#super()继承
super().__init__(name,age)
self.score=score
def info(self):
print('my name is {name},my age is {age},my score is {score},city is {city}'.format(
name=self.name, age=self.age,score=self.score,city=self.city))
print('子类调用父类的方法输出结果;/n')
self.show()
stu=Student('xw',18,81)
stu.info()
stu.show()
print(stu.city)
#按照线性方式寻找,先继承A
class A(object):
def show(self):
print('this is A')
class B(object):
def show(self):
print('this is B')
class C(A,B):
pass
c=C()
c.show()
#先继承B,如果B中没有再继承A
class A(object):
def show(self):
print('this is A')
class B(A):
def show(self):
print('this is B')
#C不能直接继承A
class C(B):
pass
c=C()
c.show()
class A(object):
def show(self):
print('this is A')
class B(A):
pass
class C(A):
def show(self):
print('C')
#C不能直接继承A
class D(B,C):
pass
d=D()
print(D.mro())

多态的优势具体可以总结为如下⼏点,具体为:
增加了持续的灵活性
增加了持续的额外扩展的功能
在Python⾥⾯,所谓鸭⼦类型就是指“如果看起来像,叫声像⽽且⾛路起来像鸭⼦,那么它就是鸭⼦”。
'''
python中一切皆对象(变量,函数,类)
'''
name='xw'
def func():
print('hello world')
class Person(object):
def show(self):
print('this is A')
obj1=name
print(obj1)
obj2=func
obj2()
obj3=Person
obj3().show()

class Animal(object):
def talk(self):
print("动物会叫")
class Cat(Animal):
def talk(self):
print('猫也会叫')
class Dog(Animal):
def talk(self):
print('狗也会叫')
def func(animal):
animal.talk()
cat=Cat()
func(animal=cat)
# cat.talk()

注册登录的改造
import json
class Main(object):
#函数的分离
def out(self):
username = input('请输入账户:\n')
password = input('请输入密码:\n')
return username,password
def register(self):
username,password=self.out()
temp=username+'|'+ password
json.dump(temp,open('user.md','w'))
# print(register())
# register()
def login(self):
username, password = self.out()
lists=str(json.load(open('user.md','r'))).split('|')
if username==lists[0] and password==lists[1]:
return True
else:
return False
# print(login())
def profile(self):
lists = str(json.load(open('user.md', 'r'))).split('|')
if self.login():
print('{nick}登陆成功'.format(nick=lists[0]))
else:
print('账号或密码错误')
# profile()
def main(self):
while True:
try:
f=int(input('1‘登录 2,个人主页 3、退出 \n'))
if f==1:
self.register()
elif f==2:
self.profile()
elif f==3:
break
except:
continue
# main()
#主函数,程序执行的入口
if __name__ == '__main__':
obj=Main()
obj.main()

浙公网安备 33010602011771号