Python面向对象编程——一些类定义(杂)
2024-10-21 05:42:58
一、abstractmethod
- 子类必须全部实现重写父类的abstractmethod方法
- 非abstractmethod方法可以不实现重写
- 带abstractmethod方法的类不能实例化
from abc import abstractmethod, ABCMeta
1 class BettingStrategy(metaclass=ABCMeta):
2
3 @abstractmethod
4 def bet(self):
5 print('0')
6
7 def record_win(self):
8 print('win')
9
10 def record_loss(self):
11 print('loss')
12
13
14 class Bet(BettingStrategy):
15 def bet(self):
16 print('1')
扩展:abc模块
二、staticmethod:静态函数
对象不用实例化即可调用的函数
1 class Hand4:
2 def __init__(self, dealer_card, *cards):
3 self.dealer_card = dealer_card
4 self.cards = cards
5
6 @staticmethod
7 def freeze(other):
8 hand = Hand4(other.dealer_card, *other.cards)
9 return hand
10
11 @staticmethod
12 def split(other, card0, card1):
13 hand0 = Hand4(other.dealer_card, other.cards[0], card0)
14 hand1 = Hand4(other.dealer_card, other.cards[1], card1)
15 return hand0, hand1
16
17 def __str__(self):
18 return ','.join(map(str, self.cards))
1 h41 = Hand4(deck3.pop(), deck3.pop(), deck3.pop())
2 s1, s2 = Hand4.split(h41, deck3.pop(), deck3.pop())
3 s3 = Hand4.freeze(h41)
调用
最新文章
- IIS7 经典模式和集成模式的区别(转载)
- nodejs 导出excel
- JavaScript实例-----反选
- python文件取MD5
- 【液晶模块系列基础视频】4.4.X-GUI图形界面库-画tab函数简介
- vps选择
- php基础知识(1)
- IBM Websphere 说明文档
- OpenStack25
- Android总结的基本机制监控事件
- iOS基础 - 定时器
- webstrom命令大全
- 【Android Developers Training】 101. 显示快速联系人挂件(Quick Contact Badge)
- SpriteBuilder中CCMotionStreak提示图片文件找不到
- .NetCore WebAPI采坑之路(持续更新)
- 【转载】NeurIPS 2018 | 腾讯AI Lab详解3大热点:模型压缩、机器学习及最优化算法
- PHP 图片处理类 错误处理方法:
- [需要补充]javaEE中servlet方法service与doXXX的关系
- [LeetCode] Binary Tree Maximum Path Sum(最大路径和)
- thinkphp5.0模块设计
热门文章
- go简单写个ini配置文件读取工具
- T137233 魔术增幅
- [前端js] 爬取亿图脑图大纲
- ScrollView里面不能嵌套一个FlatList,这个需要如何通过FlatList自己单独实现
- C#封装FluentValidation
- 记录VUE项目使用 sass 版本不匹配问题
- Vue: 单页面应用如何保持登录状态
- 对词向量模型Word2Vec和GloVe的理解
- 问题记录_IDEA启动报错:Failed to create JVM. JVM Path
- mysql sum 聚合计算后精度不准 出现多位小数点后的数