您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python设计模式之工厂模式

5b51 2022/1/14 8:24:15 python 字数 55532 阅读 611 来源 www.jb51.cc/python

一.理解工厂模式 在面向对象编程中,术语“工厂”表示一个负责创建替他类型对象的类。通常情况下,作为一个工厂的类有一个对象以及与它关联的多个方法。客户端使用某些参数调用此方法,之后,工厂会据此创建所需类

概述

<div class="markdown-here-wrapper" data-md-url="https://i.cnblogs.com/EditPosts.aspx?postid=10029960"&gt;
<h1 id="-" style="margin: 20px 0px 10px; padding: 0px; font-weight: bold; color: black; font-size: 24px; border-bottom: 2px solid #aaaaaa;">一.理解工厂模式
<blockquote style="margin: 1.2em 0px; border-left: 4px solid #dddddd; padding: 0px 1em; color: #777777; quotes: none;">
<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">在面向对象编程中,术语“工厂”表示一个负责创建替他类型对象的类。通常情况下,作为一个工厂的类有一个对象以及与它关联的多个方法。客户端使用某些参数调用方法,之后,工厂会据此创建所需类型的对象,然后将它们返回给客户端。

 abc  ABCMeta,abstractmethod

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Coke<span class="hljs-params" style="color: #ffc58f;">(Metaclass=ABCMeta):
<span class="hljs-decorator">@abstractmethod
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">pass

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Coca<span class="hljs-params" style="color: #ffc58f;">(Coke):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
print(<span class="hljs-string" style="color: #d1f1a9;">'drink Coca-Cola')

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Pepsi<span class="hljs-params" style="color: #ffc58f;">(Coke):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
print(<span class="hljs-string" style="color: #d1f1a9;">'drink Pepsi-Cola')

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Fast_food_restaurant<span class="hljs-params" style="color: #ffc58f;">():
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_coke<span class="hljs-params" style="color: #ffc58f;">(self,name):
<span class="hljs-keyword" style="color: #ebbbff;">return eval(name)()

KCD=Fast_food_restaurant()
coke=KCD.make_coke(<span class="hljs-string" style="color: #d1f1a9;">'Coca')
coke.drink()<span class="hljs-comment" style="color: #7285b7;">#drink Coca-Cola

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Coke<span class="hljs-params" style="color: #ffc58f;">(Metaclass=ABCMeta):
<span class="hljs-decorator">@abstractmethod
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">pass

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Coca<span class="hljs-params" style="color: #ffc58f;">(Coke):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
print(<span class="hljs-string" style="color: #d1f1a9;">'drink Coca-Cola')

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Pepsi<span class="hljs-params" style="color: #ffc58f;">(Coke):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
print(<span class="hljs-string" style="color: #d1f1a9;">'drink Pepsi-Cola')

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Fast_food_restaurant<span class="hljs-params" style="color: #ffc58f;">():
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_coke<span class="hljs-params" style="color: #ffc58f;">(self,name):
<span class="hljs-keyword" style="color: #ebbbff;">return eval(name)()

KCD=Fast_food_restaurant()
coke=KCD.make_coke(<span class="hljs-string" style="color: #d1f1a9;">'Coca')
coke.drink()<span class="hljs-comment" style="color: #7285b7;">#drink Coca-Cola

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Coke<span class="hljs-params" style="color: #ffc58f;">(Metaclass=ABCMeta):
<span class="hljs-decorator">@abstractmethod
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">pass

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Coca<span class="hljs-params" style="color: #ffc58f;">(Coke):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
print(<span class="hljs-string" style="color: #d1f1a9;">'drink Coca-Cola')

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Pepsi<span class="hljs-params" style="color: #ffc58f;">(Coke):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
print(<span class="hljs-string" style="color: #d1f1a9;">'drink Pepsi-Cola')

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Fast_food_restaurant<span class="hljs-params" style="color: #ffc58f;">():
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_coke<span class="hljs-params" style="color: #ffc58f;">(self,name):
<span class="hljs-keyword" style="color: #ebbbff;">return eval(name)()

KCD=Fast_food_restaurant()
coke=KCD.make_coke(<span class="hljs-string" style="color: #d1f1a9;">'Coca')
coke.drink()<span class="hljs-comment" style="color: #7285b7;">#drink Coca-Cola

 <span class="hljs-title" style="color: #7285b7;">Coke<span class="hljs-params" style="color: #ffc58f;">(Metaclass=ABCMeta):
<span class="hljs-decorator">@abstractmethod
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">pass

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Coca<span class="hljs-params" style="color: #ffc58f;">(Coke):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
print(<span class="hljs-string" style="color: #d1f1a9;">'drink Coca-Cola')

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Pepsi<span class="hljs-params" style="color: #ffc58f;">(Coke):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
print(<span class="hljs-string" style="color: #d1f1a9;">'drink Pepsi-Cola')

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Sfencs<span class="hljs-params" style="color: #ffc58f;">(Coke):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
print(<span class="hljs-string" style="color: #d1f1a9;">'drink Sfencs-Cola')

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Fast_food_restaurant<span class="hljs-params" style="color: #ffc58f;">(Metaclass=ABCMeta):
<span class="hljs-decorator">@abstractmethod
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_coke<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">pass

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Coca_produce<span class="hljs-params" style="color: #ffc58f;">(Fast_food_restaurant):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_coke<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">return Coca()

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Pepsi_produce<span class="hljs-params" style="color: #ffc58f;">(Fast_food_restaurant):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_coke<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">return Pepsi()

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Sfencs_produce<span class="hljs-params" style="color: #ffc58f;">(Fast_food_restaurant):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_coke<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">return Sfencs()

KCD=Sfencs_produce()
coke=KCD.make_coke()
coke.drink()<span class="hljs-comment" style="color: #7285b7;">#drink Sfencs-Cola

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Coca<span class="hljs-params" style="color: #ffc58f;">(Coke):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
print(<span class="hljs-string" style="color: #d1f1a9;">'drink Coca-Cola')

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Pepsi<span class="hljs-params" style="color: #ffc58f;">(Coke):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
print(<span class="hljs-string" style="color: #d1f1a9;">'drink Pepsi-Cola')

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Sfencs<span class="hljs-params" style="color: #ffc58f;">(Coke):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
print(<span class="hljs-string" style="color: #d1f1a9;">'drink Sfencs-Cola')

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Fast_food_restaurant<span class="hljs-params" style="color: #ffc58f;">(Metaclass=ABCMeta):
<span class="hljs-decorator">@abstractmethod
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_coke<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">pass

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Coca_produce<span class="hljs-params" style="color: #ffc58f;">(Fast_food_restaurant):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_coke<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">return Coca()

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Pepsi_produce<span class="hljs-params" style="color: #ffc58f;">(Fast_food_restaurant):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_coke<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">return Pepsi()

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Sfencs_produce<span class="hljs-params" style="color: #ffc58f;">(Fast_food_restaurant):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_coke<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">return Sfencs()

KCD=Sfencs_produce()
coke=KCD.make_coke()
coke.drink()<span class="hljs-comment" style="color: #7285b7;">#drink Sfencs-Cola

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Coca<span class="hljs-params" style="color: #ffc58f;">(Coke):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
print(<span class="hljs-string" style="color: #d1f1a9;">'drink Coca-Cola')

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Pepsi<span class="hljs-params" style="color: #ffc58f;">(Coke):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
print(<span class="hljs-string" style="color: #d1f1a9;">'drink Pepsi-Cola')

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Sfencs<span class="hljs-params" style="color: #ffc58f;">(Coke):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
print(<span class="hljs-string" style="color: #d1f1a9;">'drink Sfencs-Cola')

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Fast_food_restaurant<span class="hljs-params" style="color: #ffc58f;">(Metaclass=ABCMeta):
<span class="hljs-decorator">@abstractmethod
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_coke<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">pass

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Coca_produce<span class="hljs-params" style="color: #ffc58f;">(Fast_food_restaurant):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_coke<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">return Coca()

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Pepsi_produce<span class="hljs-params" style="color: #ffc58f;">(Fast_food_restaurant):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_coke<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">return Pepsi()

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Sfencs_produce<span class="hljs-params" style="color: #ffc58f;">(Fast_food_restaurant):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_coke<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">return Sfencs()

KCD=Sfencs_produce()
coke=KCD.make_coke()
coke.drink()<span class="hljs-comment" style="color: #7285b7;">#drink Sfencs-Cola

 <span class="hljs-title" style="color: #7285b7;">Ice_coke<span class="hljs-params" style="color: #ffc58f;">(Metaclass=ABCMeta):
<span class="hljs-decorator">@abstractmethod
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">pass

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Ordinary_coke<span class="hljs-params" style="color: #ffc58f;">(Metaclass=ABCMeta):
<span class="hljs-decorator">@abstractmethod
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">pass

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Coca_ice<span class="hljs-params" style="color: #ffc58f;">(Ice_coke):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
print(<span class="hljs-string" style="color: #d1f1a9;">'drink Coca-ice-Cola')

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Pepsi_ice<span class="hljs-params" style="color: #ffc58f;">(Ice_coke):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
print(<span class="hljs-string" style="color: #d1f1a9;">'drink Pepsi-ice-Cola')

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Coca_ordinary<span class="hljs-params" style="color: #ffc58f;">(Ordinary_coke):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
print(<span class="hljs-string" style="color: #d1f1a9;">'drink Coca-ordinary-Cola')

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Pepsi_ordinary<span class="hljs-params" style="color: #ffc58f;">(Ordinary_coke):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
print(<span class="hljs-string" style="color: #d1f1a9;">'drink Pepsi-ordinary-Cola')

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Fast_food_restaurant<span class="hljs-params" style="color: #ffc58f;">(Metaclass=ABCMeta):
<span class="hljs-decorator">@abstractmethod
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_ice_coke<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">pass

<span class="hljs-decorator"&gt;@abstractmethod</span>
<span class="hljs-function" style="color: #bbdaff;"&gt;<span class="hljs-keyword" style="color: #ebbbff;"&gt;def</span> <span class="hljs-title" style="color: #7285b7;"&gt;make_ordinary_coke</span><span class="hljs-params" style="color: #ffc58f;"&gt;(self)</span>:</span>
    <span class="hljs-keyword" style="color: #ebbbff;"&gt;pass</span>

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Coca_produce<span class="hljs-params" style="color: #ffc58f;">(Fast_food_restaurant):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_ice_coke<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">return Coca_ice()
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_ordinary_coke<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">return Coca_ordinary()

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Pepsi_produce<span class="hljs-params" style="color: #ffc58f;">(Fast_food_restaurant):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_ice_coke<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">return Pepsi_ice()
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_ordinary_coke<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">return Pepsi_ordinary()

KCD=Coca_produce()
coke=KCD.make_ice_coke()
coke.drink()<span class="hljs-comment" style="color: #7285b7;">#drink Coca-ice-Cola

<span class="hljs-decorator"&gt;@abstractmethod</span>
<span class="hljs-function" style="color: #bbdaff;"&gt;<span class="hljs-keyword" style="color: #ebbbff;"&gt;def</span> <span class="hljs-title" style="color: #7285b7;"&gt;make_ordinary_coke</span><span class="hljs-params" style="color: #ffc58f;"&gt;(self)</span>:</span>
    <span class="hljs-keyword" style="color: #ebbbff;"&gt;pass</span>

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Ordinary_coke<span class="hljs-params" style="color: #ffc58f;">(Metaclass=ABCMeta):
<span class="hljs-decorator">@abstractmethod
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">pass

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Coca_ice<span class="hljs-params" style="color: #ffc58f;">(Ice_coke):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
print(<span class="hljs-string" style="color: #d1f1a9;">'drink Coca-ice-Cola')

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Pepsi_ice<span class="hljs-params" style="color: #ffc58f;">(Ice_coke):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
print(<span class="hljs-string" style="color: #d1f1a9;">'drink Pepsi-ice-Cola')

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Coca_ordinary<span class="hljs-params" style="color: #ffc58f;">(Ordinary_coke):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
print(<span class="hljs-string" style="color: #d1f1a9;">'drink Coca-ordinary-Cola')

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Pepsi_ordinary<span class="hljs-params" style="color: #ffc58f;">(Ordinary_coke):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
print(<span class="hljs-string" style="color: #d1f1a9;">'drink Pepsi-ordinary-Cola')

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Fast_food_restaurant<span class="hljs-params" style="color: #ffc58f;">(Metaclass=ABCMeta):
<span class="hljs-decorator">@abstractmethod
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_ice_coke<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">pass

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Coca_produce<span class="hljs-params" style="color: #ffc58f;">(Fast_food_restaurant):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_ice_coke<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">return Coca_ice()
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_ordinary_coke<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">return Coca_ordinary()

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Pepsi_produce<span class="hljs-params" style="color: #ffc58f;">(Fast_food_restaurant):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_ice_coke<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">return Pepsi_ice()
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_ordinary_coke<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">return Pepsi_ordinary()

KCD=Coca_produce()
coke=KCD.make_ice_coke()
coke.drink()<span class="hljs-comment" style="color: #7285b7;">#drink Coca-ice-Cola

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Ordinary_coke<span class="hljs-params" style="color: #ffc58f;">(Metaclass=ABCMeta):
<span class="hljs-decorator">@abstractmethod
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">pass

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Coca_ice<span class="hljs-params" style="color: #ffc58f;">(Ice_coke):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
print(<span class="hljs-string" style="color: #d1f1a9;">'drink Coca-ice-Cola')

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Pepsi_ice<span class="hljs-params" style="color: #ffc58f;">(Ice_coke):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
print(<span class="hljs-string" style="color: #d1f1a9;">'drink Pepsi-ice-Cola')

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Coca_ordinary<span class="hljs-params" style="color: #ffc58f;">(Ordinary_coke):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
print(<span class="hljs-string" style="color: #d1f1a9;">'drink Coca-ordinary-Cola')

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Pepsi_ordinary<span class="hljs-params" style="color: #ffc58f;">(Ordinary_coke):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">drink<span class="hljs-params" style="color: #ffc58f;">(self):
print(<span class="hljs-string" style="color: #d1f1a9;">'drink Pepsi-ordinary-Cola')

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Fast_food_restaurant<span class="hljs-params" style="color: #ffc58f;">(Metaclass=ABCMeta):
<span class="hljs-decorator">@abstractmethod
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_ice_coke<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">pass

<span class="hljs-decorator"&gt;@abstractmethod</span>
<span class="hljs-function" style="color: #bbdaff;"&gt;<span class="hljs-keyword" style="color: #ebbbff;"&gt;def</span> <span class="hljs-title" style="color: #7285b7;"&gt;make_ordinary_coke</span><span class="hljs-params" style="color: #ffc58f;"&gt;(self)</span>:</span>
    <span class="hljs-keyword" style="color: #ebbbff;"&gt;pass</span>

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Coca_produce<span class="hljs-params" style="color: #ffc58f;">(Fast_food_restaurant):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_ice_coke<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">return Coca_ice()
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_ordinary_coke<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">return Coca_ordinary()

<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Pepsi_produce<span class="hljs-params" style="color: #ffc58f;">(Fast_food_restaurant):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_ice_coke<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">return Pepsi_ice()
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">make_ordinary_coke<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">return Pepsi_ordinary()

KCD=Coca_produce()
coke=KCD.make_ice_coke()
coke.drink()<span class="hljs-comment" style="color: #7285b7;">#drink Coca-ice-Cola


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶