Python--基础2
class Ball:
#def setname(self,name):
def __init__(self,name):
self.name = name
def __kick(self): #__:私有
print('我叫%s,谁踢我'%self.name) >>> tt = Ball()
>>> tt.kick('西瓜')
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
tt.kick('西瓜')
TypeError: kick() takes 1 positional argument but 2 were given
>>> tt.setname('nihao')
>>> tt.kick
<bound method Ball.kick of <__main__.Ball object at 0x000002146E286898>>
>>> tt.kick()
我叫nihao,谁踢我 >>> tt = Ball('nihaoya')
>>> tt._Ball__kick
<bound method Ball.__kick of <__main__.Ball object at 0x000001B6B2C36898>>
>>> tt._Ball__kick()
我叫nihaoya,谁踢我
class Father1:
def hello(self):
print('你好,这是在运行父程序') class Chilrd(Father1):
pass 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 Shtingfish(Fish):
pass class Shark(Fish):
def __init__(self): #重写了__init__(self)方法,新的__init__方法里没有初始化鲨鱼的x,y坐标
super().__init__() #super__init__()方法,先调用了Fish.__init__()方法
self.hungry = True
def eat(self):
if self.hungry:
print('好香,在吃会')
self.hungry = False
else:
print('不能吃了')
>>> sharkfish = Shark()
>>> sharkfish.move() ##重写了__init__(self)方法,新的__init__方法里没有初始化鲨鱼的x,y坐标
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
sharkfish.move()
File "C:\Users\tpuser\Desktop\python_20180328\复习第二个.py", line 42, in move
self.x -= 1
AttributeError: 'Shark' object has no attribute 'x'
>>> sharkfish.eat()
好香,在吃会
>>> sharkfish.eat() #super.__init__() :super__init__()方法,先调用了Fish.__init__()方法
>>> sharkfish = Shark()
>>> sharkfish.eat()
好香,在吃会
>>> sharkfish.move()
现在的位置是: (8, 9)
#多重继承
class Base1:
def foo1(self):
print('1')
class Base2:
def foo2(self):
print('2') class Bss(Base1,Base2):
pass >>> bb= Bss()
>>> bb.foo1()
1
>>> bb.foo2()
2 #组合
class Lala:
def __init__(self,x): #self 表示类class本身: Lala
self.num= x
class Eilinge:
def __init__(self,x,):
self.num = x
class Family(Lala,Eilinge):
def __init__(self,x,y):
self.lala= Lala(x)
self.eilinge = Eilinge(y)
def love(self):
#print(type(self.lala.num))
print(self.lala.num+' i love you'+self.eilinge.num)
class BB:
def print1():
print('hhaha')
>>> BB.print1()
hhaha
>>> cc= BB()
>>> cc.print1()
Traceback (most recent call last):
File "<pyshell#69>", line 1, in <module>
cc.print1()
TypeError: print1() takes 0 positional arguments but 1 was given Family.__dict__
mappingproxy({'__module__': '__main__', '__init__': <function Family.__init__ at 0x0000019EF2E528C8>,
'love': <function Family.love at 0x0000019EF2E52950>, '__doc__': None})
Python--基础2的更多相关文章
- python之最强王者(2)——python基础语法
背景介绍:由于本人一直做java开发,也是从txt开始写hello,world,使用javac命令编译,一直到使用myeclipse,其中的道理和辛酸都懂(请容许我擦干眼角的泪水),所以对于pytho ...
- Python开发【第二篇】:Python基础知识
Python基础知识 一.初识基本数据类型 类型: int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2147483648-2147483647 在64位 ...
- Python小白的发展之路之Python基础(一)
Python基础部分1: 1.Python简介 2.Python 2 or 3,两者的主要区别 3.Python解释器 4.安装Python 5.第一个Python程序 Hello World 6.P ...
- Python之路3【第一篇】Python基础
本节内容 Python简介 Python安装 第一个Python程序 编程语言的分类 Python简介 1.Python的由来 python的创始人为吉多·范罗苏姆(Guido van Rossum) ...
- 进击的Python【第三章】:Python基础(三)
Python基础(三) 本章内容 集合的概念与操作 文件的操作 函数的特点与用法 参数与局部变量 return返回值的概念 递归的基本含义 函数式编程介绍 高阶函数的概念 一.集合的概念与操作 集合( ...
- 进击的Python【第二章】:Python基础(二)
Python基础(二) 本章内容 数据类型 数据运算 列表与元组的基本操作 字典的基本操作 字符编码与转码 模块初探 练习:购物车程序 一.数据类型 Python有五个标准的数据类型: Numbers ...
- Python之路【第一篇】python基础
一.python开发 1.开发: 1)高级语言:python .Java .PHP. C# Go ruby c++ ===>字节码 2)低级语言:c .汇编 2.语言之间的对比: 1)py ...
- python基础之day1
Python 简介 Python是著名的“龟叔”Guido van Rossum在1989年圣诞节期间,为了打发无聊的圣诞节而编写的一个编程语言. Python为我们提供了非常完善的基础代码库,覆盖了 ...
- python基础之文件读写
python基础之文件读写 本节内容 os模块中文件以及目录的一些方法 文件的操作 目录的操作 1.os模块中文件以及目录的一些方法 python操作文件以及目录可以使用os模块的一些方法如下: 得到 ...
- python基础之编码问题
python基础之编码问题 本节内容 字符串编码问题由来 字符串编码解决方案 1.字符串编码问题由来 由于字符串编码是从ascii--->unicode--->utf-8(utf-16和u ...
随机推荐
- 解决MyEclipse报errors running builder ‘javascript validator’ on project
今天导入项目的时候,报了以下错误 MyEclipse测到功能代码变化(保存动作触发)就报错: errors running builder ‘javascript validator’ on proj ...
- elasticsearch排序-----5
我们之前查询出的结果都会有一个_score分值表示列出结果与搜索结果的相关性,该值越高排序位置越靠前,es具体是如何计算该值的,我们认真来看看. 1.根据字段值排序 比如我们要查询/index5下su ...
- vue 修饰符 整理
事件修饰符 .stop <!-- 阻止单击事件继续传播 --> <a v-on:click.stop="doThis"></a> .preven ...
- 常见O/R框架介绍
1.hibernate(JPA的一个实现,同时也有自己的特色)2.toplink3.jdo4.ibatis 4.JPA a)意愿统一天下
- selenium Element not found in the cache - perhaps the page has changed since it was looked up接解决
selenium Element not found in the cache - perhaps the page has changed since it was looked up.这个问题爆出 ...
- javascript代码工具库
1. 垃圾收集 另一个块作用域非常有用的原因和闭包及回收内存垃圾的回收机制相关.这里简要说明一 下,而内部的实现原理,也就是闭包的机制会在第 5 章详细解释. 考虑以下代码: function pro ...
- ES6相关特性(解构赋值)
解构赋值:本质上是一种匹配模式,等号两边的模式相同,则左边的变量可以被赋予对应的值. 注意:null & undefined 不能解构赋值!!! 数组的解构赋值: let [a,[[b],c] ...
- react+webpack 引入字体图标
在使用react+webpack 构建项目过程中免不了要用到字体图标,在引入过程中报错,不能识别字体图标文件中的@符,报错 Uncaught Error: Module parse failed: U ...
- make知识
makelist 语法 https://cmake.org/cmake/help/v3.10/manual/cmake-language.7.html CMakeLists.txt I am of t ...
- C语言头文件怎么写?(转载)
---恢复内容开始--- c语言头文件怎么写?我一直有这样的疑问,但是也一直没去问问到底咋回事:所以今天一定要把它弄明白! 其实学会写头文件之后可以为我们省去不少事情,可以避免书写大量的重复代码,还在 ...