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 课程文 ...
随机推荐
- 使用 Fresco加载图片
概念: ImagePipeline ——负责从网络.本地图片.Content Provider(内容提供者)或者本地资源那里获取图片,压缩保存在本地存储中和在内存中保存为压缩的图片 Drawee——处 ...
- Farseer.Net
Farseer.Net V0.2 ORM开源框架 目录 http://www.cnblogs.com/steden/archive/2013/01/22/2871160.html V1.0教程:htt ...
- Check the difficulty of problems(POJ 2151)
Check the difficulty of problems Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5457 ...
- Linux Mysql 1130错误解决
今天在win32下通过navicat 远程登录Mysql时出现如下错误: 想都不用想,肯定是Mysql的访问权限问题. 首先,通过终端(我用的是SSH)远程登录到Linux服务器,为了 ...
- mongodb 和 mysql 的对照
In addition to the charts that follow, you might want to consider the Frequently Asked Questions sec ...
- 执行MAVEN更新包
我们一般使用 mvn eclipse:eclipse 执行对maven库的引用,这样会修改项目下的classpath文件. 我们修改直接在eclipse 使用maven库作为项目的引用. 步骤如下: ...
- PHP FTP操作类( 上传、拷贝、移动、删除文件/创建目录 )
/** * 作用:FTP操作类( 拷贝.移动.删除文件/创建目录 ) * 时间:2006/5/9 * 作者:欣然随风 * QQ:276624915 */ class class_ftp { publi ...
- java 面向对象编程--第17章 I/O系统
1.I/O操作指的是输入和输出流的操作.相对内存而言,当我们从数据源中将数据读取到内存中,就是输入流,也叫读取流.当我们将内存中处理好的数据写入数据源,就是输出流,也叫写入流. 2.流按照内容分类:字 ...
- Android ClearEditText:输入用户名、密码错误时整体删除及输入为空时候晃动提示
package com.lixu.clearedittext; import android.app.Activity; import android.os.Bundle; import androi ...
- sql语句查询重复的数据
查找所有重复标题的记录: SELECT *FROM t_info aWHERE ((SELECT COUNT(*)FROM t_infoWHERE Title = a.Title) > 1)OR ...
