我的目标是创建一个通过一组功能扩展不同类的类。
在下面的代码中,我能够创建该问题的解决方案,但是在我看来,这并不是解决问题的方法。
要求:
-基本类别(猫,狗)无法更改。
-具有附加功能(Pet_Dog,Pet_Cat)的类应具有最少的代码量,因为我只需要很小的改动就可以使用很多它们。
-我需要能够导入BaseClasses(Cat,Dog)以及增强的类(Pet_Dog,Pet_Cat)。
到目前为止,我的“解决方案”是:
class Cat:
def __init__(self, legs):
self.legs = 4
self.lives = 9
def make_sound(self):
print('Meow!')
class Dog:
def __init__(self, legs):
self.legs = 4
self.hobby = 'Digging holes'
def make_sound(self):
print('Woof!')
def chase_tail(self):
print('Chasing tail')
def create_pet(BaseClass):
class Pet(BaseClass):
has_owner = True
def __init__(self, name, legs):
super().__init__(legs)
self.name = name
def plays_with_owner(self):
print('playing with owner...')
return Pet
class Pet_Dog(Dog):
def __init__(self, BaseClass, name, legs):
self.__class__ = create_pet(BaseClass)(name, legs).__class__
self.__init__(name, legs)
class Pet_Cat(Cat):
def __init__(self, BaseClass, name, legs):
self.__class__ = create_pet(BaseClass)(name, legs).__class__
self.__init__(name, legs)
print('Create Pet Dog')
pet_dog = Pet_Dog(Dog, 'Woofer', 4)
print('Dog Name:', pet_dog.name)
print('Dog Lives:', pet_dog.hobby)
print('Dog Owner:', pet_dog.has_owner)
pet_dog.make_sound()
pet_dog.chase_tail()
pet_dog.plays_with_owner()
最佳答案
在Python中,您可以具有从多个其他类继承的类,因此我们可以创建一个Cat
和Dog
类,以及一个单独的Pet
类。然后,我们可以使Pet_Cat
和Pet_Dog
从Pet
及其各自的动物继承,即:
class Cat:
def __init__(self, legs):
self.legs = legs
self.lives = 9
def make_sound(self):
print('Meow!')
class Dog:
def __init__(self, legs):
self.legs = legs
self.hobby = 'Digging holes'
def make_sound(self):
print('Woof!')
def chase_tail(self):
print('Chasing tail')
class Pet:
has_owner = True
name = None
def plays_with_owner(self):
print('playing with owner...')
class Pet_Dog(Dog, Pet):
def __init__(self, name, legs):
super().__init__(legs)
self.name = name
class Pet_Cat(Cat, Pet):
def __init__(self, name, legs):
super().__init__(legs)
self.name = name
print('Create Pet Dog')
pet_dog = Pet_Dog('Woofer', 4)
print('Dog Name:', pet_dog.name)
print('Dog Lives:', pet_dog.hobby)
print('Dog Owner:', pet_dog.has_owner)
pet_dog.make_sound()
pet_dog.chase_tail()
pet_dog.plays_with_owner()
输出:
Create Pet Dog
Dog Name: Woofer
Dog Lives: Digging holes
Dog Owner: True
Woof!
Chasing tail
playing with owner...
编辑:
如果要在两个函数上都使用init函数,请改用此方法:
class Cat:
def __init__(self, legs):
self.legs = legs
self.lives = 9
def make_sound(self):
print('Meow!')
class Dog:
def __init__(self, legs):
self.legs = legs
self.hobby = 'Digging holes'
def make_sound(self):
print('Woof!')
def chase_tail(self):
print('Chasing tail')
class Pet:
def __init__(self):
self.has_owner = True
self.name = None
def plays_with_owner(self):
print('playing with owner...')
class Pet_Dog(Dog, Pet):
def __init__(self, name, legs):
Dog.__init__(self, legs)
Pet.__init__(self)
self.name = name
class Pet_Cat(Cat, Pet):
def __init__(self, name, legs):
Cat.__init__(self, legs)
Pet.__init__(self)
self.name = name
关于python - 用一组方法和属性(包装器)扩展独立的类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59543322/