python类的多态
class Animal:
def speak(self):
pass class People(Animal):
def shuo(self):
print('say hello') class Dog(Animal):
def jiao(self):
print('汪汪汪') class Pig(Animal):
def chang(self):
print('哼哼哼') obj1=People()
obj2=Dog()
obj3=Pig() # obj1.speak()
# obj2.speak()
# obj3.speak() def speak(animal):
animal.speak() speak(obj1)
speak(obj2)
speak(obj3) s1='hello'
l1=[1,2,3]
t1=(1,2) print(len(s1)) #s1.__len__()
print(len(l1)) #l1.__len__()
print(len(t1)) #t1.__len__()
'''
import abc class Animal(metaclass=abc.ABCMeta):
@abc.abstractmethod #加上abc的装饰器就好像java里的抽象类,必须要求子类重写,否则就报错。
def speak(self):
pass @abc.abstractmethod
def run(self):
pass # Animal() # 父类只是用来建立规范的,不能用来实例化的,更无需实现内部的方法 class People(Animal):
def speak(self):
print('say hello') def run(self):
pass class Dog(Animal):
def speak(self):
print('汪汪汪') def run(self):
pass class Pig(Animal):
def speak(self):
print('哼哼哼') def run(self):
pass obj1=People()
obj2=Dog()
obj3=Pig()
python类的多态的更多相关文章
- Python 类的多态的运用
#类的多态的运用 #汽车类 class Car(object): def move(self): print("move ...") #汽车商店类 class CarStore(o ...
- 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 类的多态
#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 中,面向对象编程主要有两个主题,就是类和类实例类与实例:类与实例相互关联着:类是对象的定义,而实例是"真正的实物",它存放了类中 ...
随机推荐
- 索引反向使用案例,加index_desc hint
drop index idx_t;create index idx_t on t(owner desc,object_type asc); select /*+index(a,idx_t)*/ * f ...
- Hibernate初探之一对多映射 及 myeclipse自动生成hibernate文件方法
实现单向一对多: 1)在one方的实体中添加保存many方的集合 2)在one方的配置文件中添加<one-to-many>配置 实现单向多对一: 1)在many方的实体中添加one方的引用 ...
- sublime text html5开发学习 插件篇记录
1.第一步先按照 Package Control,具体步骤自行百度,Google. 2. view in browser 默认的快捷键应该是这样的,我用的是IE浏览器.所以ctrl+alt+i 即可让 ...
- Codeforces Round #423 (Div. 2)
codeforces 423 A. Restaurant Tables [水题] //注意,一个人选座位的顺序,先去单人桌,没有则去空的双人桌,再没有则去有一个人坐着的双人桌.读清题意. #inclu ...
- mysql配置远程登录
1.vim /etc/my.cnf注释这一行:bind-address=127.0.0.1 ==> #bind-address=127.0.0.1 2.重启服务:sudo service mys ...
- 使用libcurl下载文件小例
libcurl是一个很强大的开源网络处理库,支持包括HTTP.HTTPS.FTP……一系列网络协议.用它来进行HTTP的get\post 或者下载文件更是小菜一碟,chrome内核都用到了它,本文主要 ...
- YUV display in OpenGL
http://stackoverflow.com/questions/1106741/need-help-with-yuv-display-in-opengl I am having trouble ...
- JAVA格式化解析日期
- Web项目开发中常见安全问题及防范
计算机程序主要就是输入数据 经过处理之后 输出结果,安全问题由此产生,凡是有输入的地方都可能带来安全风险.根据输入的数据类型,Web应用主要有数值型.字符型.文件型. 要消除风险就要对输入的数据进行检 ...
- oracle查询相关注意点
单表查询: .or 和 and 混合使用 需求:查询业主名称包含'刘'或门牌号包含'5'的,并且地址编号为3的记录 and 的权限优先于 or 所以需要在or的两边添加() 2. 范围查询 除了传统的 ...