访问父类中的子类变量

问题描述 投票:3回答:2

从父类中的子类访问变量是否正确?这是一个很好的OOP方法吗?我不需要创建Animal类的实例,但是如果我愿意的话,make_sound方法会引发AttributeError,这让我很烦。

class Animal:
    def make_sound(self):
        print(self.sound)

class Cat(Animal):
    sound = 'meow'

class Dog(Animal):
    sound = 'bark'

cat = Cat()
cat.make_sound()

dog = Dog()
dog.make_sound()
python class oop
2个回答
7
投票

这种方法没有任何内在错误。这实际上取决于该类的范围和重要性,以及它的使用位置。构建父类以使用隐式定义的属性很快,并且在许多情况下完全正常。但是,有时这些隐式属性可能会失控,您可能希望确保创建新子类的任何人都必须定义这些属性。

有几种方法可以解决这个问题。根据您使用的Python版本,其中一些可能不起作用。我相信像这样的ABC的使用在Python 3.4+中有效。

Python(以及许多OO语言)具有Abstract Base Class的概念。这是一个永远无法实例化的类,它强制要求任何子类必须实现定义为abtract的方法或属性才能实例化。

以下是你如何提供make_sound方法,并且仍然100%确定任何继承Animal的人确实发出了这样的声音。

from abc import ABC, abstractmethod


class Animal(ABC):

    def make_sound(self):
        print(self.sound)

    @property
    @abstractmethod
    def sound(self):
        """ return the sound the animal makes """


class Dog(Animal):

    @property
    def sound(self):
        return "bark"


class Cat(Animal):

    sound = "meow"


class Thing(Animal):
    """ Not an animal """

dog = Dog()
dog.make_sound()
cat = Cat()
cat.make_sound()
# thing = Thing()   this will raise a TypeError, complaining that its abstract
# animal = Animal()   as will this

这显示了许多不同的方法。使用@property装饰器可以设置影响它的实例变量或更复杂的逻辑。在类中设置声音(有点)就像在Java类中设置静态成员一样。由于所有猫都喵喵叫,这在这种情况下可能有意义。


0
投票

是的,这是一个很好的OOP。您可以从父级继承,只需在父类中创建您将在Cat或Dog类的实例中调用的方法,这样就不会向Cat / Dog类添加任何内容:例如:

class Animal():
    def make_sound(self):
        print(self.sound)

    def make_dog_sound(self):
        print('bark')

    def make_cat_sound(self):
        print('meow')

class Cat(Animal):
      pass

class Dog(Animal):
    pass

cat = Cat()
cat.make_cat_sound()

但你所做的也是正确的。

© www.soinside.com 2019 - 2024. All rights reserved.