Python 类的多态的运用
#类的多态的运用 #汽车类
class Car(object):
def move(self):
print("move ...") #汽车商店类
class CarStore(object): #预定汽车
def order(self,car_type):
#注意,只有运行时,python才会检查是否存在selectCar()方法,意味着只要子类有这个方法就可以了,定义的时候,不写也是可以的,语法检查太差了
return self.selectCar(car_type) #挑选汽车
def selectCar(self):
print("father function().. ") #工厂类
class Factory(object):
def createCar(self):
pass class BMWFactory(Factory):
def createCar(self,car_type):
if car_type == "BMW1":
return BMW1Car()
elif car_type == "BMW2":
return BMW2Car() #宝马汽车店
class BMW_CarStore(CarStore):
#BMW_CarStore继承了CarStore,CarStore中有selectCar方法,但是两者函数签名都不一样,python仍然重写了这个方法
#通过测试,直接调用selectCar(void),提示没有这个方法,说明虽然函数签名不同,但是selectCar(()方法的确重写了
def selectCar(self,car_type):
return BMWFactory().createCar(car_type) #宝马型号1
class BMW1Car(Car):
pass #宝马型号2
class BMW2Car(Car):
pass store = BMW_CarStore() #报错
#store.selectCar() car = store.order("BMW1") car.move()
Python 类的多态的运用的更多相关文章
- Python类的多态的例子
1 # -*- coding: utf-8 -*- 2 # 类的多态 3 4 # 定义Person父类 5 class Person(object): 6 def __init__(self, nam ...
- Python类总结-多态及鸭子类型
Python天生支持多态. 什么是多态: 一类事务的多种形态. 多态的一个例子 class Alipay(): def pay(self,money): print('用支付宝支付了%s元' % mo ...
- python类的多态
1. 什么是多态 多态指的是同一种/类事物的不同形态 2. 为何要用多态 多态性:在多态的背景下,可以在不用考虑对象具体类型的前提下而直接使用对象 多态性的精髓:统一 ...
- Python 类的多态
#python的多态 class Dog(): def eat(self): print("i am dog , eat something . ") class Cat(): d ...
- python类的多态、多态性
多态:多态指的是一类事物有多种形态 多态性: class Animal: def run(self): raise AtrributeError("子类必须实现这种方法") cla ...
- Python类(四)-多态
多态即一个接口,多种实现 按照平常直接调用 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" class Person(obje ...
- Python进阶-XVII 非python的接口类、多态、python自己的封装
1.python模拟java中的接口类 python中是没有接口类的概念的,因为它支持多继承,但是java不能,所以就提出一个接口类的概念 java : 面向对象编程 设计模式 —— 接口 接口类 : ...
- python类的继承和多态,获取对象信息
继承 类的继承机制使得子类可以继承父类中定义的方法,拥有父类的财产,比如有一个Animal的类作为父类,它有一个eat方法: class Animal(object): def __init__(se ...
- python类及其方法
python类及其方法 一.介绍 在 Python 中,面向对象编程主要有两个主题,就是类和类实例类与实例:类与实例相互关联着:类是对象的定义,而实例是"真正的实物",它存放了类中 ...
随机推荐
- [LeetCode] Palindrome Permutation I & II
Palindrome Permutation Given a string, determine if a permutation of the string could form a palindr ...
- ios中解析json对象基类
这个是对上面一篇写的一个解析json对象的基类 @interface BaseObjectFromJson : NSObject + (id) objectWithDict:(NSDictionary ...
- [Windows Azure] Walkthrough to Configure System Center Management Pack for Windows Azure Fabric Preview for SCOM 2012 SP1 (with a MetricsHub Bonus)
The wait is finally over. This is a huge update to the Azure Management Pack over the one that was r ...
- 【甘道夫】HBase基本数据操作详解【完整版,绝对精品】
引言 之前详细写了一篇HBase过滤器的文章,今天把基础的表和数据相关操作补上. 本文档参考最新(截止2014年7月16日)的官方Ref Guide.Developer API编写. 所有代码均基于“ ...
- 【Socket】linux下http服务器开发
1.mystery引入 1)超文本传输协议(HTTP)是一种应用于分布式.合作式.多媒体信息系统的应用层协议 2)工作原理 1)客户端一台客户机与服务器建立连接后,会发送一个请求给服务器,请求方式的格 ...
- Vue2键盘事件
这两天学习了Vue.js 感觉组件这个地方知识点挺多的,而且很重要,所以,今天添加一点小笔记,学习一下Vue键盘事件 键盘事件 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 ...
- Upgrade Bash to 4+ on OS X
http://buddylindsey.com/upgrade-bash-to-4-on-os-x/ Unfortunately, Apple has decided to ship an old v ...
- 【自动化测试】selenium之 chromedriver与chrome版本映射表
chromedriver版本 支持的Chrome版本 v2.30 v58-60 v2.29 v56-58 v2.28 v55-57 v2.27 v54-56 v2.26 v53-55 v2.25 v5 ...
- Python3在指定路径下递归定位文件中出现的字符串
[本文出自天外归云的博客园] 脚本功能:在指定的路径下递归搜索,找出指定字符串在文件中出现的位置(行信息). 用到的python特性: 1. PEP 318 -- Decorators for Fun ...
- html table 点击跳转
在tr上加 onclick事件 ,然后再js代码中写 页面的跳转,将参数以url的形式拼接在跳转url上然后再另一个页面以 request.getAttribute接收当然你如果使用了框架 可能在一些 ...