{"id":1151663,"date":"2025-01-13T17:16:15","date_gmt":"2025-01-13T09:16:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1151663.html"},"modified":"2025-01-13T17:16:17","modified_gmt":"2025-01-13T09:16:17","slug":"python%e6%ba%90%e7%a0%81%e5%a6%82%e4%bd%95%e5%ae%9e%e4%be%8b%e5%8c%96","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1151663.html","title":{"rendered":"python\u6e90\u7801\u5982\u4f55\u5b9e\u4f8b\u5316"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25181822\/09d96ac8-51c2-4bf2-8a71-214c932db193.webp\" alt=\"python\u6e90\u7801\u5982\u4f55\u5b9e\u4f8b\u5316\" \/><\/p>\n<p><p> <strong>Python\u6e90\u7801\u5b9e\u4f8b\u5316\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u7c7b\u3001\u51fd\u6570\u548c\u6a21\u5757\u3001\u4f7f\u7528\u5de5\u5382\u51fd\u6570\u3001\u4f7f\u7528\u5143\u7c7b\u3002<\/strong> <\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<strong>\u4f7f\u7528\u7c7b<\/strong>\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u5b83\u901a\u8fc7\u5b9a\u4e49\u7c7b\u6765\u521b\u5efa\u5bf9\u8c61\uff0c\u5e76\u901a\u8fc7\u8c03\u7528\u7c7b\u7684\u6784\u9020\u51fd\u6570\u8fdb\u884c\u5b9e\u4f8b\u5316\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u4e00\u4e0b\u5982\u4f55\u4f7f\u7528\u7c7b\u6765\u5b9e\u73b0\u5b9e\u4f8b\u5316\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u7c7b\u662f\u521b\u5efa\u5bf9\u8c61\u7684\u84dd\u56fe\u3002\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u7c7b\u7684\u6784\u9020\u51fd\u6570\u6765\u521b\u5efa\u5bf9\u8c61\u3002\u6784\u9020\u51fd\u6570\u662f\u7c7b\u4e2d\u7684\u4e00\u4e2a\u7279\u6b8a\u65b9\u6cd5\uff0c\u901a\u5e38\u662f<code>__init__<\/code>\u65b9\u6cd5\uff0c\u7528\u4e8e\u521d\u59cb\u5316\u5bf9\u8c61\u7684\u5c5e\u6027\u3002\u901a\u8fc7\u4f20\u9012\u53c2\u6570\u7ed9\u6784\u9020\u51fd\u6570\uff0c\u53ef\u4ee5\u4e3a\u5bf9\u8c61\u8bbe\u7f6e\u521d\u59cb\u72b6\u6001\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self, value):<\/p>\n<p>        self.value = value<\/p>\n<h2><strong>\u5b9e\u4f8b\u5316\u5bf9\u8c61<\/strong><\/h2>\n<p>obj = MyClass(10)<\/p>\n<p>print(obj.value)  # \u8f93\u51fa: 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>MyClass<\/code>\u7c7b\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6784\u9020\u51fd\u6570<code>__init__<\/code>\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570<code>value<\/code>\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u5bf9\u8c61\u7684\u5c5e\u6027<code>self.value<\/code>\u3002\u901a\u8fc7\u8c03\u7528<code>MyClass(10)<\/code>\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5bf9\u8c61<code>obj<\/code>\uff0c\u5e76\u5c06<code>value<\/code>\u521d\u59cb\u5316\u4e3a10\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bbaPython\u6e90\u7801\u5b9e\u4f8b\u5316\u7684\u5176\u4ed6\u65b9\u6cd5\uff0c\u5e76\u6df1\u5165\u63a2\u8ba8\u5982\u4f55\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u7c7b\u5b9e\u4f8b\u5316<\/h3>\n<\/p>\n<p><h4>1. \u521b\u5efa\u7c7b\u548c\u6784\u9020\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u7c7b\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u57fa\u7840\u3002\u7c7b\u5b9a\u4e49\u4e86\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u884c\u4e3a\uff0c\u901a\u8fc7\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u5bf9\u8c61\u7684\u72b6\u6001\u3002\u6784\u9020\u51fd\u6570\u901a\u5e38\u662f<code>__init__<\/code>\u65b9\u6cd5\uff0c\u7528\u4e8e\u521d\u59cb\u5316\u5bf9\u8c61\u7684\u5c5e\u6027\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Person:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<h2><strong>\u5b9e\u4f8b\u5316\u5bf9\u8c61<\/strong><\/h2>\n<p>person1 = Person(&quot;Alice&quot;, 30)<\/p>\n<p>print(person1.name)  # \u8f93\u51fa: Alice<\/p>\n<p>print(person1.age)   # \u8f93\u51fa: 30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>Person<\/code>\u7c7b\u5b9a\u4e49\u4e86\u4e24\u4e2a\u5c5e\u6027<code>name<\/code>\u548c<code>age<\/code>\uff0c\u5e76\u901a\u8fc7\u6784\u9020\u51fd\u6570<code>__init__<\/code>\u8fdb\u884c\u521d\u59cb\u5316\u3002\u901a\u8fc7\u8c03\u7528<code>Person(&quot;Alice&quot;, 30)<\/code>\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a<code>Person<\/code>\u5bf9\u8c61\uff0c\u5e76\u5c06<code>name<\/code>\u548c<code>age<\/code>\u5c5e\u6027\u8bbe\u7f6e\u4e3a&quot;Alice&quot;\u548c30\u3002<\/p>\n<\/p>\n<p><h4>2. \u7c7b\u7684\u7ee7\u627f\u548c\u591a\u6001<\/h4>\n<\/p>\n<p><p>\u7c7b\u7684\u7ee7\u627f\u548c\u591a\u6001\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u91cd\u8981\u7279\u6027\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f\u5b9e\u73b0\u4ee3\u7801\u7684\u91cd\u7528\uff0c\u5e76\u901a\u8fc7\u591a\u6001\u5b9e\u73b0\u4e0d\u540c\u7c7b\u5bf9\u8c61\u7684\u76f8\u540c\u63a5\u53e3\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Animal:<\/p>\n<p>    def make_sound(self):<\/p>\n<p>        pass<\/p>\n<p>class Dog(Animal):<\/p>\n<p>    def make_sound(self):<\/p>\n<p>        return &quot;Woof!&quot;<\/p>\n<p>class Cat(Animal):<\/p>\n<p>    def make_sound(self):<\/p>\n<p>        return &quot;Meow!&quot;<\/p>\n<p>def play_sound(animal):<\/p>\n<p>    print(animal.make_sound())<\/p>\n<h2><strong>\u5b9e\u4f8b\u5316\u5bf9\u8c61<\/strong><\/h2>\n<p>dog = Dog()<\/p>\n<p>cat = Cat()<\/p>\n<p>play_sound(dog)  # \u8f93\u51fa: Woof!<\/p>\n<p>play_sound(cat)  # \u8f93\u51fa: Meow!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>Animal<\/code>\u7c7b\u5b9a\u4e49\u4e86\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5<code>make_sound<\/code>\uff0c<code>Dog<\/code>\u548c<code>Cat<\/code>\u7c7b\u7ee7\u627f\u4e86<code>Animal<\/code>\u7c7b\uff0c\u5e76\u5b9e\u73b0\u4e86<code>make_sound<\/code>\u65b9\u6cd5\u3002\u901a\u8fc7\u8c03\u7528<code>play_sound<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u4f20\u9012\u4e0d\u540c\u7c7b\u578b\u7684\u52a8\u7269\u5bf9\u8c61\uff0c\u5e76\u8c03\u7528\u5b83\u4eec\u7684<code>make_sound<\/code>\u65b9\u6cd5\uff0c\u5b9e\u73b0\u4e86\u591a\u6001\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u51fd\u6570\u548c\u6a21\u5757\u5b9e\u4f8b\u5316<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u5de5\u5382\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5de5\u5382\u51fd\u6570\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u7528\u4e8e\u521b\u5efa\u5bf9\u8c61\u3002\u5de5\u5382\u51fd\u6570\u662f\u4e00\u79cd\u5c01\u88c5\u4e86\u5bf9\u8c61\u521b\u5efa\u903b\u8f91\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u6761\u4ef6\u521b\u5efa\u4e0d\u540c\u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Car:<\/p>\n<p>    def __init__(self, model):<\/p>\n<p>        self.model = model<\/p>\n<p>class Bike:<\/p>\n<p>    def __init__(self, model):<\/p>\n<p>        self.model = model<\/p>\n<p>def vehicle_factory(vehicle_type, model):<\/p>\n<p>    if vehicle_type == &quot;car&quot;:<\/p>\n<p>        return Car(model)<\/p>\n<p>    elif vehicle_type == &quot;bike&quot;:<\/p>\n<p>        return Bike(model)<\/p>\n<p>    else:<\/p>\n<p>        r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;Invalid vehicle type&quot;)<\/p>\n<h2><strong>\u5b9e\u4f8b\u5316\u5bf9\u8c61<\/strong><\/h2>\n<p>car = vehicle_factory(&quot;car&quot;, &quot;Sedan&quot;)<\/p>\n<p>bike = vehicle_factory(&quot;bike&quot;, &quot;Mountain Bike&quot;)<\/p>\n<p>print(car.model)  # \u8f93\u51fa: Sedan<\/p>\n<p>print(bike.model)  # \u8f93\u51fa: Mountain Bike<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>vehicle_factory<\/code>\u51fd\u6570\u6839\u636e\u4f20\u9012\u7684<code>vehicle_type<\/code>\u53c2\u6570\u521b\u5efa\u4e0d\u540c\u7c7b\u578b\u7684\u8f66\u8f86\u5bf9\u8c61\u3002\u901a\u8fc7\u8c03\u7528<code>vehicle_factory(&quot;car&quot;, &quot;Sedan&quot;)<\/code>\u548c<code>vehicle_factory(&quot;bike&quot;, &quot;Mountain Bike&quot;)<\/code>\uff0c\u6211\u4eec\u5206\u522b\u521b\u5efa\u4e86<code>Car<\/code>\u548c<code>Bike<\/code>\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u6a21\u5757\u5b9e\u4f8b\u5316<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6a21\u5757\u662f\u7ec4\u7ec7\u4ee3\u7801\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c06\u76f8\u5173\u7684\u4ee3\u7801\u653e\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u901a\u8fc7\u5bfc\u5165\u6a21\u5757\u6765\u4f7f\u7528\u5176\u4e2d\u7684\u51fd\u6570\u548c\u7c7b\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># vehicle.py<\/p>\n<p>class Car:<\/p>\n<p>    def __init__(self, model):<\/p>\n<p>        self.model = model<\/p>\n<p>class Bike:<\/p>\n<p>    def __init__(self, model):<\/p>\n<p>        self.model = model<\/p>\n<p>def create_vehicle(vehicle_type, model):<\/p>\n<p>    if vehicle_type == &quot;car&quot;:<\/p>\n<p>        return Car(model)<\/p>\n<p>    elif vehicle_type == &quot;bike&quot;:<\/p>\n<p>        return Bike(model)<\/p>\n<p>    else:<\/p>\n<p>        raise ValueError(&quot;Invalid vehicle type&quot;)<\/p>\n<h2><strong>main.py<\/strong><\/h2>\n<p>import vehicle<\/p>\n<h2><strong>\u5b9e\u4f8b\u5316\u5bf9\u8c61<\/strong><\/h2>\n<p>car = vehicle.create_vehicle(&quot;car&quot;, &quot;Sedan&quot;)<\/p>\n<p>bike = vehicle.create_vehicle(&quot;bike&quot;, &quot;Mountain Bike&quot;)<\/p>\n<p>print(car.model)  # \u8f93\u51fa: Sedan<\/p>\n<p>print(bike.model)  # \u8f93\u51fa: Mountain Bike<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u8f66\u8f86\u76f8\u5173\u7684\u4ee3\u7801\u653e\u5728<code>vehicle.py<\/code>\u6a21\u5757\u4e2d\uff0c\u901a\u8fc7\u5bfc\u5165\u6a21\u5757<code>vehicle<\/code>\u6765\u4f7f\u7528\u5176\u4e2d\u7684\u7c7b\u548c\u51fd\u6570\u3002\u901a\u8fc7\u8c03\u7528<code>vehicle.create_vehicle<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e0d\u540c\u7c7b\u578b\u7684\u8f66\u8f86\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5143\u7c7b\u5b9e\u4f8b\u5316<\/h3>\n<\/p>\n<p><h4>1. \u4ec0\u4e48\u662f\u5143\u7c7b<\/h4>\n<\/p>\n<p><p>\u5143\u7c7b\u662f\u521b\u5efa\u7c7b\u7684\u7c7b\u3002\u5728Python\u4e2d\uff0c\u7c7b\u662f\u901a\u8fc7\u5143\u7c7b\u521b\u5efa\u7684\uff0c\u5143\u7c7b\u5b9a\u4e49\u4e86\u7c7b\u7684\u884c\u4e3a\u548c\u5c5e\u6027\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u7c7b\u90fd\u662f\u7531<code>type<\/code>\u5143\u7c7b\u521b\u5efa\u7684\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5143\u7c7b\u6765\u6539\u53d8\u7c7b\u7684\u521b\u5efa\u8fc7\u7a0b\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyMeta(type):<\/p>\n<p>    def __new__(cls, name, bases, dct):<\/p>\n<p>        print(f&quot;Creating class {name}&quot;)<\/p>\n<p>        return super().__new__(cls, name, bases, dct)<\/p>\n<p>class MyClass(metaclass=MyMeta):<\/p>\n<p>    pass<\/p>\n<h2><strong>\u5b9e\u4f8b\u5316\u5bf9\u8c61<\/strong><\/h2>\n<p>obj = MyClass()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5143\u7c7b<code>MyMeta<\/code>\uff0c\u5e76\u91cd\u5199\u4e86<code>__new__<\/code>\u65b9\u6cd5\u3002\u5728\u521b\u5efa\u7c7b<code>MyClass<\/code>\u65f6\uff0c<code>MyMeta<\/code>\u7684<code>__new__<\/code>\u65b9\u6cd5\u4f1a\u88ab\u8c03\u7528\uff0c\u5e76\u8f93\u51fa&quot;Creating class MyClass&quot;\u3002\u901a\u8fc7\u5c06<code>metaclass<\/code>\u53c2\u6570\u8bbe\u7f6e\u4e3a<code>MyMeta<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u5143\u7c7b\u521b\u5efa\u7c7b\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u5143\u7c7b\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f<\/h4>\n<\/p>\n<p><p>\u5355\u4f8b\u6a21\u5f0f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u786e\u4fdd\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5143\u7c7b\u6765\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class SingletonMeta(type):<\/p>\n<p>    _instances = {}<\/p>\n<p>    def __call__(cls, *args, kwargs):<\/p>\n<p>        if cls not in cls._instances:<\/p>\n<p>            instance = super().__call__(*args, kwargs)<\/p>\n<p>            cls._instances[cls] = instance<\/p>\n<p>        return cls._instances[cls]<\/p>\n<p>class Singleton(metaclass=SingletonMeta):<\/p>\n<p>    def __init__(self, value):<\/p>\n<p>        self.value = value<\/p>\n<h2><strong>\u5b9e\u4f8b\u5316\u5bf9\u8c61<\/strong><\/h2>\n<p>singleton1 = Singleton(10)<\/p>\n<p>singleton2 = Singleton(20)<\/p>\n<p>print(singleton1.value)  # \u8f93\u51fa: 10<\/p>\n<p>print(singleton2.value)  # \u8f93\u51fa: 10<\/p>\n<p>print(singleton1 is singleton2)  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5143\u7c7b<code>SingletonMeta<\/code>\uff0c\u5e76\u91cd\u5199\u4e86<code>__call__<\/code>\u65b9\u6cd5\u3002\u5728\u521b\u5efa\u7c7b<code>Singleton<\/code>\u7684\u5b9e\u4f8b\u65f6\uff0c<code>SingletonMeta<\/code>\u7684<code>__call__<\/code>\u65b9\u6cd5\u4f1a\u88ab\u8c03\u7528\uff0c\u5e76\u786e\u4fdd\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\u88ab\u521b\u5efa\u3002\u901a\u8fc7\u5c06<code>metaclass<\/code>\u53c2\u6570\u8bbe\u7f6e\u4e3a<code>SingletonMeta<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u5143\u7c7b\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u8bbe\u8ba1\u6a21\u5f0f\u5b9e\u4f8b\u5316<\/h3>\n<\/p>\n<p><h4>1. \u5de5\u5382\u6a21\u5f0f<\/h4>\n<\/p>\n<p><p>\u5de5\u5382\u6a21\u5f0f\u662f\u4e00\u79cd\u521b\u5efa\u5bf9\u8c61\u7684\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u521b\u5efa\u5bf9\u8c61\u7684\u63a5\u53e3\u6765\u5c01\u88c5\u5bf9\u8c61\u7684\u521b\u5efa\u8fc7\u7a0b\u3002\u5de5\u5382\u6a21\u5f0f\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7075\u6d3b\u548c\u6613\u4e8e\u6269\u5c55\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Shape:<\/p>\n<p>    def draw(self):<\/p>\n<p>        pass<\/p>\n<p>class Circle(Shape):<\/p>\n<p>    def draw(self):<\/p>\n<p>        return &quot;Drawing a Circle&quot;<\/p>\n<p>class Square(Shape):<\/p>\n<p>    def draw(self):<\/p>\n<p>        return &quot;Drawing a Square&quot;<\/p>\n<p>class ShapeFactory:<\/p>\n<p>    def create_shape(self, shape_type):<\/p>\n<p>        if shape_type == &quot;circle&quot;:<\/p>\n<p>            return Circle()<\/p>\n<p>        elif shape_type == &quot;square&quot;:<\/p>\n<p>            return Square()<\/p>\n<p>        else:<\/p>\n<p>            raise ValueError(&quot;Invalid shape type&quot;)<\/p>\n<h2><strong>\u5b9e\u4f8b\u5316\u5bf9\u8c61<\/strong><\/h2>\n<p>factory = ShapeFactory()<\/p>\n<p>circle = factory.create_shape(&quot;circle&quot;)<\/p>\n<p>square = factory.create_shape(&quot;square&quot;)<\/p>\n<p>print(circle.draw())  # \u8f93\u51fa: Drawing a Circle<\/p>\n<p>print(square.draw())  # \u8f93\u51fa: Drawing a Square<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>ShapeFactory<\/code>\u7c7b\u5b9a\u4e49\u4e86\u4e00\u4e2a\u521b\u5efa\u5f62\u72b6\u5bf9\u8c61\u7684\u63a5\u53e3<code>create_shape<\/code>\uff0c\u901a\u8fc7\u4f20\u9012<code>shape_type<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u521b\u5efa\u4e0d\u540c\u7c7b\u578b\u7684\u5f62\u72b6\u5bf9\u8c61\u3002\u901a\u8fc7\u8c03\u7528<code>factory.create_shape(&quot;circle&quot;)<\/code>\u548c<code>factory.create_shape(&quot;square&quot;)<\/code>\uff0c\u6211\u4eec\u5206\u522b\u521b\u5efa\u4e86<code>Circle<\/code>\u548c<code>Square<\/code>\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h4>2. \u539f\u578b\u6a21\u5f0f<\/h4>\n<\/p>\n<p><p>\u539f\u578b\u6a21\u5f0f\u662f\u4e00\u79cd\u521b\u5efa\u5bf9\u8c61\u7684\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u901a\u8fc7\u590d\u5236\u73b0\u6709\u5bf9\u8c61\u6765\u521b\u5efa\u65b0\u5bf9\u8c61\u3002\u539f\u578b\u6a21\u5f0f\u53ef\u4ee5\u63d0\u9ad8\u5bf9\u8c61\u521b\u5efa\u7684\u6548\u7387\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u521b\u5efa\u4ee3\u4ef7\u8f83\u9ad8\u7684\u5bf9\u8c61\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import copy<\/p>\n<p>class Prototype:<\/p>\n<p>    def clone(self):<\/p>\n<p>        return copy.deepcopy(self)<\/p>\n<p>class ConcretePrototype(Prototype):<\/p>\n<p>    def __init__(self, value):<\/p>\n<p>        self.value = value<\/p>\n<h2><strong>\u5b9e\u4f8b\u5316\u5bf9\u8c61<\/strong><\/h2>\n<p>prototype = ConcretePrototype(10)<\/p>\n<p>clone = prototype.clone()<\/p>\n<p>print(prototype.value)  # \u8f93\u51fa: 10<\/p>\n<p>print(clone.value)  # \u8f93\u51fa: 10<\/p>\n<p>print(prototype is clone)  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>Prototype<\/code>\u7c7b\u5b9a\u4e49\u4e86\u4e00\u4e2a\u514b\u9686\u65b9\u6cd5<code>clone<\/code>\uff0c\u901a\u8fc7<code>copy.deepcopy<\/code>\u65b9\u6cd5\u6765\u590d\u5236\u5bf9\u8c61\u3002<code>ConcretePrototype<\/code>\u7c7b\u7ee7\u627f\u4e86<code>Prototype<\/code>\u7c7b\uff0c\u5e76\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5c5e\u6027<code>value<\/code>\u3002\u901a\u8fc7\u8c03\u7528<code>prototype.clone()<\/code>\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a<code>prototype<\/code>\u5bf9\u8c61\u7684\u514b\u9686\uff0c\u5e76\u786e\u4fdd<code>prototype<\/code>\u548c<code>clone<\/code>\u662f\u4e0d\u540c\u7684\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u4f9d\u8d56\u6ce8\u5165\u5b9e\u4f8b\u5316<\/h3>\n<\/p>\n<p><p>\u4f9d\u8d56\u6ce8\u5165\u662f\u4e00\u79cd\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u901a\u8fc7\u5c06\u4f9d\u8d56\u9879\u6ce8\u5165\u5230\u5bf9\u8c61\u4e2d\u6765\u5b9e\u73b0\u5bf9\u8c61\u7684\u521b\u5efa\u548c\u7ba1\u7406\u3002\u4f9d\u8d56\u6ce8\u5165\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u6d4b\u8bd5\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Service:<\/p>\n<p>    def __init__(self, repository):<\/p>\n<p>        self.repository = repository<\/p>\n<p>    def get_data(self):<\/p>\n<p>        return self.repository.fetch_data()<\/p>\n<p>class Repository:<\/p>\n<p>    def fetch_data(self):<\/p>\n<p>        return &quot;Data from repository&quot;<\/p>\n<p>class Container:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.services = {}<\/p>\n<p>    def register(self, name, service):<\/p>\n<p>        self.services[name] = service<\/p>\n<p>    def resolve(self, name):<\/p>\n<p>        return self.services[name]<\/p>\n<h2><strong>\u5b9e\u4f8b\u5316\u5bf9\u8c61<\/strong><\/h2>\n<p>container = Container()<\/p>\n<p>repository = Repository()<\/p>\n<p>service = Service(repository)<\/p>\n<p>container.register(&quot;service&quot;, service)<\/p>\n<p>resolved_service = container.resolve(&quot;service&quot;)<\/p>\n<p>print(resolved_service.get_data())  # \u8f93\u51fa: Data from repository<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>Service<\/code>\u7c7b\u4f9d\u8d56\u4e8e<code>Repository<\/code>\u7c7b\uff0c\u901a\u8fc7\u4f9d\u8d56\u6ce8\u5165\u5c06<code>Repository<\/code>\u5bf9\u8c61\u4f20\u9012\u7ed9<code>Service<\/code>\u5bf9\u8c61\u3002<code>Container<\/code>\u7c7b\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u4f9d\u8d56\u6ce8\u5165\u5bb9\u5668\uff0c\u901a\u8fc7<code>register<\/code>\u65b9\u6cd5\u6ce8\u518c\u670d\u52a1\uff0c\u901a\u8fc7<code>resolve<\/code>\u65b9\u6cd5\u89e3\u6790\u670d\u52a1\u3002\u901a\u8fc7\u4f9d\u8d56\u6ce8\u5165\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u7ba1\u7406\u5bf9\u8c61\u7684\u521b\u5efa\u548c\u4f9d\u8d56\u5173\u7cfb\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86Python\u6e90\u7801\u5b9e\u4f8b\u5316\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u7c7b\u3001\u51fd\u6570\u548c\u6a21\u5757\u3001\u4f7f\u7528\u5143\u7c7b\u3001\u4f7f\u7528\u8bbe\u8ba1\u6a21\u5f0f\u4ee5\u53ca\u4f7f\u7528\u4f9d\u8d56\u6ce8\u5165\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u5e94\u7528\u573a\u666f\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u5bf9\u8c61\u7684\u521b\u5efa\u548c\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><p><strong>\u901a\u8fc7\u4f7f\u7528\u7c7b\u548c\u6784\u9020\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u884c\u4e3a\uff0c\u5e76\u901a\u8fc7\u8c03\u7528\u6784\u9020\u51fd\u6570\u8fdb\u884c\u5b9e\u4f8b\u5316\u3002<\/strong> <strong>\u901a\u8fc7\u4f7f\u7528\u5de5\u5382\u51fd\u6570\u548c\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u5c01\u88c5\u5bf9\u8c61\u7684\u521b\u5efa\u903b\u8f91\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u3002<\/strong> <strong>\u901a\u8fc7\u4f7f\u7528\u5143\u7c7b\uff0c\u6211\u4eec\u53ef\u4ee5\u81ea\u5b9a\u4e49\u7c7b\u7684\u521b\u5efa\u8fc7\u7a0b\uff0c\u5b9e\u73b0\u9ad8\u7ea7\u7684\u5bf9\u8c61\u7ba1\u7406\u3002<\/strong> <strong>\u901a\u8fc7\u4f7f\u7528\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002<\/strong> <strong>\u901a\u8fc7\u4f7f\u7528\u4f9d\u8d56\u6ce8\u5165\uff0c\u6211\u4eec\u53ef\u4ee5\u7ba1\u7406\u5bf9\u8c61\u7684\u521b\u5efa\u548c\u4f9d\u8d56\u5173\u7cfb\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u6d4b\u8bd5\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u7406\u89e3\u5bf9\u8c61\u7684\u521b\u5efa\u8fc7\u7a0b\u548c\u7ba1\u7406\u4f9d\u8d56\u5173\u7cfb\u662f\u7f16\u5199\u9ad8\u8d28\u91cfPython\u4ee3\u7801\u7684\u5173\u952e\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528Python\u6e90\u7801\u5b9e\u4f8b\u5316\u7684\u5404\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u7c7b\u7684\u5b9e\u4f8b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8981\u5b9e\u4f8b\u5316\u4e00\u4e2a\u7c7b\uff0c\u53ef\u4ee5\u4f7f\u7528\u7c7b\u540d\u540e\u8ddf\u62ec\u53f7\u7684\u65b9\u5f0f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u540d\u4e3a<code>MyClass<\/code>\u7684\u7c7b\uff0c\u53ef\u4ee5\u901a\u8fc7<code>my_instance = MyClass()<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u5b9e\u4f8b<code>my_instance<\/code>\u3002\u8fd9\u4e2a\u5b9e\u4f8b\u73b0\u5728\u53ef\u4ee5\u8bbf\u95ee\u7c7b\u4e2d\u5b9a\u4e49\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u5b9e\u4f8b\u5316Python\u7c7b\u65f6\u53ef\u4ee5\u4f20\u9012\u54ea\u4e9b\u53c2\u6570\uff1f<\/strong><br \/>\u5728\u5b9e\u4f8b\u5316\u7c7b\u65f6\uff0c\u53ef\u4ee5\u5411\u6784\u9020\u51fd\u6570\uff08<code>__init__<\/code>\u65b9\u6cd5\uff09\u4f20\u9012\u53c2\u6570\u3002\u8fd9\u4e9b\u53c2\u6570\u53ef\u4ee5\u7528\u6765\u521d\u59cb\u5316\u5bf9\u8c61\u7684\u5c5e\u6027\u3002\u4f8b\u5982\uff0c\u5982\u679c\u7c7b\u7684\u5b9a\u4e49\u662f<code>class MyClass:<\/code>\u5e76\u4e14\u6784\u9020\u51fd\u6570\u5b9a\u4e49\u4e86<code>__init__(self, value)<\/code>\uff0c\u90a3\u4e48\u5b9e\u4f8b\u5316\u65f6\u53ef\u4ee5\u5199\u4f5c<code>my_instance = MyClass(10)<\/code>\uff0c\u8fd9\u5c06\u628a<code>10<\/code>\u8d4b\u503c\u7ed9<code>value<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u5b9e\u4f8b\u5316\u540e\u8bbf\u95ee\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff1f<\/strong><br \/>\u4e00\u65e6\u5b9e\u4f8b\u5316\u4e86\u4e00\u4e2a\u5bf9\u8c61\uff0c\u53ef\u4ee5\u901a\u8fc7\u70b9\u8fd0\u7b97\u7b26\uff08<code>.<\/code>\uff09\u6765\u8bbf\u95ee\u5176\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u5982\u679c<code>my_instance<\/code>\u662f\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5e76\u4e14\u7c7b\u4e2d\u6709\u4e00\u4e2a\u5c5e\u6027<code>attribute<\/code>\u548c\u4e00\u4e2a\u65b9\u6cd5<code>method()<\/code>\uff0c\u5219\u53ef\u4ee5\u901a\u8fc7<code>my_instance.attribute<\/code>\u6765\u8bbf\u95ee\u5c5e\u6027\uff0c\u901a\u8fc7<code>my_instance.method()<\/code>\u6765\u8c03\u7528\u65b9\u6cd5\u3002\u8fd9\u6837\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u64cd\u4f5c\u548c\u4f7f\u7528\u5bf9\u8c61\u7684\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u6e90\u7801\u5b9e\u4f8b\u5316\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u7c7b\u3001\u51fd\u6570\u548c\u6a21\u5757\u3001\u4f7f\u7528\u5de5\u5382\u51fd\u6570\u3001\u4f7f\u7528\u5143\u7c7b\u3002 \u5176\u4e2d\uff0c\u4f7f\u7528\u7c7b\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1151669,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1151663"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1151663"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1151663\/revisions"}],"predecessor-version":[{"id":1151670,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1151663\/revisions\/1151670"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1151669"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1151663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1151663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1151663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}