子类没有正确地从超类继承结构
Subclass not inheriting structure from superclass properly
我刚开始使用 Python,但我无法弄清楚为什么我在使用如此简单的 class 继承时遇到问题,尽管我经常使用本教程我一直在关注,我还没有在 Stack Overflow 上看到其他人遇到过这个问题。这是代码(别担心,没什么太复杂的):
import random
import sys
import os
class Animal:
__name = ""
__height = 0
__weight = 0
__sound = 0
def __init__(self, name, height, weight, sound):
self.__name = name
self.__height = height
self.__weight = weight
self.__sound = sound
def toString(self):
return "{} is {} cm tall and {} kilograms and says {}".format(self.__name,
self.__height,
self.__weight,
self.__sound)
cat = Animal ('Whiskers', 33, 10, 'meow')
print(cat.toString())
bird = Animal ('Flutie', 33, 10, 'tweet')
print(bird.toString())
class Dog(Animal):
def __init__(self, name, height, weight, sound):
super(Dog, self).__init__(name, height, weight, sound)
def toString(self):
return "{} is {} cm tall and {} kilograms and says {}".format(self.__name,
self.__height,
self.__weight,
self.__sound)
spot = Dog ('Spot', 53, 27, "Woof")
print(spot.toString())
...这是输出:
Whiskers is 33 cm tall and 10 kilograms and says meow
Flutie is 33 cm tall and 10 kilograms and says tweet
Traceback (most recent call last):
File "C:/.../animal_test.py", line 72, in <module>
print(spot.toString())
File "C:/.../animal_test.py", line 65, in toString
return "{} is {} cm tall and {} kilograms and says {}".format(self.__name,
AttributeError: 'Dog' object has no attribute '_Dog__name'
双下划线代表name mangling。
class Animal:
def __init__(self, name, height, weight, sound):
self.__name = name
self.__height = height
self.__weight = weight
self.__sound = sound
在解释时直译为:
class Animal:
def __init__(self, name, height, weight, sound):
self._Animal__name = name
self._Animal__height = height
self._Animal__weight = weight
self._Animal__sound = sound
不管它在哪里被调用,或者是谁调用了 __init__
,前缀 _Animal
都会出现,因为它实际位于 Animal
class 之下.
但是当你在这里使用这些属性时,因为它物理上位于 Dog
class 下面,所以得到的名字被改成了这样:
class Dog(Animal):
def __init__(self, name, height, weight, sound):
super(Dog, self).__init__(name, height, weight, sound)
def toString(self):
return "{} is {} cm tall and {} kilograms and says {}".format(self._Dog__name,
self._Dog__height,
self._Dog__weight,
self._Dog__sound)
其中 Dog
对象肯定没有名为 self._Dog__name
的属性,而是具有 self._Animal__name
.
属性
我刚开始使用 Python,但我无法弄清楚为什么我在使用如此简单的 class 继承时遇到问题,尽管我经常使用本教程我一直在关注,我还没有在 Stack Overflow 上看到其他人遇到过这个问题。这是代码(别担心,没什么太复杂的):
import random
import sys
import os
class Animal:
__name = ""
__height = 0
__weight = 0
__sound = 0
def __init__(self, name, height, weight, sound):
self.__name = name
self.__height = height
self.__weight = weight
self.__sound = sound
def toString(self):
return "{} is {} cm tall and {} kilograms and says {}".format(self.__name,
self.__height,
self.__weight,
self.__sound)
cat = Animal ('Whiskers', 33, 10, 'meow')
print(cat.toString())
bird = Animal ('Flutie', 33, 10, 'tweet')
print(bird.toString())
class Dog(Animal):
def __init__(self, name, height, weight, sound):
super(Dog, self).__init__(name, height, weight, sound)
def toString(self):
return "{} is {} cm tall and {} kilograms and says {}".format(self.__name,
self.__height,
self.__weight,
self.__sound)
spot = Dog ('Spot', 53, 27, "Woof")
print(spot.toString())
...这是输出:
Whiskers is 33 cm tall and 10 kilograms and says meow
Flutie is 33 cm tall and 10 kilograms and says tweet
Traceback (most recent call last):
File "C:/.../animal_test.py", line 72, in <module>
print(spot.toString())
File "C:/.../animal_test.py", line 65, in toString
return "{} is {} cm tall and {} kilograms and says {}".format(self.__name,
AttributeError: 'Dog' object has no attribute '_Dog__name'
双下划线代表name mangling。
class Animal:
def __init__(self, name, height, weight, sound):
self.__name = name
self.__height = height
self.__weight = weight
self.__sound = sound
在解释时直译为:
class Animal:
def __init__(self, name, height, weight, sound):
self._Animal__name = name
self._Animal__height = height
self._Animal__weight = weight
self._Animal__sound = sound
不管它在哪里被调用,或者是谁调用了 __init__
,前缀 _Animal
都会出现,因为它实际位于 Animal
class 之下.
但是当你在这里使用这些属性时,因为它物理上位于 Dog
class 下面,所以得到的名字被改成了这样:
class Dog(Animal):
def __init__(self, name, height, weight, sound):
super(Dog, self).__init__(name, height, weight, sound)
def toString(self):
return "{} is {} cm tall and {} kilograms and says {}".format(self._Dog__name,
self._Dog__height,
self._Dog__weight,
self._Dog__sound)
其中 Dog
对象肯定没有名为 self._Dog__name
的属性,而是具有 self._Animal__name
.