python面向对象总结
Posted wen-hai
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python面向对象总结相关的知识,希望对你有一定的参考价值。
-
三大支柱:封装、继承、多态
例子:工资结算系统。
""" 月薪结算系统 - 部门经理每月15000 程序员每小时200 销售员1800底薪加销售额5%提成 """ from abc import ABCMeta, abstractmethod class Employee(metaclass=ABCMeta): """员工(抽象类)""" def __init__(self, name): self.name = name @abstractmethod def get_salary(self): """结算月薪(抽象方法)""" pass class Manager(Employee): """部门经理""" def get_salary(self): return 15000.0 class Programmer(Employee): """程序员""" def __init__(self, name, working_hour=0): self.working_hour = working_hour super().__init__(name) def get_salary(self): return 200.0 * self.working_hour class Salesman(Employee): """销售员""" def __init__(self, name, sales=0.0): self.sales = sales super().__init__(name) def get_salary(self): return 1800.0 + self.sales * 0.05 class EmployeeFactory(): """创建员工的工厂(工厂模式 - 通过工厂实现对象使用者和对象之间的解耦合)""" @staticmethod def create(emp_type, *args, **kwargs): """创建员工""" emp_type = emp_type.upper() emp = None if emp_type == ‘M‘: emp = Manager(*args, **kwargs) elif emp_type == ‘P‘: emp = Programmer(*args, **kwargs) elif emp_type == ‘S‘: emp = Salesman(*args, **kwargs) return emp def main(): """主函数""" emps = [ EmployeeFactory.create(‘M‘, ‘曹操‘), EmployeeFactory.create(‘P‘, ‘荀彧‘, 120), EmployeeFactory.create(‘P‘, ‘郭嘉‘, 85), EmployeeFactory.create(‘S‘, ‘典韦‘, 123000), ] for emp in emps: print(‘%s: %.2f元‘ % (emp.name, emp.get_salary())) if __name__ == ‘__main__‘: main()
-
类与类之间的关系
- is-a关系:继承
- has-a关系:关联 / 聚合 / 合成
- use-a关系:依赖
例子:扑克游戏。
""" 经验:符号常量总是优于字面常量,枚举类型是定义符号常量的最佳选择 """ from enum import Enum, unique import random @unique class Suite(Enum): """花色""" SPADE, HEART, CLUB, DIAMOND = range(4) def __lt__(self, other): return self.value < other.value class Card(): """牌""" def __init__(self, suite, face): """初始化方法""" self.suite = suite self.face = face def show(self): """显示牌面""" suites = [‘♠?‘, ‘♥?‘, ‘♣?‘, ‘♦?‘] faces = [‘‘, ‘A‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘10‘, ‘J‘, ‘Q‘, ‘K‘] return f‘suites[self.suite.value] faces[self.face]‘ def __str__(self): return self.show() def __repr__(self): return self.show() class Poker(): """扑克""" def __init__(self): self.index = 0 self.cards = [Card(suite, face) for suite in Suite for face in range(1, 14)] def shuffle(self): """洗牌(随机乱序)""" random.shuffle(self.cards) self.index = 0 def deal(self): """发牌""" card = self.cards[self.index] self.index += 1 return card @property def has_more(self): return self.index < len(self.cards) class Player(): """玩家""" def __init__(self, name): self.name = name self.cards = [] def get_one(self, card): """摸一张牌""" self.cards.append(card) def sort(self, comp=lambda card: (card.suite, card.face)): """整理手上的牌""" self.cards.sort(key=comp) def main(): """主函数""" poker = Poker() poker.shuffle() players = [Player(‘东邪‘), Player(‘西毒‘), Player(‘南帝‘), Player(‘北丐‘)] while poker.has_more: for player in players: player.get_one(poker.deal()) for player in players: player.sort() print(player.name, end=‘: ‘) print(player.cards) if