zoukankan      html  css  js  c++  java
  • Python--基础2

    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	puserDesktoppython_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 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})
  • 相关阅读:
    html php插入百度地图定位
    thinkphp验证功能(部分)
    thinkphp用ajax注册及检测个人见解
    文件系统处理_下
    文件系统处理
    jQuery ajax
    jquery(复选框全选)
    jquery(鼠标)
    找房子(数据库应用)
    php基础题
  • 原文地址:https://www.cnblogs.com/eilinge/p/9239809.html
Copyright © 2011-2022 走看看