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 中,面向对象编程主要有两个主题,就是类和类实例类与实例:类与实例相互关联着:类是对象的定义,而实例是"真正的实物",它存放了类中 ...
随机推荐
- Word 启动模板文件
运行窗口输入:%appdata%\microsoft\templates
- js漂浮广告实现代码(合集经典)
<html> <head> <title>漂浮广告</title> <body> <div id="codefans_net ...
- CDN缓存策略
以下内容就是FAQ,自己也学习一下... 1.CDN加速原理通过动态域名解析,网友的请求被分配到离自己最快的服务器.CDN服务器直接返回缓存文件或通过专线代理原站的内容.网络加速+内容缓存,有效提供访 ...
- February 3 2017 Week 5 Friday
Laughter is an instant vacation. 笑一笑,身心轻松宛如度了个短假. Always present a smile on your face, even there's ...
- February 2 2017 Week 5 Thursday
Only do what your heart tells you. 随心而行. My heart tells me that I should leave here and go back to X ...
- C#配置IIS搭建网站的工具类
public class IISWorker { public static string HostName = "localhost"; /// <summary> ...
- HDU 5677 ztr loves substring(Manacher+dp+二进制分解)
题目链接:HDU 5677 ztr loves substring 题意:有n个字符串,任选k个回文子串,问其长度之和能否等于L. 题解:用manacher算法求出所有回文子串的长度,并记录各长度回文 ...
- UVa 1636 - Headshot(概率)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- mysql 设置无密码登陆
登入 MySQL 报错: C:\Users\dell>mysql -uroot -p Enter password: **** ERROR 1045 (28000): Access denied ...
- python调用对象属性出错:AttributeError: 'function' object has no attribute '_name_'
出错如下图所示: 原来是因为把__name__写成_name_, 下图为正确结果: