-1
class Animal:
__name = None
__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 set_name(self, name):
self.__name = name
def get_name(self):
return self.__name
def set_height(self, height):
self.__height = height
def get_height(self):
return self.__height
def set_weight(self, weight):
self.__weight = weight
def get_weight(self):
return self.__weight
def set_sound(self, sound):
self.__sound = sound
def get_sound(self):
return self.__sound
def get_type(self):
print('Animal')
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())
class Dog(Animal):
__owner = None
def __init__(self, name, height, weight, sound, owner):
self.__owner = owner
super(Dog, self).__init__(name, height, weight, sound)
def set_owner(self, owner):
self.__owner = owner
def get_owner(self):
return self.__owner
def get_type(self):
print('Dog')
def toString(self):
return '{} is {} cm tall and {} kilograms and says {}. His owner is {}'.format(self.set_name(),
self.set_height(),
self.set_weight(),
self.set_sound(),
self.set_owner())
def multiple_sounds(self, how_many=None):
if how_many is None:
print(self.get_sound())
else:
print(self.get_sound() * how_many)
spot = Dog('Spot', 53, 27, 'Ruff', 'Derek')
print(spot.toString())
継承時にコンストラクタに問題はありますか? 66行目でこのエラーが発生するのはなぜですか? エラーは次のとおりです。TypeError:set_name()missing 1必要な位置引数: 'name'
TypeError: set_name() missing 1 required positional argument: 'name'
は別のエラーがライン80
なぜstatic initを使用していますか? '__name = None'と他の? – nikniknik2016
を初期化するコンストラクタがあるので、これを使う必要はありません。これは実際にPythonでクラスを書く方法ではありません。 –