Python Base Five
// 8 day(2016/8/11)
38. In python , it is oop.
class Baskball:
def setName(self, name):
self.name = name
def kick(self):
print('my name is %s' % self.name)
baskball = Baskball()
baskball.setName('baskball')
baskball.kick()
-> my name is baskball
class Ball:
def __init__(self, name):
self.name = name
def kick(self):
print('my name is %s' % self.name)
b = Ball('tom')
b.kick()
-> my name is tom
39. In python ,how to define private variable,
such as:
class Person:
name = 'roy'
p = Person()
print(p.name)
-> roy
if you use:
class Person:
__name = 'roy'
p = Person()
print(p.__name) || print(p.name)
-> error
if you use __ before variable ,you can access it direct.
class Person:
__name = 'roy'
def getName(self):
return self.__name
p = Person()
print(p.getName())
-> roy
class Person:
__name = 'roy'
p = Person()
print(p._Person__name)
-> roy
40. inheritance mechanism
class SubClassName:(ParentClassName):
……
class Parent:
def hello(self):
print('write code change world')
class Child(Parent):
pass
p = Parent()
p.hello()
c = Child()
c.hello()
->
write code change world
write code change world
if subclass methon is same with parent , it will cover parent method, such as:
class Child(Parent):
def hello(self):
print('believe youself')
c = Child()
c.hello()
-> believe youself
now we will study a simple example:
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('my position is:',self.x, self.y)
class Shark(Fish):
def __init__(self):
#Fish.__init__(self)
super().__init__()
self.hungry = True
def eat(self):
if self.hungry:
print('eat eat eat')
self.hungry = False
else:
print('not hungry')
1,Fish.__init__(self)
2,super().__init__()
1 and 2 is same ,if you not add this ,you invoke move in Shark ,it will error, because ,__init__ will cover parent method, you call move() ,it will not found x and y. if you use 1 and 2, it will solve this question
multiply parent class:
class subClassName:(parent1ClassName, parent2ClassName):
……
class Base1:
def fool1(self):
print('it is fool1')
class Base2:
def fool2(self):
print('it is fool2')
class c(Base1, Base2):
pass
c = c()
c.fool1()
c.fool2()
-> it is fool1
-> it is fool2
Python Base Five的更多相关文章
- Python Base of Scientific Stack(Python基础之科学栈)
Python Base of Scientific Stack(Python基础之科学栈) 1. Python的科学栈(Scientific Stack) NumPy NumPy提供度多维数组对象,以 ...
- Python Base Four
35. In python, file operation syntax is similar to c. open(file,'r',……) //the first parameters is ne ...
- Python Base One
//this is my first day to study python, in order to review, every day i will make notes (2016/7/31) ...
- Python Base Three
//sixth day to study python(2016/8/7) 32. In python , there are have an special type dictionary , it ...
- Python Base Two
//fourth day to study python 24. In python , how to create funcation. we can use def to define funca ...
- 2019-04-18 Python Base 1
C:\Users\Jeffery1u>python Python 3.7.3 (default, Mar 27 2019, 17:13:21) [MSC v.1915 64 bit (AMD64 ...
- python base 64
python中base64编码与解码 引言: 在一些项目中,接口的报文是通过base64加密传输的,所以在进行接口自动化时,需要对所传的参数进行base64编码,对拿到的响应报文进行解码: Bas ...
- Python Base HTTP Server
import BaseHTTPServer import cgi, random, sys MESSAGES = [ "That's as maybe, it's still a frog. ...
- 基于Python+协程+多进程的通用弱密码扫描器
听说不想扯淡的程序猿,不是一只好猿.所以今天来扯扯淡,不贴代码,只讲设计思想. 0x00 起 - 初始设计 我们的目标是设计一枚通用的弱密码扫描器,基本功能是针对不同类型的弱密码,可方便的扩展,比如添 ...
随机推荐
- nginx之HTTP模块配置
listen 指令只能使用与server字段里 如果本地调用可以监听本地Unix套接字文件,性能更加,因为不用走内核网络协议栈 listen unix:/var/run/nginx.sock; ...
- Cross-Entropy Loss 与Accuracy的数值关系(很重要,很好的博客)
http://www.cnblogs.com/dengdan890730/p/6132937.html
- .vue公共组件裁减导航
场景: 有一个公共头部和底部,vue搭建的框架,在app.vue里写的公共方法,首页是个登录页面,不需要公共部分,在这基础上进行公共部分的显示隐藏. 即注册页.登录页.404页面都不要导航 代码: ( ...
- 使用lua实现Spine动画的预加载
创建spine动画有两种方法,分别是createwithfile和createwithdata. createWithFile是通过加载动作数据马上进行创建,如果spine动画中的json文件大小超过 ...
- UIDeviceOrientation 和 UIInterfaceOrientation
有时候,我们处理自动布局时,需要获取到屏幕旋转方向: 以下为本人亲测: UIInterfaceOrientation: 我们需要在- (void)viewDidLoad或其他方法中添加观察者,检测屏幕 ...
- day2-python 登录
# username = 'niuhanyang' # 写一个判断登录的程序: # 输入: username # password # 最大错误次数是3次,输入3次都没有登录成功,提示错误次数达到上限 ...
- 【mysql】[Err]1267 - Illegal mix of collations(utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) for operation ‘=
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
- Yii2 基于rbac访问控制
Yii2 是一款非常强大的PHP底层框架, 牛b的人都喜欢用它, 有时候你们可能会发现, Yii2 底层处理不是很好, 比如: 每次分页, yii底层都会多统计一次数据的总条数! 那只能说你对它还不 ...
- Python9-事件及队列-day37
信号量 from multiprocessing import Process from multiprocessing import Semaphore import time import ran ...
- PAT Basic 1075
1075 链表元素分类 给定一个单链表,请编写程序将链表元素进行分类排列,使得所有负值元素都排在非负值元素的前面,而 [0, K] 区间内的元素都排在大于 K 的元素前面.但每一类内部元素的顺序是不能 ...