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 起 - 初始设计 我们的目标是设计一枚通用的弱密码扫描器,基本功能是针对不同类型的弱密码,可方便的扩展,比如添 ...
随机推荐
- java 自定义容器,实现foreach
import java.util.Arrays; import java.util.Iterator; public class ArrayList implements Iterable<In ...
- Makefile 编写实例
make命令常用的三个选项: 1.-k:它的作用是让make命令在发现错误的时候仍然继续执行.我们可以利用这个选项在一次操作中发现未编译成功的源文件. 2.-n:它的作用是让make命令输出将要执行的 ...
- iOS--获取文件目录的方法
很多文章都有写这个问题,我只是为了记录一下,免得总翻书... 1.Documents 目录: 你应该将所有的应用程序数据文件写入到这个目录下.这个目录用于存储用户数据或其它应该定期备份的信息. 2.L ...
- 一句话懂什么是JS闭包
无论何时声明新函数并将其赋值给变量,都要存储函数定义和闭包.闭包包含在函数创建时作用域中的所有变量,它类似于背包.函数定义附带一个小背包,它的包中存储了函数定义创建时作用域中的所有变量. 我将永远记住 ...
- 【转】pDc->SelectObject(pOldBrush)恢复画刷
请看下面的代码: CDC *pDc=new CClientDC(this); CBrush brush; brush.CreateSolidBrush(RGB(0,255,0)); CBrush * ...
- C++系统学习之九:顺序容器
元素在顺序容器中的顺序与其加入容器时的位置相对应.关联容器中元素的位置由元素相关联的关键字值决定.所有容器类都共享公共的接口,不同容器按不同方式对其进行扩展. 一个容器就是一些特定类型对象的集合.顺序 ...
- MySQL 如何将Id相同的字段合并,并且以逗号隔开
数据库存的数据 sql: SELECT Id,GROUP_CONCAT(`Name` SEPARATOR ',') NAMES FROM `stu` GROUP BY Id;
- python入门:输出1-10以内除去7的所有数(自写)
#!/usr/bin/env python # -*- coding:utf-8 -*- #输出1-10以内除去7的所有数(自写) """ 变量kaishi赋值等于1,w ...
- Livid : 在 26 岁时写给 18 岁的自己
转载自: https://livid.v2ex.com/essays/2012/01/24/a-letter-from-26-to-18.html 在 26 岁时写给 18 岁的自己 Jan 24, ...
- Python正则表达式详解——re库
一.简介 1.1.相关链接 官方文档: Python2:https://docs.python.org/2/library/re.html Python3:https://docs.python.or ...