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 课程文 ...
随机推荐
- Beautiful Soup教程 转
Python中使用Beautiful Soup库的超详细教程 转 http://www.jb51.net/article/65287.htm 作者:崔庆才 字体:[增加 减小] 类型:转载 时间:20 ...
- org.hibernate.LazyInitializationException: could not initialize proxy - no Session
原因:在延迟加载的状态下,使用某个属性时,但session已经关闭. 解决方法: 1.把load改成get,直接加载所有属性. 2.获取对象进行一次判断,如果对象没有初始化,就进行一次初始化. if ...
- SQL Server数据库(SQL Sever语言 CRUD)
使用SQL Sever语言进行数据库的操作 常用关键字identity 自增长primary key 主键unique 唯一键not null 非空references 外键(引用) 在使用查询操作数 ...
- Swing——JFrame
1.定义 相对于AWT(hevay weight component),Swing(hevay weight component)是轻量化的组件.Swing由纯Java Code 所写,解决了Java ...
- Eclipse 反编译器
Help-->Install New SoftWare 贴上反编译地址:http://opensource.cpupk.com/decompiler/update/ 选择add,一路向北,起飞.
- JDE修改Grid列样式
Set Grid Color :to change the background color of a cell, row, column, or the entire control. Set Gr ...
- JDE报表开发笔记(R5537011 收货校验统计表)
业务场景:根据批次收货,收货后对该批次产品进行检验,记录检验结果生成统计表. 涉及表:主表F37011,业务从表F43121/F4101/F4108 ------------------------- ...
- CentOS 下的MySQL配置
先贴出代码(/etc/my.cnf)如下: #The following options will be passed to all MySQL clients [client] #password ...
- Wcf Client 异常和关闭的通用处理方法
在项目中采用wcf通讯,客户端很多地方调用服务,需要统一的处理超时和通讯异常以及关闭连接. 1.调用尝试和异常捕获 首先,项目中添加一个通用类ServiceDelegate.cs public del ...
- Java 面向对象编程——第一章 初识Java
第一章 初识Java 1. 什么是Java? Java是一种简单的.面向对象的.分布式的.解释的.安全的.可移植的.性能优异的多线程语言.它以其强安全性.平台无关性.硬件结构无关性.语言简 ...
