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的更多相关文章
随机推荐
- [Go] 开始试探一门新语言的五点思考 - Golang
1.如果在其他语言环境中写的代码很烂,那么换一门语言很可能情况更糟,因为是涉及到基本功.工程能力和心思逻辑. 2.一定要了解语言解决的问题(比如:多核并发机制性能高.省机器.简洁易学.资料少),优势是 ...
- PeopleSoft进程卡在“已排队”状态诊断和解决
进程卡在“已排队”状态的原因很多.最常见的原始是进程调度器服务挂掉了(可以在“进程监视器”页面的“服务器”tab页中查看). 除此之外,还可以进行下面诊断:1. 检查下面3张表:PSPRCSRQSTP ...
- 如何在ubuntu中安装php
如何在ubuntu中安装php 情衅 | 浏览 692 次 发布于2016-05-07 12:36 最佳答案 关于Ubuntu下的LAMP配置步骤: 首先要安装LAMP 就是Apache,PH ...
- 128bit 整数运算的实现
对于128bit的长整型运算,GCC提供了扩展类型:__int128.然而该类型不在C/C++语言的标准之中,并且对于不同种类的编译器,它的实现情况不同.因此,在编写相关的可移植程序时,我们有必要实现 ...
- PHP上传图片例子
PHP上传图片例子 源码下载 两个文件: tu.php upload.php tu.php 代码: <?php ini_set("display_errors", &q ...
- session的简单应用
session的作用:服务器端保存信息. 用户登陆后,服务器端保存了 自定义的key:value 如下: if username == 'xxx' and password == 'xxxx': re ...
- C++———Vector
#include<algorithm> #include <vector> #include <iostream> #include <stdio.h> ...
- Unity3d项目入门之Rolling Ball
下面通过分析制作一个简单的收集特定物体的滚球游戏来入门unity,包括操作面板和C#脚本的编写导入,创建Game Object和给Object添加组件等等. 一 初始设置 在Assert下创建主场景M ...
- innodb_flush_log_at_trx_commit与sync_binlog理解
innodb_flush_log_at_trx_commit该参数控制重做日志写入磁盘的过程.我们知道 InnoDB 使用“Write Ahead Log”策略来避免数据丢失问题,即依靠重做日志来保证 ...
- CentOS7+CDH5.14.0安装CDH错误排查: HiveServer2 该角色的进程已退出。该角色的预期状态为已启动
错误提示: HiveServer2 该角色的进程已退出.该角色的预期状态为已启动 解决办法:出现此问题应该是内存不足造成的,重启相应的组件即可.比如Hive报错,重启Hive,YARN报错,重启YAR ...