Python--基础2
2018-07-03 01:11:04来源:博客园 阅读 ()
class Ball:
#def setname(self,name):
def __init__(self,name):
self.name = name
def __kick(self): #__:私有
print('我叫%s,谁踢我'%self.name)
>>> tt = Ball()
>>> tt.kick('西瓜')
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
tt.kick('西瓜')
TypeError: kick() takes 1 positional argument but 2 were given
>>> tt.setname('nihao')
>>> tt.kick
<bound method Ball.kick of <__main__.Ball object at 0x000002146E286898>>
>>> tt.kick()
我叫nihao,谁踢我
>>> tt._Ball__kick
<bound method Ball.__kick of <__main__.Ball object at 0x000001B6B2C36898>>
>>> tt._Ball__kick()
我叫nihaoya,谁踢我
def hello(self):
print('你好,这是在运行父程序')
pass
def __init__(self):
self.x = r.randint(0,10)
self.y = r.randint(0,10)
def move(self):
self.x -= 1
print('现在的位置是:',(self.x,self.y))
class Goldfish(Fish):
pass
class Shtingfish(Fish):
pass
def __init__(self): #重写了__init__(self)方法,新的__init__方法里没有初始化鲨鱼的x,y坐标
super().__init__() #super__init__()方法,先调用了Fish.__init__()方法
self.hungry = True
def eat(self):
if self.hungry:
print('好香,在吃会')
self.hungry = False
else:
print('不能吃了')
>>> sharkfish = Shark()
>>> sharkfish.move() ##重写了__init__(self)方法,新的__init__方法里没有初始化鲨鱼的x,y坐标
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
sharkfish.move()
File "C:\Users\tpuser\Desktop\python_20180328\复习第二个.py", line 42, in move
self.x -= 1
AttributeError: 'Shark' object has no attribute 'x'
>>> sharkfish.eat()
好香,在吃会
>>> sharkfish.eat()
>>> sharkfish = Shark()
>>> sharkfish.eat()
好香,在吃会
>>> sharkfish.move()
现在的位置是: (8, 9)
'''
#多重继承
class Base1:
def fool(self):
print('1')
class Base2:
def fool(self):
print('2')
'''
#多重继承
class Base1:
def foo1(self):
print('1')
class Base2:
def foo2(self):
print('2')
pass
>>> bb.foo1()
1
>>> bb.foo2()
2
class Lala:
def __init__(self,x): #self 表示类class本身: Lala
self.num= x
class Eilinge:
def __init__(self,x,):
self.num = x
class Family(Lala,Eilinge):
def __init__(self,x,y):
self.lala= Lala(x)
self.eilinge = Eilinge(y)
def love(self):
#print(type(self.lala.num))
print(self.lala.num+' i love you'+self.eilinge.num)
class BB:
def print1():
print('hhaha')
>>> BB.print1()
hhaha
>>> cc= BB()
>>> cc.print1()
Traceback (most recent call last):
File "<pyshell#69>", line 1, in <module>
cc.print1()
TypeError: print1() takes 0 positional arguments but 1 was given
mappingproxy({'__module__': '__main__', '__init__': <function Family.__init__ at 0x0000019EF2E528C8>,
'love': <function Family.love at 0x0000019EF2E52950>, '__doc__': None})
'''
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:pythonday1
- python3基础之“术语表(2)” 2019-08-13
- python_0基础开始_day07 2019-08-13
- 【Python】语法基础 | 开始使用Python 2019-08-13
- Python基础总结之初步认识---class类的继承(终)。第十六天 2019-08-13
- python3基础之“小练习(3)” 2019-08-13
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash