Java枚举方法到Delphi [英] Java enum method to Delphi

查看:114
本文介绍了Java枚举方法到Delphi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public enum HTTPHeaderKey {
    CACHE_CONTROL("Cache-Control"), CONNECTION("Connection"), TRANSFER_ENCODING("Transfer-Encoding"), HOST("Host"), USER_AGENT("User-Agent"), CONTENT_LENGTH("Content-Length"), CONTENT_TYPE("Content-Type");
    private final String str;

    private HTTPHeaderKey(final String _str) {
        str = _str;
    }

    /** Over ridden toString returns the HTTP/1.1 compatible header */
    public String toString() {
        return str;
    }
};

我正在尝试将此枚举转换为Delphi。我知道如何定义枚举变量,但我不知道,我如何在枚举中插入一个方法?

I am trying to convert this enum to Delphi. I know that how to define enum variables but i have no idea, how can i insert a method in enum?

还是有人建议另一种方法来转换? p>

Or can someone suggest another way to convert this ?

推荐答案

您可以使用一个记录助手,从XE3起可用于值类型。例如:

You can get part way there with a record helper, which is available for value types from XE3 onwards. For instance:

{$APPTYPE CONSOLE}

uses
  System.SysUtils, System.TypInfo;

type
  TMyEnum = (enumValue1, enumValue2);

  TMyEnumHelper = record helper for TMyEnum
  public
    function ToString: string;
  end;

function TMyEnumHelper.ToString: string;
begin
  Result := GetEnumName(TypeInfo(TMyEnum), ord(Self));
end;

begin
  Writeln(enumValue1.ToString);
  Writeln(enumValue2.ToString);
end.

此程序输出以下内容:


enumValue1
enumValue2

当然,您可能喜欢这样做:

Of course you may prefer to do it like this:

{$APPTYPE CONSOLE}

uses
  System.SysUtils, System.TypInfo;

type
  TMyEnum = (enumValue1, enumValue2);

  TMyEnumHelper = record helper for TMyEnum
  private
    const
      EnumNames: array [TMyEnum] of string = ('Friendly name 1', 'Friendly name 2');
  public
    function ToString: string;
  end;

function TMyEnumHelper.ToString: string;
begin
  Result := EnumNames[Self];
end;

begin
  Writeln(enumValue1.ToString);
  Writeln(enumValue2.ToString);
end.

这里的输出是:


Friendly name 1
Friendly name 2

这可能会让您解决Delphi列举的事实类型不支持Java中可用的文本命名。

This would presumably allow you to tackle the fact the Delphi enumerated types don't support the textual naming that is available in Java.

其他方法,构造函数 HTTPHeaderKey 不能被枚举类型支持。原因是它需要状态,Delphi枚举类型的唯一状态是枚举类型值本身。您不能像在Java代码中那样在一个额外的实例变量上移植。

The other method, the constructor HTTPHeaderKey cannot be supported with an enumerated type. The reason being that it requires state, and the only state for a Delphi enumerated type is the enumerated type value itself. You cannot graft on an extra instance variable as is done in the Java code.

所有考虑的事情,我不认为使用枚举类型的文字翻译尝试会锻炼出来我建议您使用记录或类进行翻译,并使用可用的Delphi语言构造构建等效功能。

All things considered, I don't think an attempt at a literal translation with an enumerated type will work out. I suggest that you translate using either a record or a class, and build the equivalent functionality using the available Delphi language constructs.

这篇关于Java枚举方法到Delphi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆