擅长:python、mysql、java
<p>如果您希望您的<code>Manager</code>类型具有额外的行为,也许您希望它们拥有自己的、更精细的元类,而不仅仅是使用从<code>Component</code>继承的元类。尝试编写从<code>MetaComponent</code>继承的<code>MetaManager</code>元类。您甚至可以将类方法从<code>Manager1</code>移到元类中(在那里它们成为普通方法):</p>
<pre><code>class MetaManager(MetaComponent):
def __init__(cls, *args, **kwargs):
super().__init__(*args, **kwargs)
cls.SubManagers = [] # each class gets its own SubManagers list
def ListSubManagers(cls):
for manager in cls.SubManagers:
print(manager)
def RegisterSubManager(cls, manager):
cls.SubManagers.append(manager)
return manager
class Manager(Component, metaclass=MetaManager): # inherit from this to get the metaclass
pass
class Manager1(Manager):
pass
@Manager1.RegisterSubManager
class Manager2(Manager):
pass
@Manager2.RegisterSubManager
class Manager3(Manager):
pass
</code></pre>