gpt4 book ai didi

java - 如何允许用户在 FilenameFilter 中编写自己的预定义方法(例如accept())的实现

转载 作者:行者123 更新时间:2023-11-30 04:57:50 25 4
gpt4 key购买 nike

FilenameFilter有一个 accept我可以实现告诉系统我想如何过滤文件的方法。我想实现类似的东西。这是我的场景:

我正在编写一个在 iText 之上运行的通用 API添加 Barcode到 pdf 文档。我有一个通用的自定义条形码。

public class MyCustomBarcode{

/**
* This variable holds the type of barcode
* com.itextpdf.text.pdf.Barcode
*/
private Barcode barcode;

/**
* The X position of the barcode. (0, 0) is at the bottom left
*/
private float x;

/**
* The Y position of the barcode. (0, 0) is at the bottom left
*/
private float y;

/**
*
*/
private int rotation;

...
}

所以当用户使用这个API时,他们只需要传入一个 List<MyCustomBarcode>到我的 API 中的方法,然后 API 会将条形码插入到每个 pdf 页面上。问题是每个条形码都有不同的 code格式。例如,BarcodeInter25 可能使用类似 000001, 000002 ... 的代码。 Barcode39 可能会使用其他东西。所以我想让用户自己编写如何生成条形码值的实现。像这样的事情

MyCustomBarcode barcode = new MyCustomBarcode(x, y, z){
public String getSeqNum(int i){
//The user own implementation of how they want integer i to look like.
//E.g if i==1, I might return 000001
}
);

由于我允许用户拥有多种类型的条形码,我希望允许他们为他们拥有的每个条形码编写自己的实现。

最佳答案

在父类中,创建方法 abstract ,然后让每个子类提供自己的具体实现。

<小时/>

示例

父类

public abstract class Barcode{
.
.
.
public abstract String getSeqNum(int i);
}

子类

public final class FooBarcode extends Barcode{
.
.
.
public final String getSeqNum(int i){
// provide own implementation
}
}

关于java - 如何允许用户在 FilenameFilter 中编写自己的预定义方法(例如accept())的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7968179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com