继承与父类特化

huangapple go评论98阅读模式
英文:

inheritance with parent class specialization

问题

对不起,如果我很难理解,英语不是我的母语。

我正在尝试创建一个从专门化的父类派生的小狗类,但不知道它将继承哪个父类。同时,给小狗类提供一组要实现的抽象方法。

  1. from abc import ABC, abstractmethod
  2. class DogSkillSet(ABC):
  3. @abstractmethod
  4. def bark(self):
  5. raise NotImplementedError
  6. @abstractmethod
  7. def chase(self):
  8. raise NotImplementedError
  9. @abstractmethod
  10. def fetch(self):
  11. raise NotImplementedError
  12. class Dog:
  13. def __new__(cls, race, **kwargs):
  14. if race == 'sheperd':
  15. return GermanSheperd()
  16. elif race == 'rott':
  17. return Rottweiller()
  18. class GermanSheperd:
  19. def __init__(self):
  20. self.race = "sheperd"
  21. class Rottweiller:
  22. def __init__(self):
  23. self.race = "rott"
  24. class Puppy(Dog, DogSkillSet):
  25. def __init__(self, race):
  26. super().__init__(race)
  27. def bark(self):
  28. print("small woof")
  29. def chase(self):
  30. print("stare into the air")
  31. def fetch(self):
  32. print("stare into the air")
  33. def play(self):
  34. print('playing')
  35. if __name__ == '__main__':
  36. my_dog = Puppy('berger')
  37. my_dog.bark()

但在专门化之后继承不会发生。你能告诉我我做错了什么吗?

英文:

I'm sorry if I'm hard to understand, English is not my first language.

I'm trying to have a Puppy class derived from a specialize parent class, but without knowing which parent class it will inherit. And at the same time, give the Puppy class a set of abstract methods to implement.

  1. from abc import ABC, abstractmethod
  2. class DogSkillSet(ABC):
  3. @abstractmethod
  4. def bark(self):
  5. raise NotImplementedError
  6. @abstractmethod
  7. def chase(self):
  8. raise NotImplementedError
  9. @abstractmethod
  10. def fetch(self):
  11. raise NotImplementedError
  12. class Dog:
  13. def __new__(cls, race, **kwargs):
  14. if race == 'sheperd':
  15. return GermanSheperd()
  16. elif race == 'rott':
  17. return Rottweiller()
  18. class GermanSheperd:
  19. def __init__(self):
  20. self.race = "sheperd"
  21. class Rottweiller:
  22. def __init__(self):
  23. self.race = "rott"
  24. class Puppy(Dog, DogSkillSet):
  25. def __init__(self, race):
  26. super().__init__(race)
  27. def bark(self):
  28. print("small woof")
  29. def chase(self):
  30. print("stare into the air")
  31. def fetch(self):
  32. print("stare into the air")
  33. def play(self):
  34. print('playing')
  35. if __name__ == '__main__':
  36. my_dog = Puppy('berger')
  37. my_dog.bark()

But the inheritance doesn't happen after the specialization. Could you tell me what I am doing wrong.

答案1

得分: 1

以下是已翻译的内容:

你的 Dog.__new__,被 Puppy 继承,返回 GermanShepherdRottweiler 的实例,它们都不继承自 DogSkillSet(或者说不继承自 Dog)。

英文:

Your Dog.__new__ which is inherited by Puppy, returns instances of GermanShepherd or Rottweiler, neither of which inherit from DogSkillSet (or Dog for that matter)

huangapple
  • 本文由 发表于 2023年6月29日 23:23:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582480.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定