方法return返回值
class 类名:
def 方法名(self,arg):
print(arg)
return 1
中间人 = 类名()
ret = 中间人.方法名(1)
print(ret)
#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:object_oriented.py
# time:2021/02/19
class Bar:
def foo(self,name, age, gender, content):
print(name, age, gender, content)
return 1
obj = Bar()
ret = obj.foo('小明','10岁','男','上山去砍柴')
print(ret)
ret = obj.foo('小明','10岁','男','开车去东北')
print(ret)
ret = obj.foo('小明','10岁','男','最爱大保健')
print(ret)
ret = obj.foo('老李','90岁','男','上山去砍柴')
print(ret)
ret = obj.foo('老李','90岁','男','开车去东北')
print(ret)
ret = obj.foo('老李','90岁','男','最爱大保健')
print(ret)
/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/object_oriented.py
小明 10岁 男 上山去砍柴
1
小明 10岁 男 开车去东北
1
小明 10岁 男 最爱大保健
1
老李 90岁 男 上山去砍柴
1
老李 90岁 男 开车去东北
1
老李 90岁 男 最爱大保健
1
Process finished with exit code 0
函数编程VS面向对象编程
#第一次
函数编程
#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:object_oriented.py
# time:2021/02/19
def foo(name,age,gender,content):
print(name,age,gender,content)
foo('小明','10岁','男','上山去砍柴')
foo('小明','10岁','男','开车去东北')
foo('小明','10岁','男','最爱大保健')
foo('老李','90岁','男','上山去砍柴')
foo('老李','90岁','男','开车去东北')
foo('老李','90岁','男','最爱大保健')
面向对象编程
#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:object_oriented.py
# time:2021/02/19
class Bar:
def foo(self,name, age, gender, content):
print(name, age, gender, content)
obj = Bar()
obj.foo('小明','10岁','男','上山去砍柴')
obj.foo('小明','10岁','男','开车去东北')
obj.foo('小明','10岁','男','最爱大保健')
obj.foo('老李','90岁','男','上山去砍柴')
obj.foo('老李','90岁','男','开车去东北')
obj.foo('老李','90岁','男','最爱大保健')
# self
self代指,调用方法的对象(中间人)
字符串
s1 = ""
s1 = str() #str其实是个类
s1.upper() #upper是str类中的方法
#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:object_oriented.py
# time:2021/02/19
class Bar:
def foo(self,arg):
print(self,arg)
z1 = Bar()
print(z1)
/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/object_oriented.py
<__main__.Bar object at 0x7f075449f3d0>
Process finished with exit code 0
#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:object_oriented.py
# time:2021/02/19
class Bar:
def foo(self,arg):
print(self,arg)
z1 = Bar()
print(z1)
z1.foo(111)
/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/object_oriented.py
<__main__.Bar object at 0x7f09712973d0>
<__main__.Bar object at 0x7f09712973d0> 111
Process finished with exit code 0
#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:object_oriented.py
# time:2021/02/19
class Bar:
def foo(self,arg):
print(self,arg)
z1 = Bar()
print(z1)
z1.foo(111)
print('======================================')
z2 = Bar()
print(z2)
z2.foo(666)
/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/object_oriented.py
<__main__.Bar object at 0x7f0e743663d0>
<__main__.Bar object at 0x7f0e743663d0> 111
======================================
<__main__.Bar object at 0x7f0e7439c550>
<__main__.Bar object at 0x7f0e7439c550> 666
Process finished with exit code 0
给self中间人传参数
#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:object_oriented.py
# time:2021/02/19
class Bar:
def foo(self,arg):
print(self,self.name,arg)
z = Bar()
z.name = 'smoke'
z.foo(666)
/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/object_oriented.py
<__main__.Bar object at 0x7f60a043d3d0> smoke 666
Process finished with exit code 0
#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:object_oriented.py
# time:2021/02/19
class Bar:
def foo(self,arg):
print(self,self.name,self.age,self.gender,arg)
z = Bar()
z.name = 'smoke'
z.age = 84
z.gender = 'zhong'
z.foo(666)
/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/object_oriented.py
<__main__.Bar object at 0x7f080904f3d0> smoke 84 zhong 666
Process finished with exit code 0
#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author:smoke
# file:object_oriented.py
# time:2021/02/19
class Bar:
def foo(self,arg):
print(self,self.name,self.age,self.gender,arg)
z = Bar()
z.name = 'smoke'
z.age = 84
z.gender = 'zhong'
z.foo(666)
z1 = Bar()
z1.name = 'cherry'
z1.age = 73
z1.gender = 'nv'
z1.foo(666)
/usr/bin/python3.8 /home/smoke/PycharmProjects/pythonProject/lean_python/object_oriented.py
<__main__.Bar object at 0x7fa2fb6123d0> smoke 84 zhong 666
<__main__.Bar object at 0x7fa2fb648550> cherry 73 nv 666
Process finished with exit code 0
浙公网安备 33010602011771号