Python _ 开始介绍对象

时间:2023-03-09 18:15:59
Python _  开始介绍对象

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

例如

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

Python _  开始介绍对象

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

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

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

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

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

下面进行测试 .

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

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

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

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

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

1:

 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):
Fish.__init__(self) # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
self.hungry=True
def eat(self):
if self.hungry:
print("吃货的梦想就是天天有得吃")
self.hungry=False
else:
print('撑死我 你偿命?')
 =============== RESTART: C:/Users/Administrator/Desktop/new.py ===============
>>> shark=Shark()
>>> shark.eat()
吃货的梦想就是天天有得吃
>>> shark.move
<bound method Fish.move of <__main__.Shark object at 0x02E7DA10>>
>>> shark.move()
我的位置是: 9 4

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

 

 =============== RESTART: C:/Users/Administrator/Desktop/new.py ===============
我的位置是: 2 1
>>>

 

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的话就不需要填写 父类的名字 , 它可以帮你自动寻找 .

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

 class Base1:
def foo1(self):
print('我是foo1,我为Base1代言....') class Base2:
def foo2(self):
print('我是foo2,我为foo2代言.....') class C(Base1,Base2):
pass c=C()
c.foo1()
c.foo2()
 =============== RESTART: C:/Users/Administrator/Desktop/new.py ===============
我是foo1,我为Base1代言....
我是foo2,我为foo2代言.....
>>>

汇合类

 class Turtle:
def __init__(self,x): # 在生命对象的时候 说明对象的 数量 . (还是一个对象 . 数量只是该对象的一个属性 . )
self.num=x class Fish:
def __init__(self,x):
self.num=x class Pool:
def __init__(self,x,y):
self.turtle=Turtle(x) #在该对象中定义 乌龟属性 , 该属性 为乌龟对象的实例化
self.fish=Fish(y)
def print_num(self):
print('池塘里面有乌龟 %d 个'% self.turtle.num,'\n')
print('池塘里面有鱼 %d 个'%self.fish.num,'\n')
pool=Pool(,)
pool.print_num()
 =============== RESTART: C:\Users\Administrator\Desktop\new.py ===============
池塘里面有乌龟 个 池塘里面有鱼 个 >>>