多个子类的超类应该更一般还是更具体?

2024-07-08 10:54:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我注意到,很多时候,我不清楚一个由许多子类组成的超类在属性方面应该更一般还是更受限制。下面是一个人为的例子来说明我的意思:

# A) superclass more general (union of attributes)
class human:
    self.__init__(height, beardColor, nailColor):
        self.height=height
        self.beardColor=beardColor
        self.nailColor=nailColor

class man(human):
    self.__init__(heigth, beardColor):
        super().__init__(height, beardColor, None)

class woman(human):
    self.__init__(heigth, nailColor):
        super().__init__(height, None, nailcolor)

# B) superclass more specific (intersection of attributes)
class human:
    self.__init__(height):
        self.height=height

class man(human):
    self.__init__(heigth, beardColor):
        super().__init__(height)
        self.beardColor=beardColor

class woman(human):
    self.__init__(heigth, nailColor):
        super().__init__(height)
        self.nailColor=nailColor

它以某种方式归结为一个问题:一个超类是否应该拥有子类的所有属性的并集,或者更确切地说是它们的交集

他们的研究是否有哪种范式更好的一般规则


Tags: ofself属性initmore子类attributesclass

热门问题