Python--基础2

2018-07-03 01:11:04来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

'''
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('nihaoya')
>>> tt._Ball__kick
<bound method Ball.__kick of <__main__.Ball object at 0x000001B6B2C36898>>
>>> tt._Ball__kick()
我叫nihaoya,谁踢我
 
class Father1:
    def hello(self):
        print('你好,这是在运行父程序')
 
class Chilrd(Father1):
    pass
 
import random as r
 
class Fish:
    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
 
class Shark(Fish):
    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()
 
#super.__init__()   :super__init__()方法,先调用了Fish.__init__()方法
>>> 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')
 
class Bss(Base1,Base2):
    pass
 
>>> bb= Bss()
>>> 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
 
Family.__dict__
mappingproxy({'__module__': '__main__', '__init__': <function Family.__init__ at 0x0000019EF2E528C8>,
              'love': <function Family.love at 0x0000019EF2E52950>, '__doc__': None})
'''

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:pythonday1

下一篇:Django框架的使用教程--路由-请求-响应[四]