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 起 - 初始设计 我们的目标是设计一枚通用的弱密码扫描器,基本功能是针对不同类型的弱密码,可方便的扩展,比如添 ...
随机推荐
- @Param注解在dao层的使用
有时在前台用ajax传过来许多参数,不知道在mybatis如何封装,就要用到@Param注解了,这时就不需要在映射文件写传入参数了,这种方法虽然比较取巧,但还是很实用的,如下图:
- Forbidden You don't have permission to access /phpStudyTest/application/index/controller/Index.php on this server.
发生情况:将thinkPHP从官网上下了 http://thinkphp.cn 然后安装了phpstudy和PHPstorm,并将thinkPHP解压到www路径下 在用PHPstorm打开 thi ...
- java,编写一个从1循环到150并在每行打印一个值,另外在每个3的倍数行上打印出foo,在每个5的倍数行上打印biz,在每个7的倍数上打印baz.
需求:编写一个从1循环到150并在每行打印一个值,另外在每个3的倍数行上打印出foo,在每个5的倍数行上打印biz,在每个7的倍数上打印baz. package study01; public cla ...
- Java中List集合排序的方法 比较器的使用 根据学生对象数学 语文 英语成绩总和进行sort排序
package com.swift; import java.util.ArrayList; import java.util.Collections; import java.util.Compar ...
- MySQL数据库---索引
索引的作用就是快速找出在一个列上用一特定值的行.如果没有索引,MySQL不得不首先以第一条记录开始并然后读完整个表直到它找出相关的行. 索引的类型: 先写一个建表语句: CREATE TABLE `t ...
- destoon 短信发送函数及短信接口修改
// $DT在common.inc.php中定义, $CACHE = cache_read('module.php'); $DT = $CACHE['dt']; 从缓存里读取网站配置信息. //$d ...
- Java 编辑html模板并生成pdf
1.工具类 import com.hujiang.project.zhgd.Util; import com.itextpdf.text.BaseColor; import com.itextpdf. ...
- 使用python3调用MyQR库生成动态二维码(附源代码)
可生成普通二维码.带图片的艺术二维码(黑白与彩色).动态二维码(黑白与彩色). GitHub:https://github.com/sylnsfar/qrcode 中文版:https://github ...
- leetcode-20-Dynamic Programming
303. Range Sum Query - Immutable 解题思路: Note里说sumRange会被调用很多次..所以简直强烈暗示要做cache啊...所以刚开始,虽然用每次都去遍历数组求和 ...
- logging记录了其他操作的问题
做atm作业的时候,记录转账操作的那个功能的文件里,同时也记录了增加账号和冻结账号的操作 2018-11-28 17:14:51,754 : transfer : INFO : 用户edward向用户 ...