可重写常量?

发布于 2024-08-16 16:13:08 字数 766 浏览 4 评论 0原文

也许我应该将其设为公共成员,但我一直认为它应该是一个常量。

场景

我正在将表单渲染为 PDF(使用 iTextSharp)。在代码中,表单本身都继承一个基类 (OrderForm)。在 OrderForm 中,有一个常量列表,我在对齐 PDF 渲染时使用这些常量进行测量,例如 LEFT_MARGIN 等。这些常量在多个函数中使用确定事情的去向。

所有表单实际上都是相同的,因为它们由相同的组件(标题、地址框、项目等)组成。然而,表格的布局略有不同......在一个表格上,地址框向右一英寸(以适合我雇主使用的信封窗口)。

ISSUE

而不是为基类中的每个表单创建大量常量,例如:PURCHASE_ORDER_LEFT_MARGININVOICE_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

虚拟世界 2024-08-23 16:13:09

常量是隐式静态的(共享)

使用只读属性,然后您可以选择随时随地覆盖它。

一个简单的例子......

Class BaseClass

    ' Assume this field is set with a value from somewhere else'
    Private _leftmargin As Integer

    Overridable Readonly Property LEFT_MARGIN  As Integer
        Get
            Return _leftmargin
        End Get
    End Property
End Class

Class DerivedClass1
    Inherits BaseClass

    Overrides Readonly Property LEFT_MARGIN  As Integer
        Get
            Return 5 'specialized case for this class'
        End Get
    End Property
End Class

Class DerivedClass2
    Inherits BaseClass

    'uses base class LEFT_MARGIN'

End Class

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 ....

Class BaseClass

    ' Assume this field is set with a value from somewhere else'
    Private _leftmargin As Integer

    Overridable Readonly Property LEFT_MARGIN  As Integer
        Get
            Return _leftmargin
        End Get
    End Property
End Class

Class DerivedClass1
    Inherits BaseClass

    Overrides Readonly Property LEFT_MARGIN  As Integer
        Get
            Return 5 'specialized case for this class'
        End Get
    End Property
End Class

Class DerivedClass2
    Inherits BaseClass

    'uses base class LEFT_MARGIN'

End Class
梦里南柯 2024-08-23 16:13:09

你想要的是一个常规属性......任何改变的值都不是常量......:)

What you want is a regular property ... Any value that changes is not a constant ... :)

丿*梦醉红颜 2024-08-23 16:13:09

常量被编译为文字值。如果您有 这个(C#)源代码

public static class MyStringTestClass
{
    // Fields
    public const string ConstString = "Const String";

    public void TestMethod()
    {
        string sc = MyStringTestClass.ConstString;
        SomeOtherFunction(sc);
    }
}

然后编译器将生成以下内容:

public static class MyStringTestClass
{
    // I'm not 100% sure if this declaration is removed as well...
    public const string ConstString = "Const String";

    public void TestMethod()
    {
        // ...but I know for sure that it will be removed
        // everywhere it's used - including other assemblies!
        string sc = "Const String";
        SomeOtherFunction(sc);
    }
}

如您所见,“ConstString”完全消失了,它的文字值被插入到各处。

因此,使用 VB.net 等效的只读虚拟属性(我认为“虚拟”在 VB.net 中称为“可重写”?)并重写它。

Constants are compiled as literal values. If you have this (C#) source code:

public static class MyStringTestClass
{
    // Fields
    public const string ConstString = "Const String";

    public void TestMethod()
    {
        string sc = MyStringTestClass.ConstString;
        SomeOtherFunction(sc);
    }
}

then the compiler will produce this:

public static class MyStringTestClass
{
    // I'm not 100% sure if this declaration is removed as well...
    public const string ConstString = "Const String";

    public void TestMethod()
    {
        // ...but I know for sure that it will be removed
        // everywhere it's used - including other assemblies!
        string sc = "Const String";
        SomeOtherFunction(sc);
    }
}

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.

假扮的天使 2024-08-23 16:13:09

你能做的就是使用方法而不是常量。在基类中,该方法将返回当前常量的值。

继承类可以通过重写相应的方法来为“常量”提供新的值。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文