将所有信息放入列表而不是 OOP 中的 class 继承是不好的做法吗?
Is it bad practice to put all information into lists instead of class inheritance in OOP?
使用列表可以节省很多行代码,但它不会扼杀 OOP 背后的理念吗?因为使用函数式编程来实现它会更有意义。
在Python中使用列表的例子:
class Pets:
def printlist(self, petslist):
for pet in petslist:
print pet[0]+ " is a "+pet[1]
pet = Pets()
pet.printlist([('parrot','bird'),('snake','reptile'),('dog','mammal')])is a "+pet[1]
使用class继承的例子:
class Pets:
def __init__(self, pet, petclass):
self.petclass=petclass
self.pet=pet
def printlist(self):
print self.pet+" is a "+self.petclass
class Dog(Pets):
def __init__(self):
Pets.__init__(self, "dog", "mammal")
class Parrot(Pets):
def __init__(self):
Pets.__init__(self, "parrot", "bird")
class Snake(Pets):
def __init__(self):
Pets.__init__(self, "snake", "reptile")
x = Dog()
x.printlist()
y = Parrot()
y.printlist()
z = Snake()
z.printlist()
此外,如果列表太大而无法通过继承处理怎么办?使用可以从文本文件或其他文件导入的列表不是更容易吗?
这是否会使 OOP 在这种情况下变得无用?还是我在这里遗漏了什么?
是的,我认为这是一种不好的做法。没有验证列表有两个字符串,甚至根本不包含字符串。假设我传递了一个包含三个字符串的列表,或者一个,或者空列表?然后呢?
但除此之外,您将放弃 OOP 的大部分优势。这些 类 只是名称,没有与之关联的行为。没有继承的概念,也没有明确的方法将其他信息与对象相关联。你会把它存储在列表中的其他位置吗?也没有信息隐藏,因此无法确保任何不变量。有权访问列表的任何人都可以随时更改它们。
您不妨存储 "snake is a reptile"、"dog is a mammal" 和 "parrot is a bird" 等字符串,而不是使用列表。
这是一个在试图决定对象的用途时遇到的问题。
如果对象是 'Store' 某些数据,则对象必须以特定方式运行。
在上面的示例中,如果您的目的只是存储数据并打印它们(使用标准方法),请使用 'list'。
这样就够了。
另一方面,如果您希望每个 'variety' 表现 不同,请使用 类.
class Pet:
def makeSound(self, args):
None
class Dog(Pet):
def makeSound(self, args):
'Specific behaviour ONLY for Dogs.
class Parrot(Pet):
def makeSound(self, args):
'Specific behaviour ONLY for Parrots.
如果您想对每种宠物实施不同的 'printlist',您可以考虑 'Classes'。
使用列表可以节省很多行代码,但它不会扼杀 OOP 背后的理念吗?因为使用函数式编程来实现它会更有意义。
在Python中使用列表的例子:
class Pets:
def printlist(self, petslist):
for pet in petslist:
print pet[0]+ " is a "+pet[1]
pet = Pets()
pet.printlist([('parrot','bird'),('snake','reptile'),('dog','mammal')])is a "+pet[1]
使用class继承的例子:
class Pets:
def __init__(self, pet, petclass):
self.petclass=petclass
self.pet=pet
def printlist(self):
print self.pet+" is a "+self.petclass
class Dog(Pets):
def __init__(self):
Pets.__init__(self, "dog", "mammal")
class Parrot(Pets):
def __init__(self):
Pets.__init__(self, "parrot", "bird")
class Snake(Pets):
def __init__(self):
Pets.__init__(self, "snake", "reptile")
x = Dog()
x.printlist()
y = Parrot()
y.printlist()
z = Snake()
z.printlist()
此外,如果列表太大而无法通过继承处理怎么办?使用可以从文本文件或其他文件导入的列表不是更容易吗?
这是否会使 OOP 在这种情况下变得无用?还是我在这里遗漏了什么?
是的,我认为这是一种不好的做法。没有验证列表有两个字符串,甚至根本不包含字符串。假设我传递了一个包含三个字符串的列表,或者一个,或者空列表?然后呢?
但除此之外,您将放弃 OOP 的大部分优势。这些 类 只是名称,没有与之关联的行为。没有继承的概念,也没有明确的方法将其他信息与对象相关联。你会把它存储在列表中的其他位置吗?也没有信息隐藏,因此无法确保任何不变量。有权访问列表的任何人都可以随时更改它们。
您不妨存储 "snake is a reptile"、"dog is a mammal" 和 "parrot is a bird" 等字符串,而不是使用列表。
这是一个在试图决定对象的用途时遇到的问题。 如果对象是 'Store' 某些数据,则对象必须以特定方式运行。
在上面的示例中,如果您的目的只是存储数据并打印它们(使用标准方法),请使用 'list'。 这样就够了。
另一方面,如果您希望每个 'variety' 表现 不同,请使用 类.
class Pet:
def makeSound(self, args):
None
class Dog(Pet):
def makeSound(self, args):
'Specific behaviour ONLY for Dogs.
class Parrot(Pet):
def makeSound(self, args):
'Specific behaviour ONLY for Parrots.
如果您想对每种宠物实施不同的 'printlist',您可以考虑 'Classes'。