是否有替代方法可以覆盖所引用示例中的抽象方法?

Is there an alternative for overwriting the abstract method in the example cited?

我对这里示例的实现有疑问:https://dev.grakn.ai/docs/examples/phone-calls-migration-java。我们在嵌套的抽象静态 class:

中有一个抽象方法
public class PhoneCallsCSVMigration {
    /**
     * representation of Input object that links an input file to its own templating function,
     * which is used to map a Json object to a Graql query string
     */
    abstract static class Input {
        String path;

        public Input(String path) {
            this.path = path;
        }

        String getDataPath() {
            return path;
        }

        abstract String template(Json data);
    }
...

稍后,抽象方法template(Json data)被覆盖,目的是获得graqlInsertQuery:

inputs.add(new Input("files/phone-calls/data/companies") {
            @Override
            public String template(Json company) {
                return "insert $company isa company, has name " + company.at("name") + ";";
            }
        });

首先,如何实例化 Input 类型的东西?其次,Json company 从何而来?我想将 PhoneCallsCSVMigration class 拆分成几个 class,例如 InputGraknHandlerQueryHandler 等,我想知道除了覆盖抽象 class 之外,我如何定义用于构建 Graql 插入查询的模板。非常感谢任何帮助。

First of all, how is it even possible to instantiate something of the type Input?

您没有实例化 class Input。您正在创建一个匿名 class 的实例,它派生自 Input 并实现其抽象方法 template.

where does the Json company come from?

它来自将调用 template 方法并将其作为参数传递的方法。

我把剩下的问题留给理解它的人...

从外部可以看出,输入子项是使用 path 和实施模板创建的。 可能这个对象被传递给周围的class并且它调用template传递Json数据。

抽象方法可以很容易地用 Function<Json, String> 代替,但是那个函数会错过 path,所以可以使用 BiFunction<String, Json, String>.

您必须查看路径和 Json 的来源和时间。这里似乎有点做作。传统的(非常相似的)模式将是:

,而不是上面的静态内部 class
abstract class A {
    public final void func() { // Some service offered by this class.
        B b = ...;
        C c = onFunc(b);
    }

    abstract protected C onFunc(B b); // Some requirement to implement.
}
  • 这里func是针对A用户的一项服务
  • onFunc是为了A的实现者满足特定的要求。

所以断章取义就像你的情况一样有点奇怪或过度设计。只传递一个回调函数似乎就可以了。