2015-02-09 15 views
7
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; 
    } 
}; 

Sto provando a convertire questo enum in Delphi. So come definire le variabili enum ma non ne ho idea, come posso inserire un metodo in enum?metodo enum Java a Delphi

Oppure qualcuno può suggerire un altro modo per convertire questo?

risposta

8

È possibile ottenere part-way con un helper record, che è disponibile per i tipi di valore da XE3 in poi. Per esempio:

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

Questo programma emette il seguente:

 
enumValue1 
enumValue2 

Naturalmente è preferibile farlo in questo modo:

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

L'output è:

 
Friendly name 1 
Friendly name 2 

Questo presumibilmente consente di affrontare il fatto che i tipi enumerati Delphi non supportano la denominazione testuale disponibile in Java.

L'altro metodo, il costruttore HTTPHeaderKey non può essere supportato con un tipo enumerato. Il motivo è che richiede lo stato e l'unico stato per un tipo enumerato Delphi è il valore del tipo enumerato stesso. Non è possibile eseguire l'innesto su una variabile di istanza aggiuntiva come è fatto nel codice Java.

Tutto considerato, non penso che un tentativo di una traduzione letterale con un tipo enumerato funzionerà. Suggerisco di tradurre usando un record o una classe e creare la funzionalità equivalente usando i costrutti del linguaggio Delphi disponibili.

+1

Wow. Non sapevo che è possibile utilizzare gli helper dei record su enumerazione +1 –

+6

@markus_ja 20 minuti fa non sapevo che sia –

+0

grazie per la risposta, ho capito :) –

1

Sulla base di Davids risposta, questa versione modificata dovrebbe essere in grado di stampare i nomi delle intestazioni HTTP:

dovrebbe uscita

Cache-Control 
Transfer-Encoding