第七章 面向对象
7.1 面向对象的基本格式
class 类:
def 方法名(self,name)
print(name)
return 123
def 方法名(self,name)
print(name)
return 123
def 方法名(self,name)
print(name)
return 123
#调用类中的函数
1.创建改类的对象
obj = 类名()
2.对象调用方法
resut = obj.方法名('liujia')
print(result)
应用场景:遇到很多函数,需要给函数进行分类规划【封装】
7.2对象的作用
存储一些值,方便以后调用
class File:
def read(self):
with open(self.xxx,mode='r',encoding='utf-8') as f:
data = read()
return data
def write(self,content):
with open(self.xxx,mode='w',encoding='uttf-8') as f:
f.write(content)
#示例化一个File类的对象
obj1 = File()
#在对象中写了一个xxxx = 'test.log'
obj1.xxx = 'test.log'
#通过对象调用类中的read方法,read方法中的self就是obj
obj1.read()
obj1.write('liujia')
#实例化了一个File类的对象
obj2 = File()
obj2.xxxxxx = 'info.txt'
obj.read()
obj2.write('liujia')
class Person:
def show(self):
temp = "我是%s,今年%s岁,性别%s" %(self.name,self.age,self.gender)
print(temp)
obj = Person()
obj.name = 'liujia'
obj.age = 2
obj.gender = 'boy'
obj.show()
class Person:
def __init__(self,n,a,g)#初始化方法(构造方法),给对象的内部做初始化
self.name = n
self.age = a
self.gender = g
def show(self):
temp = "我是%s,今年%s岁,性别%s" %(self.name,self.age,self.gender)
print(temp)
#类()实例化对象时,会自动执行此类的__init__方法
obj = Person('lijia',2,'boy')
obj.show()
总结:将数据封装到对象中,方便使用
"""
如果写代码时,函数比较多比较乱。
1. 可以将函数归类并放到同一个类中。
2. 函数如果有一个反复使用的公共值,则可以放到对象中。
"""
class File:
def __init__(self,path):
self.file_path = path
def read(self):
print(self.file_path)
def write(self,content):
print(self.file_path)
def delete(self):
print(self.file_path)
def update(self):
print(self.file_path)
p1 = File('log.txt')
p1.read()
p2 = File('xxxxxx.txt')
p2.read()
#1.循环让用户输入:用户名/密码/邮箱。输入完成后再进行数据打印。
USER_LIST = []
class User:
def __init__(self,name,pwd,email):
self.name = name
self.pwd = pwd
self.email = email
while True:
name = input('请输入用户名:')
pwd = input('请输入密码:')
email = input('请输入邮箱:')
p = Person(name,pwd,email)
USER_LIST.append(p)
for item in USER_LIST:
temp = "我的名字:%s,密码:%s,邮箱%s" %(item.name,item.password,item.email,)
print(temp)
示例:游戏开发
class Police:
def __init__(self,name)
self.name = name
self.hp = 10000
def tax(self):
msg = "%s收了个税。" %(self.name,)
print(msg)
def fight(self):
msg = "%s去战了个斗。" %(self.name,)
lsq = Police('李邵奇')
zzh = Police('渣渣会')
tyg = Police('堂有光')
class Bandit:
def __init__(self,nickname)
self.nickname = nickname
self.hp = 1000
def murder(self,name):
msg = "%s去谋杀了%s" %(self.nickname, name,)
lcj = Bandit('二蛋')
lp = Bandit('二狗')
zsd = Bandit('狗蛋')
# 1. 二狗去谋杀渣渣会,二狗生命值-100; 渣渣会生命值减5000
lp.murder(zzh.name)
lp.hp = lp.hp - 100
zzh.hp = zzh.hp - 5000
# ...
class Police:
def __init__(self,name)
self.name = name
self.hp = 10000
def dao(self,other):
msg = "%s个了%s一刀。" %(self.name,other.nickname)
self.hp = self.hp - 10
other.hp = other.hp - 50
print(msg)
def qiang(self):
msg = "%s去战了个斗。" %(self.name,)
def quan(self,other):
msg = "%s个了%s一全。" %(self.name,other.nickname)
self.hp = self.hp - 2
other.hp = other.hp - 10
print(msg)
class Bandit:
def __init__(self,nickname)
self.nickname = nickname
self.hp = 1000
def qiang(self,other):
msg = "%s个了%s一全。" %(self.nickname,other.name)
self.hp -= 20
other.hp -= 500
lcj = Bandit('二蛋')
lsq = Police('李邵奇')
lsq.dao(lcj)
lsq.quan(lcj)
lcj.qiang(lsq)
7.3 继承
#父类(基类)
class Base:
def f1(self):
pass
#子类(派生类)
class Foo(Base):
def f2(self):
pass
#创建一个子类的对象
obj = FOO()
#执行对象方法时,有限在自己的类中找,如果没有就去父类中找
obj.f2()
obj.f1()
问题:什么时候才能用到继承?多个类中如果有公共的方法,可以放到基类中避免重负编写
class Base:
def f1(self):
pass
class Foo(Base):
def f2(self):
pass
class Bar(Base):
def f3(self):
pass
obj1 = Foo()
obj2 = Bar()
继承关系中查找方法的顺序:
# 示例一
class Base:
def f1(self):
print('base.f1')
class Foo(Base):
def f2(self):
print('foo.f2')
obj = Foo()
obj.f1()
obj.f2()
# 示例二
class Base:
def f1(self):
print('base.f1')
class Foo(Base):
def f2(self):
self.f1()
print('foo.f2')
obj = Foo()
obj.f2()
# 示例三
class Base:
def f1(self):
print('base.f1')
class Foo(Base):
def f2(self):
self.f1()
print('foo.f2')
def f1(self):
print('foo.f1')
obj = Foo()
obj.f2()
# 示例四
class Base:
def f1(self):
self.f2()
print('base.f1')
def f2(self):
print('base.f2')
class Foo(Base):
def f2(self):
print('foo.f2')
obj = Foo()
obj.f1()
# 示例五
class TCPServer:
pass
class ThreadingMixIn:
pass
class ThreadingTCPServer(ThreadingMixIn, TCPServer):
pass
# 示例六
class BaseServer:
def serve_forever(self, poll_interval=0.5):
self._handle_request_noblock()
def _handle_request_noblock(self):
self.process_request(request, client_address)
def process_request(self, request, client_address):
pass
class TCPServer(BaseServer):
pass
class ThreadingMixIn:
def process_request(self, request, client_address):
pass
class ThreadingTCPServer(ThreadingMixIn, TCPServer):
pass