zoukankan      html  css  js  c++  java
  • Python _ 开始介绍对象

    Python的私有变量,函数是在前面加上一个双下划线'__'来声明的,气访问和C++大同小异

    例如

    1 class Person:
    2         __name='小甲鱼'
    3         def print1(self):   # 和 c++ 相同 私有变量只能本类之内访问 .
    4                 return self.__name
    5 nam=Person()
    6 print(nam.print1())
    7 print(nam._Person__name)

    1 class Parent:
    2     def hello(self):
    3         print('我是爸爸 .')
    4 class Child(Parent):
    5     pass
    6 parent=Parent()    # 儿子说 他是爸爸
    7 child=Child()   # 爸爸就生气了       虽然说你继承了我但是   这样就太过分了
    8 p.hello()

    如果子类定义了和 父类相同的方法或者属性 子类的会将父类的覆盖

     1 class Parent:
     2     def hello(self):
     3         print('我是爸爸 .')
     4 class Child(Parent):
     5     def hello(self):
     6         print('我是儿子')
     7 parent=Parent()    # 儿子说 他是爸爸
     8 child=Child()   # 这样还差不多 , 要有自己的发展空间么 . 
     9 parent.hello()
    10 child.hello()

    以前一直困惑的 __init__  不知道是啥东西 .  今天才知道  这就是 和C++差不多的 构造函数 (在建立对象的时候  会自动运行的函数 . )

     1 import random as r
     2 class Fish:
     3     def __init__(self):
     4         self.x=r.randint(0,10)
     5         self.y=r.randint(0,10)
     6 
     7     def move(self):
     8         self.x-=1
     9         print('我的位置是: ',self.x,self.y)
    10 
    11 class Goldfish(Fish):
    12     pass
    13 
    14 class Carp(Fish):
    15     pass
    16 
    17 class Aslmon(Fish):
    18     pass
    19 
    20 class Shark(Fish):
    21     def __init__(self):
    22         self.hungry=True
    23     def eat(self):
    24         if self.hungry:
    25             print("吃货的梦想就是天天有得吃")
    26             self.hungry=False
    27         else:
    28             print('撑死我 你偿命?')

    下面进行测试 . 

     1 Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32
     2 Type "copyright", "credits" or "license()" for more information.
     3 >>> 
     4 =============== RESTART: C:/Users/Administrator/Desktop/new.py ===============
     5 >>> fish=Fish()
     6 >>> fish.move()
     7 我的位置是:  8 8
     8 >>> fish.move()
     9 我的位置是:  7 8
    10 >>> fish.move()
    11 我的位置是:  6 8
    12 >>> goldfish=Goldfish()
    13 >>> goldfish.move()
    14 我的位置是:  7 6
    15 >>> goldfish.move()
    16 我的位置是:  6 6
    17 >>> goldfish.move()
    18 我的位置是:  5 6
    19 >>> shark=Shark()
    20 >>> shark.eat()
    21 吃货的梦想就是天天有得吃
    22 >>> shark.eat()
    23 撑死我 你偿命?
    24 >>> shark.eat()
    25 撑死我 你偿命?
    26 >>> shark.move
    27 <bound method Fish.move of <__main__.Shark object at 0x031C9FF0>>
    28 >>> shark.move()
    29 Traceback (most recent call last):
    30   File "<pyshell#13>", line 1, in <module>
    31     shark.move()
    32   File "C:/Users/Administrator/Desktop/new.py", line 8, in move
    33     self.x-=1
    34 AttributeError: 'Shark' object has no attribute 'x'
    35 >>> 

    可以看到在最后调用 shark 的 move的时候 发生了错误 . 报错说 没有 X 这个东西 . 

    咋回事呢 .  Shark 在继承Fish 类的时候 重写了 __init__  导致没有 x 和 y 这两个变量 . 

    那我们应该怎么避开这个坑呢 . ?  我们应该 在子类重写 __init__ 的时候现调用父类的 __init__ 使其同时存在  . 

    实现这种思想 一共有两种方法 .  1 : 调用未绑定的父类方法 .  2 : 使用supper 函数  .

     1:

     1 import random as r
     2 class Fish:
     3     def __init__(self):
     4         self.x=r.randint(0,10)
     5         self.y=r.randint(0,10)
     6 
     7     def move(self):
     8         self.x-=1
     9         print('我的位置是: ',self.x,self.y)
    10 
    11 class Shark(Fish):
    12     def __init__(self):
    13         Fish.__init__(self)     # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    14         self.hungry=True
    15     def eat(self):
    16         if self.hungry:
    17             print("吃货的梦想就是天天有得吃")
    18             self.hungry=False
    19         else:
    20             print('撑死我 你偿命?')
    1 =============== RESTART: C:/Users/Administrator/Desktop/new.py ===============
    2 >>> shark=Shark()
    3 >>> shark.eat()
    4 吃货的梦想就是天天有得吃
    5 >>> shark.move
    6 <bound method Fish.move of <__main__.Shark object at 0x02E7DA10>>
    7 >>> shark.move()
    8 我的位置是:  9 4
    
    
     1 import random as r
     2 class Fish:
     3     def __init__(self):
     4         self.x=r.randint(0,10)
     5         self.y=r.randint(0,10)
     6 
     7     def move(self):
     8         self.x-=1
     9         print('我的位置是: ',self.x,self.y)
    10 
    11 class Shark(Fish):
    12     def __init__(self):
    13         Fish.__init__(self)
    14         self.hungry=True
    15     def eat(self):
    16         if self.hungry:
    17             print("吃货的梦想就是天天有得吃")
    18             self.hungry=False
    19         else:
    20             print('撑死我 你偿命?')
    21 
    22 shark=Shark()
    23 shark.move()
    
    
    
     
    
    
    1 =============== RESTART: C:/Users/Administrator/Desktop/new.py ===============
    2 我的位置是:  2 1
    3 >>> 
    
    
    
     

    2 : 更加优秀的方法  就是使用supper 函数 . 

    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 Shark(Fish):
        def __init__(self):
            super().__init__()
            self.hungry=True
    
        def eat(self):
            if self.hungry:
                print("吃货的梦想就是天天有得吃")
                self.hungry=False
            else:
                print('撑死我 你偿命?')
    shark=Shark()
    shark.move()
    shark.eat()

    使用super的话就不需要填写 父类的名字 , 它可以帮你自动寻找 . 

    最后说一下多重继承把  .  多重继承 也就只是在 括号内多写几个 类名罢了 . 

     1 class Base1:
     2     def foo1(self):
     3         print('我是foo1,我为Base1代言....')
     4 
     5 class Base2:
     6     def foo2(self):
     7         print('我是foo2,我为foo2代言.....')
     8 
     9 class C(Base1,Base2):
    10     pass
    11 
    12 c=C()
    13 c.foo1()
    14 c.foo2()
    1 =============== RESTART: C:/Users/Administrator/Desktop/new.py ===============
    2 我是foo1,我为Base1代言....
    3 我是foo2,我为foo2代言.....
    4 >>> 

     汇合类

     1 class Turtle:
     2     def __init__(self,x):   # 在生命对象的时候 说明对象的 数量 . (还是一个对象 . 数量只是该对象的一个属性 . )
     3         self.num=x
     4 
     5 class Fish:
     6     def __init__(self,x):
     7         self.num=x
     8 
     9 class Pool:
    10     def __init__(self,x,y):
    11         self.turtle=Turtle(x)   #在该对象中定义 乌龟属性 , 该属性 为乌龟对象的实例化
    12         self.fish=Fish(y)
    13     def print_num(self):
    14         print('池塘里面有乌龟 %d 个'% self.turtle.num,'
    ')
    15         print('池塘里面有鱼 %d 个'%self.fish.num,'
    ')
    16 pool=Pool(1,10)
    17 pool.print_num()
    1 =============== RESTART: C:UsersAdministratorDesktopnew.py ===============
    2 池塘里面有乌龟 103 
    4 池塘里面有鱼 15 
    6 >>> 
  • 相关阅读:
    .Net里的Attribute 学习
    浅谈并发与并行(一)
    Asp.net Mvc4默认权限详细(下)
    Enum是如何用的?
    指针与引用
    栈与队列
    mini2440裸机之I2C
    解决Delphi MDI 闪烁问题(使用WM_SETREDRAW锁屏后进行处理)
    DelphiXE 显示GIF动画
    参数传递方法(Delphi1.0与win16API使用pascal方法,即从左到右)
  • 原文地址:https://www.cnblogs.com/A-FM/p/5671795.html
Copyright © 2011-2022 走看看