在可以重定义的类中声明常量的最佳方法是什么

时间:2020-10-16 16:40:03

标签: constants eiffel

拥有可以重新定义的常数的最佳方法是什么?

  • A类=>颜色:STRING =“绿色”
  • 颜色B继承A =>无法重新定义

具有仅返回“绿色”或“蓝色”的函数时,是否需要再次创建字符串,性能问题还是没关系?

据我了解,曾经无法再次宣布...

1 个答案:

答案 0 :(得分:2)

可以将特征重新定义为任何其他特征。例子是

class A feature
    color: STRING once Result := "green" end
end

class B inherit A redefine color end feature
    color: STRING once Result := "blue" end
end

此外,清单字符串本身也可以定义一次:

class A feature
    color: STRING do Result := once "green" end
end

class B inherit A redefine color end feature
    color: STRING do Result := once "blue" end
end

两种情况下的行为相同,因此您甚至可以混合使用两种变体。性能可能有所不同,但仅略有不同。在这两种情况下,都不会在每次调用时都创建新的字符串。