Python _ 开始介绍对象
Python的私有变量,函数是在前面加上一个双下划线'__'来声明的,气访问和C++大同小异
例如
class Person:
__name='小甲鱼'
def print1(self): # 和 c++ 相同 私有变量只能本类之内访问 .
return self.__name
nam=Person()
print(nam.print1())
print(nam._Person__name)
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 ===============
池塘里面有乌龟 个 池塘里面有鱼 个 >>>
Python _ 开始介绍对象的更多相关文章
- Day1 - Python基础1 介绍、基本语法、流程控制
Python之路,Day1 - Python基础1 本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼 ...
- Python基础1 介绍、基本语法 、 流程控制-DAY1
本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语 ...
- 《python解释器源码剖析》第4章--python中的list对象
4.0 序 python中的list对象,底层对应的则是PyListObject.如果你熟悉C++,那么会很容易和C++中的list联系起来.但实际上,这个C++中的list大相径庭,反而和STL中的 ...
- python 11 类与对象
给大家介绍对象hhhh 封装 举个例子,把乱七八糟的数据仍到列表,数据层面的封装 把常用的代码打包成一个函数,封装 外观特征(静态) 能做什么(动态) 对象=属性(静态) + 方法(动态) 1.方法多 ...
- Python学习笔记—Python基础1 介绍、发展史、安装、基本语法
第一周学习笔记: 一.Python介绍 1.Python的创始人为吉多·范罗苏姆.1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言 ...
- python _、__和__xx__的区别
python _.__和__xx__的区别 本文为译文,版权属于原作者,在此翻译为中文分享给大家.英文原文地址:Difference between _, __ and __xx__ in Pytho ...
- Python内建的对象列表
Python内建的对象列表 刚写Python肯定会遇到这样的情况,想写些什么,但又不知从何写起... 在我看来问题在于我们不知道有什么东东可以拿来玩,这里列出Python的内建对象,稍微归类了一下,多 ...
- 类和对象:给大家介绍对象 - 零基础入门学习Python036
类和对象:给大家介绍对象 让编程改变世界 Change the world by program 我们之前说过Python无处不对象,Python到处都是对象,然后你会发现很多童鞋其实并不知道对象是什 ...
- Python 科学计算-介绍
Python 科学计算 作者 J.R. Johansson (robert@riken.jp) http://dml.riken.jp/~rob/ 最新版本的 IPython notebook 课程文 ...
随机推荐
- JSON 换行、JSON \r\n、怎么处理 ?(转载)
参考地址: http://www.cnblogs.com/mamingbo/archive/2010/11/27/1889583.html 在最后json的字符串上:.Replace("\r ...
- DataTable 中Distinct操作
DataTable dt = ds.Tables[]; DataView dataView = dt.DefaultView; DataTable dtDistinct = dataView.ToTa ...
- PHP获取当前页面的URL作为参数以供下一层的页面可以返回上一层页面
1.基础url的获取 #测试网址: http://localhost/blog/testurl.php?id=5 //获取域名或主机地址 echo $_SERVER['HTTP_HOST'].&quo ...
- B+树索引和哈希索引的区别——我在想全文搜索引擎为啥不用hash索引而非得使用B+呢?
哈希文件也称为散列文件,是利用哈希存储方式组织的文件,亦称为直接存取文件.它类似于哈希表,即根据文件中关键字的特点,设计一个哈希函数和处理冲突的方法,将记录哈希到存储设备上. 在哈希文件中,是使用一个 ...
- 【北京站】详解Visual Studio 2013:开发iOS及android应用!现场图集
现场图集: 活动介绍地址:http://huiyi.csdn.net/module/meeting/meeting/info/660/biz
- Internet Explorer已限制此网页运行可以访问计算机的脚本或ActiveX控件
在制作网页的时候,大家不免要用到script,也即是脚本,主要是VBScript以及JavaScript.那么时常遇到这样的情况: 在本地双击打开html文件时,如果是IE的话,会出现提示框(如下图) ...
- bzoj 2661: [BeiJing wc2012]连连看
#include<cstdio> #include<iostream> #include<cstring> #include<cmath> #inclu ...
- ROS语音识别
一.语音识别包 1.安装 安装很简单,直接使用ubuntu命令即可,首先安装依赖库: $ sudo apt-get install gstreamer0.10-pocketsphinx ...
- Apache—DBUtils
简介 commons-dbutils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,学习成本极低,并且使用dbutils能极大简化jdbc编码的工作量,同时也不会影 ...
- 自动登录 登陆成功那个alert遮盖一直存在bug
手动登陆的时候,登陆成功MBProgressHUD message:@"登陆成功" 然后再dispatch_after 里调用MBProgressHUD hideHUD隐藏可以成功 ...
