Python is-a和has-a的区别是什么?
我正在学习python,我必须用is-a或has-a来填补空白,但我还不太清楚。所以我需要下面的例子来解释为什么它们是这样的 下面是一些例子。填空的是###的Python is-a和has-a的区别是什么?,python,Python,我正在学习python,我必须用is-a或has-a来填补空白,但我还不太清楚。所以我需要下面的例子来解释为什么它们是这样的 下面是一些例子。填空的是###的 我想你正在寻找: 动物是一种物体 狗是一种动物 狗有一个名字 猫是一种动物 猫有一个名字 人是一个物体 一个人有一个名字 一个人有一只宠物 我想你正在寻找: 动物是一种物体 狗是一种动物 狗有一个名字 猫是一种动物 猫有一个名字 人是一个物体 一个人有一个名字 一个人有一只宠物 这方面的技术术语是“继承”(“is-a”)与“组合”(“ha
我想你正在寻找: 动物是一种物体 狗是一种动物 狗有一个名字 猫是一种动物 猫有一个名字 人是一个物体 一个人有一个名字 一个人有一只宠物
我想你正在寻找: 动物是一种物体 狗是一种动物 狗有一个名字 猫是一种动物 猫有一个名字 人是一个物体 一个人有一个名字 一个人有一只宠物
这方面的技术术语是“继承”(“is-a”)与“组合”(“has-a”) 在任何特定的情况下,哪一个更好都有很多争论。对于OOP的经典
车型
,您可以:
class Car(object): # everything inherits from object in Py3
# stuff related to being a car
class TwoWheelDrive(Car):
# This car has two wheels that drive, so maybe
def accelerate(self, vector):
for wheel in [self.front_left_wheel, self.front_right_wheel]:
wheel.RPM += vector
class FourWheelDrive(Car):
def accelerate(self, vector):
for wheel in [self.front_left_wheel, self.front_right_wheel,
self.rear_left_wheel, self.rear_right_wheel]:
wheel.RPM += vector
my_car = FourWheelDrive(color='red',speed='super fast',coolness='very')
my_car.accelerate(float('inf')) # runs FourWheelDrive().accelerate(float('inf'))
或者,您可以构建汽车拥有的接口,而不是所有其他汽车的不同类型的汽车
class Wheel(object):
# would probably have properties like RPM, traction, etc
class TwoWheelDriveInterface(object):
def __init__(self, *args):
self.front_left_wheel = Wheel(*args)
self.front_right_wheel = Wheel(*args)
self.rear_left_wheel = Wheel(*args)
self.rear_right_wheel = Wheel(*args)
self.wheels = [self.front_left_wheel, self.front_right_wheel,
self.rear_left_wheel, self.rear_right_wheel]
def accelerate(self, vector):
for wheel in [self.front_left_wheel, self.front_right_wheel]:
wheel.RPM += vector
class FourWheelDriveInterface(object):
def __init__(self, *args):
# same as TwoWheelDriveInterface. In fact these could both
# inherit from a common DriveInterface class and override
# accelerate! (that is, both TwoWheelDriveInterface and
# FourWheelDriveInterace ARE-A DriveInterface)
def accelerate(self, vector):
for wheel in self.wheels:
wheel.RPM += vector
class Car(object):
def __init__(self, color,speed,coolness,two_wheel_drive=False,*wheelargs):
self.color = color
self.speed = speed
self.coolness = coolness
if two_wheel_drive:
self.driveinterface = TwoWheelDrive(*wheelargs)
else:
self.driveinterface = FourWheelDrive(*wheelargs)
self.accelerate = self.driveinterface.accelerate # bind the methods together
my_car = Car(color='red',speed='very fast',coolness='very',two_wheel_drive=True)
my_car.accelerate(float('inf')) # runs TwoWheelDriveInterface().accelerate(float('inf'))
另一个常见的例子是动物园。你可能有很多动物
class Animal(object):
# attributes all animals have. Name, species, color, sound, etc?
class Kangaroo(Animal):
# override attributes from Animal to make them specific to Kangas
class Lion(Animal):
# see above
class Hyena(Animal):
class Monkey(Animal):
class Chimp(Monkey):
# Chimps are-a monkey, which is-an Animal
class Bear(Animal):
# etc etc ad nauseam, however....
class Zoo(Animal): # NO!
动物园不是动物。它可能具有与动物相同的几个特征,甚至可能使代码乍看起来更清晰。毕竟,“干”是吧?我们可以忽略我们不需要的东西。然而,这显然是一个组合的情况,而不是继承的情况。动物园有动物,但它本身不是动物
class Zoo(object):
def __init__(self, zoo_name, zoo_location, zoo_animals):
self.name = zoo_name
self.location = zoo_location
self.animals = zoo_animals
# zoo_animals in this case should be a list of Animal objects
ny_zoo = Zoo("New York Zoo", "New York", [Bear(), Lion(), Chimp(), Kangaroo()])
# ny_zoo HAS the following animals:
# # Bear
# # Lion
# # Chimp
# # Kangaroo
这方面的技术术语是“继承”(“is-a”)与“组合”(“has-a”) 在任何特定的情况下,哪一个更好都有很多争论。对于OOP的经典
车型
,您可以:
class Car(object): # everything inherits from object in Py3
# stuff related to being a car
class TwoWheelDrive(Car):
# This car has two wheels that drive, so maybe
def accelerate(self, vector):
for wheel in [self.front_left_wheel, self.front_right_wheel]:
wheel.RPM += vector
class FourWheelDrive(Car):
def accelerate(self, vector):
for wheel in [self.front_left_wheel, self.front_right_wheel,
self.rear_left_wheel, self.rear_right_wheel]:
wheel.RPM += vector
my_car = FourWheelDrive(color='red',speed='super fast',coolness='very')
my_car.accelerate(float('inf')) # runs FourWheelDrive().accelerate(float('inf'))
或者,您可以构建汽车拥有的接口,而不是所有其他汽车的不同类型的汽车
class Wheel(object):
# would probably have properties like RPM, traction, etc
class TwoWheelDriveInterface(object):
def __init__(self, *args):
self.front_left_wheel = Wheel(*args)
self.front_right_wheel = Wheel(*args)
self.rear_left_wheel = Wheel(*args)
self.rear_right_wheel = Wheel(*args)
self.wheels = [self.front_left_wheel, self.front_right_wheel,
self.rear_left_wheel, self.rear_right_wheel]
def accelerate(self, vector):
for wheel in [self.front_left_wheel, self.front_right_wheel]:
wheel.RPM += vector
class FourWheelDriveInterface(object):
def __init__(self, *args):
# same as TwoWheelDriveInterface. In fact these could both
# inherit from a common DriveInterface class and override
# accelerate! (that is, both TwoWheelDriveInterface and
# FourWheelDriveInterace ARE-A DriveInterface)
def accelerate(self, vector):
for wheel in self.wheels:
wheel.RPM += vector
class Car(object):
def __init__(self, color,speed,coolness,two_wheel_drive=False,*wheelargs):
self.color = color
self.speed = speed
self.coolness = coolness
if two_wheel_drive:
self.driveinterface = TwoWheelDrive(*wheelargs)
else:
self.driveinterface = FourWheelDrive(*wheelargs)
self.accelerate = self.driveinterface.accelerate # bind the methods together
my_car = Car(color='red',speed='very fast',coolness='very',two_wheel_drive=True)
my_car.accelerate(float('inf')) # runs TwoWheelDriveInterface().accelerate(float('inf'))
另一个常见的例子是动物园。你可能有很多动物
class Animal(object):
# attributes all animals have. Name, species, color, sound, etc?
class Kangaroo(Animal):
# override attributes from Animal to make them specific to Kangas
class Lion(Animal):
# see above
class Hyena(Animal):
class Monkey(Animal):
class Chimp(Monkey):
# Chimps are-a monkey, which is-an Animal
class Bear(Animal):
# etc etc ad nauseam, however....
class Zoo(Animal): # NO!
动物园不是动物。它可能具有与动物相同的几个特征,甚至可能使代码乍看起来更清晰。毕竟,“干”是吧?我们可以忽略我们不需要的东西。然而,这显然是一个组合的情况,而不是继承的情况。动物园有动物,但它本身不是动物
class Zoo(object):
def __init__(self, zoo_name, zoo_location, zoo_animals):
self.name = zoo_name
self.location = zoo_location
self.animals = zoo_animals
# zoo_animals in this case should be a list of Animal objects
ny_zoo = Zoo("New York Zoo", "New York", [Bear(), Lion(), Chimp(), Kangaroo()])
# ny_zoo HAS the following animals:
# # Bear
# # Lion
# # Chimp
# # Kangaroo
找到本教程的原因是:这是我正在学习的内容,但他给我的解释不清楚。@Robᵩ, 问题是is-a和has-a之间有什么区别。发现本教程的内容是:这就是我正在学习的内容,但他给出的解释我并不清楚。@Robᵩ, 问题是is-a和has-a有什么区别。谢谢,我现在明白了。当它以代码的形式出现时,它对我来说并没有那么快。谢谢谢谢,我现在明白了。当它以代码的形式出现时,它对我来说并没有那么快。谢谢