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的更多相关文章
随机推荐
- 为什么打开fiddler电脑就不能上网,关了就能正常打开了呢?
因为打开fiddler是它修改浏览器走代理服务器,关掉fiddler之后,代理服务器已经关闭了.但是,但是浏览器的代理模式还没改回来,就是说浏览器还要通过代理访问站点,然而代理服务器已经没有了.打开浏 ...
- XproerIM2-更新-2017-6-28
资源下载:源代码,开发文档,客户端,openfire-3.9.3.exe,openfire-4.1.4.exe, 开源库:cximage600-full,boost-1.55.0,pugixml-1. ...
- png 变透明
using System.Drawing Image image; image = Image.FromFile("d:\\1.png"); Bitmap bitma ...
- OO电梯调度
告别了三次奇妙无比的求导作业之后,我们就开始搭建一部自己的电梯了.相信我们不同同学的电梯运行方式肯定各具特色吧,但值得肯定的是,在艰苦的走完了三次电梯逐步改进的作业之后,我们的电梯在正常情况下应该是可 ...
- 面嚮對象程序設計第一單元作業——OO初試
一.三次作业情况简介 第一次:本次作业仅涉及简单的常数和幂函数的加减法.要求在正确输入下给出正确的求导结果,对错误输出报错 第二次:本次作业涉及常数.幂函数和标准正余弦函数的相乘和相加减,难度较上回作 ...
- HATEOAS
HATEOAS(Hypermedia as the engine of application state)是 REST 架构风格中最复杂的约束,也是构建成熟 REST 服务的核心.它的重要性在于打破 ...
- 需要重写URL但请求的目录不存在报404
用的是asp.net webform,在global.asax的application_beginrequest中写的代码 很简单的一个需求,在url中输入http://www.test.com/lc ...
- 大数据入门到精通16--hive 的条件语句和聚合函数
一.条件表达 case when ... then when .... then ... when ... then ...end select film_id,rpad(title,20," ...
- Commit can not be set while enrolled in a transaction
[java] Exception: java.sql.SQLException [java] Message: Commit can not be set while enrolled in a tr ...
- 【JAVA】JAVAで各DBに接続する方法(JDBC)の纏め(未完結)
■目録 ■ソース ①SQLite3 package cn.com.sy; import java.sql.Connection; import java.sql.DriverManager; impo ...