【问题标题】:Is it possible to write the following Java program in Delphi?是否可以在 Delphi 中编写以下 Java 程序?
【发布时间】:2021-03-28 10:44:18
【问题描述】:

这是Java程序:

cardReader.searchCard(slotTypes, 60, new OnCardInfoListener() {
    @Override
    public void onCardInfo(int retCode, CardInfoEntity cardInfo) {
        //Instruction
    }
    @Override
    public void onSwipeIncorrect() {
        //Instruction
    }

    @Override
    public void onMultipleCards() {
        //Instruction
    }
    });

我尝试了以下说明,但不知道将其余部分放在哪里:

var
cardInfo : JOnCardInfoListener;
begin
cardReader.searchCard(slotTypes,60,TJonCardInfoListener.Create???);

end;

这是 JAVA 中的类:它是我在 delphi 上导入的第三方库。

package com.nexgo.oaf.apiv3.device.reader;

public interface OnCardInfoListener {
  void onCardInfo(int paramInt, CardInfoEntity paramCardInfoEntity);
  
  void onSwipeIncorrect();
  
  void onMultipleCards();
}

在使用 java2op 从 .jar 文件生成的桥文件中,我有以下语句:

JOnCardInfoListener = interface;//com.nexgo.oaf.apiv3.device.reader.OnCardInfoListener

JOnCardInfoListenerClass = interface(IJavaClass)
['{283DE9B4-B2F7-4BED-B90E-A2C39DAB2687}']
end;

[JavaSignature('com/nexgo/oaf/apiv3/device/reader/OnCardInfoListener')]
JOnCardInfoListener = interface(IJavaInstance)
['{E65167F4-7C28-46EA-A29F-2993A714CC93}']
procedure onCardInfo(i: Integer; cardInfoEntity: JCardInfoEntity); cdecl;
procedure onMultipleCards; cdecl;
procedure onSwipeIncorrect; cdecl;
end;
TJOnCardInfoListener = class(TJavaGenericImport<JOnCardInfoListenerClass, JOnCardInfoListener>) end;

我可以从 TJonCrardInfoListener 声明另一个类以覆盖 onCardInfo 方法吗?

【问题讨论】:

  • 是的,这当然是可能的。你试过什么?
  • Andreas Rejbrand:你能给我举个例子吗?
  • 这取决于OnCardInfoListener 是一个类还是一个接口。如果它是一个类,那么不,它不能用Delphi编写
  • 是的,它是类:所以我必须找到另一种方式。谢谢大家!
  • 按照您刚刚提供的Java 代码,它不是一个类,而是一个接口。有人需要“解开”问题,以便回答

标签: java android oop delphi


【解决方案1】:

非常基于您迄今为止提供的内容的人为示例,但这应该会给您一个想法:

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  // You will need to add your import unit to the uses clause here
  Androidapi.JNI.JNIBridge;

type
  TCardInfoListener = class(TJavaLocal, JOnCardInfoListener)
  public
    { JOnCardInfoListener }
    procedure onCardInfo(i: Integer; cardInfoEntity: JCardInfoEntity); cdecl;
    procedure onMultipleCards; cdecl;
    procedure onSwipeIncorrect; cdecl;
  end;

  TForm1 = class(TForm)
  private
    FListener: JOnCardInfoListener;
  public
    procedure SearchCard;
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

{ TCardInfoListener }

procedure TCardInfoListener.onCardInfo(i: Integer; cardInfoEntity: JCardInfoEntity);
begin
  // Implementation of onCardInfo goes here
end;

procedure TCardInfoListener.onMultipleCards;
begin
  // Implementation of onMultipleCards goes here
end;

procedure TCardInfoListener.onSwipeIncorrect;
begin
  // Implementation of onSwipeIncorrect goes here
end;

{ TForm1 }

procedure TForm1.SearchCard;
begin
  if FListener = nil then
    FListener := TCardInfoListener.Create;
  cardReader.searchCard(slotTypes, 60, FListener);
end;

end.

【讨论】:

  • 感谢这个例子。我试试看!
猜你喜欢
  • 1970-01-01
  • 2010-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-26
相关资源
最近更新 更多