可重写常量?
也许我应该将其设为公共成员,但我一直认为它应该是一个常量。
场景
我正在将表单渲染为 PDF(使用 iTextSharp)。在代码中,表单本身都继承一个基类 (OrderForm
)。在 OrderForm
中,有一个常量列表,我在对齐 PDF 渲染时使用这些常量进行测量,例如 LEFT_MARGIN
等。这些常量在多个函数中使用确定事情的去向。
所有表单实际上都是相同的,因为它们由相同的组件(标题、地址框、项目等)组成。然而,表格的布局略有不同......在一个表格上,地址框向右一英寸(以适合我雇主使用的信封窗口)。
ISSUE
而不是为基类中的每个表单创建大量常量,例如:PURCHASE_ORDER_LEFT_MARGIN
、INVOICE_LEFT_MARGIN
等等,创建一个可以在“inheritee”[原文如此]对象中设置的可重写的LEFT_MARGIN
不是更好吗?我的推理是,它是一个常量,不会在该对象内发生变化,只是形式不同,但基类中的渲染将保持相对于该常量的关系。
我知道我可以简单地创建一个公共成员并设置其值,但我想知道执行此操作的正确方法。
谢谢。
Maybe I should just make this a public member, but I keep thinking it should be a constant.
SCENARIO
I have forms I'm rendering into PDFs (using iTextSharp). In code, the forms themselves all inherit a base class (OrderForm
). Within OrderForm
, there are a list of constants that I use for measurements when aligning things for the PDF rendering, such as LEFT_MARGIN
, etc. These constants are used in several functions to determine where things go.
All the forms are virtually identical as they are comprised of the same components (header, address boxes, items, etc). However, the layout of the forms differ slightly... on one form, the address box is an inch further to the right (to fit in the envelope window my employer uses).
ISSUE
Rather than create a slew of constants for each margin, padding, blah blah for each form in the base class, eg: PURCHASE_ORDER_LEFT_MARGIN
, INVOICE_LEFT_MARGIN
, etc, wouldn't it be better to create an overridable LEFT_MARGIN
that can be set in the "inheritee" [sic] object? My reasoning is that it is a constant that will not change within that object, only form to form, yet the renderings in the base class will remain relative to whatever that constant is.
I know I could simply create a public member and set its value, but I'd like to know the right way to go about this.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
常量是隐式静态的(共享)。
使用只读属性,然后您可以选择随时随地覆盖它。
一个简单的例子......
Constants are implicitly static (Shared).
Use a Readonly Property instead, and then you can choose to override it whenever or wherever need be.
A quick example ....
你想要的是一个常规属性......任何改变的值都不是常量......:)
What you want is a regular property ... Any value that changes is not a constant ... :)
常量被编译为文字值。如果您有 这个(C#)源代码:
然后编译器将生成以下内容:
如您所见,“ConstString”完全消失了,它的文字值被插入到各处。
因此,使用 VB.net 等效的只读虚拟属性(我认为“虚拟”在 VB.net 中称为“可重写”?)并重写它。
Constants are compiled as literal values. If you have this (C#) source code:
then the compiler will produce this:
As you see, "ConstString" is completely GONE and it's literal value is inserted everywhere.
So use the VB.net equivalent of a ReadOnly Virtual Property (I think "virtual" is called "Overridable" in VB.net?) and override it.
你能做的就是使用方法而不是常量。在基类中,该方法将返回当前常量的值。
继承类可以通过重写相应的方法来为“常量”提供新的值。
What you can do is to use methods instead of constants. At the base class the method will return the value of the constant as it is now.
Inheriting class can provide a new value for the "constant" by overriding the corresponding method.