gpt4 book ai didi

java - 我可以在 Java 中重载接口(interface)方法吗?

转载 作者:行者123 更新时间:2023-12-03 16:08:22 25 4
gpt4 key购买 nike

我有一个数据描述接口(interface):

public interface DataDescription {
int getId();
}

和两个实现:
public class DataWithId1 implements DataDescription {
// ... simple getter impl.
}

public class OtherDataWithId implements DataDescription {
// ... simple getter impl.
}

现在我有了这个界面:
public interface DataProcessor {
void process(DataDescription data);
}

我想实现 DataProcessor有一个类,像这样:
public class DataProcessorImpl implements DataProcessor {
@Override
public void process(DataDescription data) {
// Do something...
}

public void process(DataWithId1 data) {
// Do something specific with this type (e.g. directly store in table of database)
}

public void process(OtherDataWithId data) {
// Do something specific with this type (convert to format DataWithId1 and then store in DB)
}
}

和一些代码来调用它:
public Main {
private DataProcessor dataProcessor = new DataProcessor();

public static void main(String[] args) {
DataDescription data = new DataWithId1(1);
dataProcessor.process(data);
}
}

我想要实现的是 DataProcessor调用者不必担心数据转换(他们有 DataDescription 并且不必知道这两种类型)。另外我想避免 instanceof代码。

我所做的假设是我可以重载这样的接口(interface)方法。在查看 section 15.12 of the java language specification 时,我找不到这方面的证据。 (这并不意味着它不存在......)。

我关于重载的假设是否正确?我可以避免 instanceof ?

最佳答案

也许您正在寻找 Visitor Pattern :

public interface DataDescription {
int getId();
void accept(DataProcessorImpl p);
}

public class DataWithId1 implements DataDescription {
private final int id;

public DataWithId1(int id) {
this.id=id;
}
@Override
public void accept(DataProcessorImpl p) {
p.process(this);
}
@Override
public int getId() {
return id;
}
// ... simple getter impl.
}

public class OtherDataWithId implements DataDescription {
private final int id;

public OtherDataWithId(int id) {
this.id=id;
}
@Override
public void accept(DataProcessorImpl p) {
p.process(this);
}
@Override
public int getId() {
return 42;
}
// ... simple getter impl.
}
public interface DataProcessor {
void process(DataDescription data);
}

public class DataProcessorImpl implements DataProcessor {
@Override
public void process(DataDescription data) {
data.accept(this);
}

public void process(DataWithId1 data) {
System.out.println("process(DataWithId1)");
// Do something specific with this type
// (e.g. directly store in table of database)
}

public void process(OtherDataWithId data) {
System.out.println("process(OtherDataWithId)");
// Do something specific with this type
// (convert to format DataWithId1 and then store in DB)
}
}

public class Main {
private static DataProcessor dataProcessor=new DataProcessorImpl();

public static void main(String[] args) {
DataDescription data = new DataWithId1(1);
dataProcessor.process(data);
data = new OtherDataWithId(100);
dataProcessor.process(data);
}
}

关于java - 我可以在 Java 中重载接口(interface)方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38436571/

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