Python中类的约束
2024-09-04 02:38:56
如何在python中进行类的约束
使某些类必须有一些方法
1 python 的抽象类实现
=== 约束性不高
Python是 解释性语言
from abc import ABCMeta
from abc import abstractmethod
class Base(ABCMeta);
def send(*args,**kwargs):
pass
class A(Base):
pass
运行的时候才报错
不运行不会报错
2 通过父类抛异常实现
class Base(object):
def send(*args,**kwargs):
raise NotImplementError('未实现的方法')
class A(Base):
pass
定义一个需要重写的方法,否则会抛出异常
最新文章
- Ternary Expression Parser
- Ubuntu 安装Theano
- 使用JSP开发动态网站基础
- Java+FlexPaper+swfTools仿百度文库文档在线预览系统设计与实现
- Aspect Oriented Programming using Interceptors within Castle Windsor and ABP Framework AOP
- spring-boot项目在外部tomcat环境下部署
- 我来说说MVC过滤器
- 添加gif效果图
- Linux基本命令(5)管理使用者和设立权限的命令
- js如何判断一个数组中是否有重复的值
- php cloure闭包
- Mac os 进行Android开发笔记(1)
- 5.1 timestamp数据类型默认值
- spring的Convert机制
- 算法(第四版)C# 习题题解——1.3
- pip3 install pymysql
- How to Pronounce We’ll Contraction
- JPA注解指南
- Web开发中常用的定位布局position
- cf1088D. Ehab and another another xor problem(思维)