Python_day8
- 多态
class Animal(object):
def run(self):
print('animal is running') class Dog(Animal):
def run(self):
print('Dog run fast') class Rabbit(Animal):
def run(self):
print('Rabbit jump...') class Cat(Animal):
pass class Timer(object):
def run(self):
print('the time is running')
多态:同一种类型,不同的表现形式
def runTwice(animal):
animal.run()
animal.run() a = Animal()
rabbit = Rabbit() runTwice(a)
runTwice(rabbit)
鸭子类型
tm = Timer()
runTwice(tm)
限制实例属性
只允许由'name' 'age'属性
class Person(object):
__slots__ = ('name', 'age')
per1 = Person()
per1.name = '小明'
print(per1.name)
per1.height = 167 # 不允许
class Student(object):
def __init__(self, age):
self.__age = age
•def setScore(self, score):
if 0 <= score <= 100:
self.__score = score
return 'ok'
else:
return 'error' def getScore(self):
return self.__score
•@property # 访问器 可以单独存在
def score(self):
print('getter is called')
return self.__score @score.setter # 设置器 不单独存在,一定要有property
def score(self, score):
print('setter is called')
if 0 <= score <= 100:
self.__score = score
return 'ok'
else:
return 'error' @ property
def age(self):
return self.__age s1 = Student(20)
s1.score = 100
print(s1.score)
print(s1.age)
Python_day8的更多相关文章
随机推荐
- CSS 文字太多用省略号表示
width:150px;/*要显示文字的宽度*/ overflow:hidden; /*超出的部分隐藏起来.*/ white-space:nowrap;/*不显示的地方用省略号...代替*/ text ...
- c3p0数据源的第一次尝试
开始补习 以前学习过的基础 正在尝试从c3p0 获取到connection 好的,首先上代码吧 public static DataSource ds = null; static { ComboPo ...
- DOM节点遍历
"DOM2级遍历和范围"模块定义了两个用于辅助完成顺序遍历DOM结构的类型:NodeIterator 和 TreeWalker .这两个类型能够根据给定的节点对DOM结构进行深度优 ...
- JUnit源码分析 - 扩展 - 自定义RunListener
RunListener简述 JUnit4中的RunListener类用来监听测试执行的各个阶段,由RunNotifier通知测试去运行.RunListener与RunNotifier之间的协作应用的是 ...
- 【相关网站 - 01】- Java 相关网站
一.官方网站 1. Java 官方网站 https://www.java.com/zh_CN/ 2. Spring 官方网站 http://spring.io/ 1. Spring Framework ...
- 4.Redis客户端
4.Redis客户端4.1 客户端通信协议4.2 Java客户端Jedis4.2.1 获取Jedis4.2.2 Jedis的基本使用方法4.2.3 Jedis连接池的使用方法4.2.4 Redis中P ...
- Java多线程中static变量的使用
轉:https://blog.csdn.net/yy304935305/article/details/52456771 鲁迅先生曾说过:“时间就像海绵里的水,只要愿挤,总还是有的”.不管肿(怎)么说 ...
- vue中具名插槽的使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Python基础与进阶
1 Python基础与进阶 欢迎来到Python世界 搭建编程环境 变量 | 字符串 | 注释 | 错误消除 他只用一张图,就把Python中的列表拿下了! 使用 If 语句进行条件测试 使用字典更准 ...
- 27. pt-table-checksum
在主库执行命令: pt-table-checksum -h 192.168.100.101 -P 3306 -u admin -p admin \--nocheck-binlog-format --r ...