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 起 - 初始设计 我们的目标是设计一枚通用的弱密码扫描器,基本功能是针对不同类型的弱密码,可方便的扩展,比如添 ...
随机推荐
- FTP服务器建立windows与Linux的文件共享与读写操作
centos7搭建vsftpd 2018-11-15 我们有时想要windows与Linux互传文件,就要用到vsftpd了.它仅仅在windows上面操作,就可以实现与Linux的通信,详情如下: ...
- Java第7次作业:造人类(用private封装,用static关键字自己造重载输出方法)什么是面向对象程序设计?什么是类和对象?什么是无参有参构造方法 ?什么是封装?
什么是面向对象程序设计? 我们称为OOP(Object Oriented Programming) 就是非结构化的程序设计 要使用类和对象的方法来进行编程 什么是类,什么是对象 类就是封装了属性和 ...
- cocostudio的bug(1)
今天有个女同事问我一个问题,两个cocostudio的ui同时addChild到一个layer上面,高层级的ui设置visible为false,低层级的ui设置的visible设置为true,然后低层 ...
- javascript原型继承圣杯模式
javascript纯面向对象开发需要使用到的一个模式,来对对象之间原型继承做中间层代理避免重复继承与代码杂乱 <!DOCTYPE html> <html lang="en ...
- 变量赋值理解--Pyton中让两个值互换的方法
#Pyton中让两个值互换的方法 #方法一:可以理解为相当于是同时赋值 a = 5 b = 4 a,b = b,a print(a,b) #方法二:可以理解为拿箱子过程 c = 10 d = 20 e ...
- paper:synthesizable finite state machine design techniques using the new systemverilog 3.0 enhancements 之 standard verilog FSM conding styles(三段式)
Three always block style with registered outputs(Good style)
- python 类的使用
目录 类的继承 类的派生 类的组合 菱形继承问题 多态与多态性 dataclass的使用 类的继承 什么是继承,在生活中,子承父业,父亲和儿子就是继承的关系 在python中,父类和子类(派生类),父 ...
- Java并发编程的艺术 记录(三)
Java内存模型 并发编程的两个关键问题: 1.线程之间如何通讯. 2.线程间如何同步. 两种方式:共享内存和消息传递. Java的并发采用的是共享内存模型,Java线程之间的通信总是隐式进行,整个通 ...
- python基础学习笔记——循环语句(while、for)
while 循环 流程控制语句 while 1.基本循环 while 条件: # 循环体 # 如果条件为真,那么循环则执行 # 如果条件为假,那么循环不执行 2.break break 用于退出当 ...
- python_字符串,元组,格式化输出
一.字符串 1.字符串是有成对的单引号或者双引号括起来的.例如:name="张三",sex="女" 2.字符串的索引是从0开始的 3.字符串的切片 a.单个字符 ...