Python Base Five
2016-08-12 08:52
344 查看
// 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
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
相关文章推荐
- 3.1决策树理论--python深度机器学习
- 2基本概念--python深度机器学习
- 1.2机器学习基础下--python深度机器学习
- 1.1机器学习基础-python深度机器学习
- TF-IDF算法-Python实现(附源代码)
- <Python核心编程>条件和循环(包括迭代器、生成器)
- python全文搜索库Whoosh新手入门(二)快速上手
- Python Tools
- python包urllib名称
- python全文搜索库Whoosh新手入门(一)安装教程
- 部署flask2
- 部署flask1
- [python] 各种ERROR
- 多个版本的Python如何设置不冲突
- numpy.concatenate
- Python中作Q-Q图(quantile-quantile Plot)
- Python 爬虫思路梳理
- 利用Python进行数据分析(5) NumPy基础: ndarray索引和切片
- 判断sqlite是否存在某个表,不存在则创建 python
- Python模块之ConfigParser - 读写配置文件