SpecialistOff.NET / Вопросы / Статьи / Фрагменты кода / Резюме / Метки / Помощь / Файлы
НазадМетки: python
To fit into the prototyping scheme, the only thing that’s required of each new subclass of Trash is that it contain a constructor that takes a doubleargument. Java reflection handles everything else.
Here are the different types of Trash, each in their own file but part of the Trash package (again, to facilitate reuse within the chapter):
# PatternRefactoring/trash/Aluminum.py # The Aluminum class with prototyping. class Aluminum(Trash): val = 1.67f def __init__(self, wt): Trash.__init__(wt) def getValue(self): return val def setValue(self, newVal): self.val = newVal:: # PatternRefactoring/trash/Paper.py # The Paper class with prototyping. class Paper(Trash): val = 0.10f def __init__(self, wt): Trash.__init__(wt) def getValue(self): return self.val def setValue(self, newVal): self.val = newVal:: # PatternRefactoring/trash/Glass.py # The Glass class with prototyping. class Glass(Trash): val = 0.23f def __init__(self, wt): Trash.__init__(wt) def getValue(self): return self.val def setValue(self, newVal): self.val = newVal
And here’s a new type of Trash:
# PatternRefactoring/trash/Cardboard.py # The Cardboard class with prototyping. class Cardboard(Trash): val = 0.23f def __init__(self, wt): Trash.__init__(wt) def getValue(self): return self.val def setValue(self, newVal): self.val = newVal
You can see that, other than the constructor, there’s nothing special about any of these classes.