请问有没有好用的JAVA代码转成DELPHI的OBJECT PASCAL代码的工具?(20分)

  • 主题发起人 主题发起人 TENTODBV
  • 开始时间 开始时间
T

TENTODBV

Unregistered / Unconfirmed
GUEST, unregistred user!
我想把一个用JAVA写的带源码的程序转成DELPHI的,请问有没有好的工具?
 
还有这样的工具???
 
下面代码是JAVA的,请帮忙转成DELPHI的。

public final class Turn {
public static final int QUARTER_METRIC = 0;
public static final int FACE_METRIC = 1;
public static final int SLICE_METRIC = 2;

public static final int A_U1 = 0;
public static final int A_U2 = 1;
public static final int A_U3 = 2;
public static final int A_D1 = 3;
public static final int A_D2 = 4;
public static final int A_D3 = 5;
public static final int A_F1 = 6;
public static final int A_F2 = 7;
public static final int A_F3 = 8;
public static final int A_B1 = 9;
public static final int A_B2 = 10;
public static final int A_B3 = 11;
public static final int A_L1 = 12;
public static final int A_L2 = 13;
public static final int A_L3 = 14;
public static final int A_R1 = 15;
public static final int A_R2 = 16;
public static final int A_R3 = 17;
public static final int A_E1 = 18;
public static final int A_E2 = 19;
public static final int A_E3 = 20;
public static final int A_S1 = 21;
public static final int A_S2 = 22;
public static final int A_S3 = 23;
public static final int A_M1 = 24;
public static final int A_M2 = 25;
public static final int A_M3 = 26;
public static final int A_NUM = 27;

public static final int B_U1 = 0;
public static final int B_U2 = 1;
public static final int B_U3 = 2;
public static final int B_D1 = 3;
public static final int B_D2 = 4;
public static final int B_D3 = 5;
public static final int B_F2 = 6;
public static final int B_B2 = 7;
public static final int B_L2 = 8;
public static final int B_R2 = 9;
public static final int B_E1 = 10;
public static final int B_E2 = 11;
public static final int B_E3 = 12;
public static final int B_S2 = 13;
public static final int B_M2 = 14;
public static final int B_NUM = 15;

public static final int MASK_U = 256;
public static final int MASK_D = 128;
public static final int MASK_F = 64;
public static final int MASK_B = 32;
public static final int MASK_L = 16;
public static final int MASK_R = 8;
public static final int MASK_E = 4;
public static final int MASK_S = 2;
public static final int MASK_M = 1;
public static final int MASK_U2 = 131072;
public static final int MASK_D2 = 65536;
public static final int MASK_F2 = 32768;
public static final int MASK_B2 = 16384;
public static final int MASK_L2 = 8192;
public static final int MASK_R2 = 4096;
public static final int MASK_E2 = 2048;
public static final int MASK_S2 = 1024;
public static final int MASK_M2 = 512;

public final int[] lengthA = new int[A_NUM];
public final int[] lengthB = new int[B_NUM];

public static final int[] turnMaskA = {
MASK_U, MASK_U | MASK_U2, MASK_U,
MASK_D, MASK_D | MASK_D2, MASK_D,
MASK_F, MASK_F | MASK_F2, MASK_F,
MASK_B, MASK_B | MASK_B2, MASK_B,
MASK_L, MASK_L | MASK_L2, MASK_L,
MASK_R, MASK_R | MASK_R2, MASK_R,
MASK_E, MASK_E | MASK_E2, MASK_E,
MASK_S, MASK_S | MASK_S2, MASK_S,
MASK_M, MASK_M | MASK_M2, MASK_M,
};
public static final int[] turnMaskB = {
MASK_U, MASK_U | MASK_U2, MASK_U,
MASK_D, MASK_D | MASK_D2, MASK_D,
MASK_F,
MASK_B,
MASK_L,
MASK_R,
MASK_E, MASK_E | MASK_E2, MASK_E,
MASK_S,
MASK_M,
};
public static final int[] b2a = {
A_U1, A_U2, A_U3,
A_D1, A_D2, A_D3,
A_F2,
A_B2,
A_L2,
A_R2,
A_E1, A_E2, A_E3,
A_S2,
A_M2
};
public static final int[] a2b = {
B_U1, B_U2, B_U3,
B_D1, B_D2, B_D3,
-1, B_F2, -1,
-1, B_B2, -1,
-1, B_L2, -1,
-1, B_R2, -1,
B_E1, B_E2, B_E3,
-1, B_S2, -1,
-1, B_M2, -1
};
public static final boolean[] inB = {
true, true, true,
true, true, true,
false, true, false,
false, true, false,
false, true, false,
false, true, false,
true, true, true,
false, true, false,
false, true, false,
};
public static final int[] quarter = {
A_U1, A_U3, A_D1, A_D3, A_F1, A_F3, A_B1, A_B3, A_L1, A_L3, A_R1, A_R3
};
public static final int[] face = {
A_U2, A_D2, A_F2, A_B2, A_L2, A_R2
};
public static final int[] slice = {
A_E1, A_E3, A_S1, A_S3, A_M1, A_M3
};
public static final int[] slice2 = {
A_E2, A_S2, A_M2
};

public static final String[] metricString = {
"Quarter", "Face", "Slice"
};

private static final Turn turnQuarter = new Turn(QUARTER_METRIC);
private static final Turn turnFace = new Turn(FACE_METRIC);
private static final Turn turnSlice = new Turn(SLICE_METRIC);

private Turn(int metric) {
for (int i = 0
i < A_NUM
i++) {
lengthA = 1;
}
if (metric == QUARTER_METRIC) {
for (int i = 0
i < face.length
i++)
lengthA[face] = 2;
for (int i = 0
i < slice.length
i++)
lengthA[slice] = 2;
for (int i = 0
i < slice2.length
i++)
lengthA[slice2] = 4;
}
else if (metric == FACE_METRIC) {
for (int i = 0
i < slice.length
i++)
lengthA[slice] = 2;
for (int i = 0
i < slice2.length
i++)
lengthA[slice2] = 2;
}
for (int i = 0
i < b2a.length
i++) {
lengthB = lengthA[b2a];
}
}

public static Turn turn(int metric) {
switch (metric) {
case QUARTER_METRIC:
return turnQuarter;
case FACE_METRIC:
return turnFace;
case SLICE_METRIC:
return turnSlice;
default:
throw new IllegalArgumentException(&quot;Invalid metric&quot;);
}
}
}
 
最好的工具是自己編寫。
 
两个都精通的人不多
你说说功能,也许大家可以给你用Delphi写
 
主要是语法知识问题。以上JAVA代码定义了一个类。
在类Turn里定义了许多类似下面这样的常量
public static final int QUARTER_METRIC = 0;
public static final int FACE_METRIC = 1;
public static final int SLICE_METRIC = 2;
public static final int A_NUM = 27;
public static final int A_E2 = 19;
public static final int A_S2 = 22;
public static final int A_M2 = 25;
定义了许多类似下面这样的数组
public static final int[] slice2 = {A_E2, A_S2, A_M2};
public final int[] lengthA = new int[A_NUM];

private static final Turn turnQuarter = new Turn(QUARTER_METRIC);
private static final Turn turnFace = new Turn(FACE_METRIC);
private static final Turn turnSlice = new Turn(SLICE_METRIC);

PRIVATE TURN(INT METRIC) {
FOR (INT I = 0
I < A_NUM
I++) {
LENGTHA = 1;
}
IF (METRIC == QUARTER_METRIC) {
FOR (INT I = 0
I < FACE.LENGTH
I++)
LENGTHA[FACE] = 2;
FOR (INT I = 0
I < SLICE.LENGTH
I++)
LENGTHA[SLICE] = 2;
FOR (INT I = 0
I < SLICE2.LENGTH
I++)
LENGTHA[SLICE2] = 4;
}
ELSE IF (METRIC == FACE_METRIC) {
FOR (INT I = 0
I < SLICE.LENGTH
I++)
LENGTHA[SLICE] = 2;
FOR (INT I = 0
I < SLICE2.LENGTH
I++)
LENGTHA[SLICE2] = 2;
}
FOR (INT I = 0
I < B2A.LENGTH
I++) {
LENGTHB = LENGTHA[B2A];
}
}

public static Turn turn(int metric) {
switch (metric) {
case QUARTER_METRIC:
return turnQuarter;
case FACE_METRIC:
return turnFace;
case SLICE_METRIC:
return turnSlice;
default:
throw new IllegalArgumentException(&quot;Invalid metric&quot;);
}
}
}

我直接翻译以上代码为DELPHI OBJECT PASCAL,有以下几个问题不明白:
1.以上java代码定义的类Turn中定义的常量,比如 public static final int QUARTER_METRIC = 0;在DELPHI中的类里是否也可以定义常量,我试了好像总是不行。我只好把这些常量定义放类的外面了。不知是否有区别。

2.以上JAVA代码中定义的数组中,大括号中最后有一个逗号,不知是什么意思,转成DELPHI代码时不知该怎么处理?我是忽略掉它的。
public static final int[] turnMaskA = {
MASK_U, MASK_U | MASK_U2, MASK_U,
MASK_D, MASK_D | MASK_D2, MASK_D,
MASK_F, MASK_F | MASK_F2, MASK_F,
MASK_B, MASK_B | MASK_B2, MASK_B,
MASK_L, MASK_L | MASK_L2, MASK_L,
MASK_R, MASK_R | MASK_R2, MASK_R,
MASK_E, MASK_E | MASK_E2, MASK_E,
MASK_S, MASK_S | MASK_S2, MASK_S,
MASK_M, MASK_M | MASK_M2, MASK_M,
};

3.JAVA代码中定义了一个名为Turn的方法,和一个返回Turn类型对象的turn方法。加上类本身的名称也是Turn。我都不知道该怎么处理才好。我照着语法直接把它们翻译了,并把后者该名为turn1

private Turn(int metric) {
for (int i = 0
i < A_NUM
i++) {
lengthA = 1;
}
if (metric == QUARTER_METRIC) {
for (int i = 0
i < face.length
i++)
lengthA[face] = 2;
for (int i = 0
i < slice.length
i++)
lengthA[slice] = 2;
for (int i = 0
i < slice2.length
i++)
lengthA[slice2] = 4;
}
else if (metric == FACE_METRIC) {
for (int i = 0
i < slice.length
i++)
lengthA[slice] = 2;
for (int i = 0
i < slice2.length
i++)
lengthA[slice2] = 2;
}
for (int i = 0
i < b2a.length
i++) {
lengthB = lengthA[b2a];
}
}

public static Turn turn(int metric) {
switch (metric) {
case QUARTER_METRIC:
return turnQuarter;
case FACE_METRIC:
return turnFace;
case SLICE_METRIC:
return turnSlice;
default:
throw new IllegalArgumentException(&quot;Invalid metric&quot;);
}
}

但是这行我不知该如何处理,对我来说这里是关键之处:
private static final Turn turnQuarter = new Turn(QUARTER_METRIC);

下面是我转换的代码,有劳各位大富翁指点迷津:
unit UnitTurn;

interface

const
QUARTER_METRIC = 0;
FACE_METRIC = 1;
SLICE_METRIC = 2;

A_U1 = 0;
A_U2 = 1;
A_U3 = 2;
A_D1 = 3;
A_D2 = 4;
A_D3 = 5;
A_F1 = 6;
A_F2 = 7;
A_F3 = 8;
A_B1 = 9;
A_B2 = 10;
A_B3 = 11;
A_L1 = 12;
A_L2 = 13;
A_L3 = 14;
A_R1 = 15;
A_R2 = 16;
A_R3 = 17;
A_E1 = 18;
A_E2 = 19;
A_E3 = 20;
A_S1 = 21;
A_S2 = 22;
A_S3 = 23;
A_M1 = 24;
A_M2 = 25;
A_M3 = 26;
A_NUM = 27;

B_U1 = 0;
B_U2 = 1;
B_U3 = 2;
B_D1 = 3;
B_D2 = 4;
B_D3 = 5;
B_F2 = 6;
B_B2 = 7;
B_L2 = 8;
B_R2 = 9;
B_E1 = 10;
B_E2 = 11;
B_E3 = 12;
B_S2 = 13;
B_M2 = 14;
B_NUM = 15;

MASK_U = 256;
MASK_D = 128;
MASK_F = 64;
MASK_B = 32;
MASK_L = 16;
MASK_R = 8;
MASK_E = 4;
MASK_S = 2;
MASK_M = 1;
MASK_U2 = 131072;
MASK_D2 = 65536;
MASK_F2 = 32768;
MASK_B2 = 16384;
MASK_L2 = 8192;
MASK_R2 = 4096;
MASK_E2 = 2048;
MASK_S2 = 1024;
MASK_M2 = 512;

turnMaskA :array[0..26] of integer=(
MASK_U, MASK_U or MASK_U2, MASK_U,
MASK_D, MASK_D or MASK_D2, MASK_D,
MASK_F, MASK_F or MASK_F2, MASK_F,
MASK_B, MASK_B or MASK_B2, MASK_B,
MASK_L, MASK_L or MASK_L2, MASK_L,
MASK_R, MASK_R or MASK_R2, MASK_R,
MASK_E, MASK_E or MASK_E2, MASK_E,
MASK_S, MASK_S or MASK_S2, MASK_S,
MASK_M, MASK_M or MASK_M2, MASK_M
);
turnMaskB :array[0..14] of integer=(
MASK_U, MASK_U or MASK_U2, MASK_U,
MASK_D, MASK_D or MASK_D2, MASK_D,
MASK_F,
MASK_B,
MASK_L,
MASK_R,
MASK_E, MASK_E or MASK_E2, MASK_E,
MASK_S,
MASK_M
);
b2a:array[0..14] of integer=(
A_U1, A_U2, A_U3,
A_D1, A_D2, A_D3,
A_F2,
A_B2,
A_L2,
A_R2,
A_E1, A_E2, A_E3,
A_S2,
A_M2
);
a2b:array[0..26] of integer=(
B_U1, B_U2, B_U3,
B_D1, B_D2, B_D3,
-1, B_F2, -1,
-1, B_B2, -1,
-1, B_L2, -1,
-1, B_R2, -1,
B_E1, B_E2, B_E3,
-1, B_S2, -1,
-1, B_M2, -1
);
inB:array[0..26] of boolean=(
true, true, true,
true, true, true,
false, true, false,
false, true, false,
false, true, false,
false, true, false,
true, true, true,
false, true, false,
false, true, false
);
quarter:array[0..11] of integer=(
A_U1, A_U3, A_D1, A_D3, A_F1, A_F3, A_B1, A_B3, A_L1, A_L3, A_R1, A_R3
);
face:array[0..5] of integer=(
A_U2, A_D2, A_F2, A_B2, A_L2, A_R2
);
slice:array[0..5] of integer=(
A_E1, A_E3, A_S1, A_S3, A_M1, A_M3
);
slice2:array[0..2] of integer=(
A_E2, A_S2, A_M2
);

metricString:array[0..2] of string = (
'Quarter', 'Face', 'Slice'
);

type
TTurn=class //我把类名改了
private
turnQuarter:TTurn;//这个不知该怎么定义,原JAVA代码是private static final Turn turnQuarter = new Turn(QUARTER_METRIC);
turnFace:TTurn;//这个不知该怎么定义,原JAVA代码是private static final Turn turnFace = new Turn(FACE_METRIC);
turnSlice:TTurn;//这个不知该怎么定义,原JAVA代码是private static final Turn turnSlice = new Turn(SLICE_METRIC);




procedure Turn(metric:integer);
public
lengthA:array[0..A_NUM-1] of integer;
lengthB:array[0..B_NUM-1] of integer;

function turn1(metric:integer):TTurn;//名称改了
end;


implementation

procedure TTurn.Turn(metric:integer);
var
i:integer;
begin
for i:=0 to A_NUM-1 do
begin
lengthA:=1;
end;
if (metric=QUARTER_METRIC) then
begin
for i:=0 to High(face) do
begin
lengthA[face]:=2;
end;
for i:=0 to High(slice) do
begin
lengthA[slice]:=2;
end;
for i:=0 to High(slice2) do
begin
lengthA[slice2]:=4;
end;
end
else if (metric=FACE_METRIC) then
begin
for i:=0 to High(slice) do
begin
lengthA[slice]:=2;
end;
for i:=0 to High(slice2) do
begin
lengthA[slice2]:=2;
end;
for i:=0 to High(b2a) do
begin
lengthB:=lengthA[b2a];
end;
end;
end;

function TTurn.turn1(metric:integer):TTurn;
begin
case metric of
QUARTER_METRIC:
Result:=turnQuarter;
FACE_METRIC:
Result:=turnFace;
SLICE_METRIC:
Result:=turnSlice;
else
{throw new IllegalArgumentException(&quot;Invalid metric&quot;)};//(SLICE_METRIC);这个不知该怎么转
end;
end;

end.
 
{throw new IllegalArgumentException(&quot;Invalid metric&quot;)};//(SLICE_METRIC);这个不知该怎么转
-----------
这里是抛出一个异常,IllegalArgumentException在java里已经定义好的。意思是参数非法。
在delphi你也直接抛出一个通用异常就可以了。
raise Exception.Create('非法参数');、
为了区分其他异常,你最好定义一个单独的异常。
TIllegalArgumentException = class(Exception)
end;
raise TIllegalArgumentException.Create('参数非法');

PS:
感觉在Delphi里大家都很少使用异常处理。
 
谢谢楼上。
还有其他几个问题有劳各位富翁继续解答。
 
但是这行我不知该如何处理,对我来说这里是关键之处:
private static final Turn turnQuarter = new Turn(QUARTER_METRIC);
 
private static final Turn turnQuarter = new Turn(QUARTER_METRIC);
private static final Turn turnFace = new Turn(FACE_METRIC);
private static final Turn turnSlice = new Turn(SLICE_METRIC);
-----------
这里用的是一个多例模式。
Delphi里没有static所以这个不是很好实现。
刘艺的那个设计模式里有说单例怎么实现,不过好像有些麻烦,我没太注意过。
JAVA的语言特性和Delphi差异还是满大的,完全改写确实有些问题。

那位兄弟改写的代码还有些错误。
private Turn(int metric) {这是一个构造函数,在使用多例的时候,构造函数不对外公布(在Delphi中无法实现)。
你得改成
public
constructor Create(int metric);
然后使用的时候直接用这个构造函数create就可以了。
多例部分主要是出于性能方面的考虑,你可以不进行处理。
 
Delphi 单例可以写成
private
class function turnQuarter: Turn;

implementation

class function Turn.turnQuarter: Turn;
{$J+} const Reference: Turn = nil
{$J-}
begin
if Reference = nil then Reference := Turn.Create(QUARTER_METRIC);
Result := Reference;
end;
 
type
TTurn=class
private
constructor Create2(metric:integer);
public
lengthA: array[0..A_NUM-1] of integer;
lengthB: array[0..B_NUM-1] of integer;
// constructor Create;
end;

function Turn(metric:integer): TTurn;

implementation

var
turnQuarter: TTurn = nil;
turnFace: TTurn = nil;
turnSlice: TTurn = nil;

constructor TTurn.Create2(metric: integer);
begin
// To-do
end;

function Turn(metric:integer): TTurn;
begin
Result := nil;
case metric of
QUARTER_METRIC:
begin
if turnQuarter = nil then turnQuarter := TTurn.Create2(metric);
Result := turnQuarter;
end;
FACE_METRIC: ;
// To do
SLICE_METRIC: ;
// To do
else
// To do
end;
end;

initialization

finalization
turnQuarter.Free;
turnFace.Free;
turnSlice.Free;

end.

使用全局函数 Turn 去访问TTurn 类,也可以使用 单例模式,用Create 去实现。
 
[:)]
比较统一楼上兄弟的改法。
在Delphi一般都用这样的方式实现类似单例的功能,Delphi毕竟有些Delphi的特点。
使用
class function Turn.turnQuarter: Turn;
{$J+} const Reference: Turn = nil
{$J-}
begin
if Reference = nil then Reference := Turn.Create(QUARTER_METRIC);
Result := Reference;
end;
的方式,我感觉有些过了。
 
......

虽然说 J+const 或 内部var 都能模拟 单例模式(并不是完全的,不能用于继承),如果不拘泥于 Java的类 (Java中必须是类),在Delphi用结构(Record)和数组 同样可以实现。
type
TTurnRecord = record
lengthA: array[0..A_NUM-1] of integer;
lengthB: array[0..B_NUM-1] of integer

end;
TTurnArray = array [0..2] of TTurnRecord
//QUARTER_METRIC..SLICE_METRIC;
var
Turn: TTurnArray;
end;
 
帮顶!

http://www.source520.com

站长开发推广同盟 站长朋友的终极驿站
同时拥有海量源码电子经典书籍下载

http://www.source520.com/search/search.asp

&quot;编程.站长&quot;论坛搜索引擎-----为中国站长注入动力!
 
后退
顶部