Lalavava's recent timeline updates
Lalavava

Lalavava

V2EX member #468694, joined on 2020-02-10 23:34:37 +08:00
Per Lalavava's settings, the topics list is hidden
Deals info, including closed deals, is not hidden
Lalavava's recent replies
首先根据问题描述
这里问题应该不在取什么名字
而是你的这种设计本来就糟糕透顶
你在外部对一个对象实例内部字段进行初始化
试想一下如果别人看你的代码
他必须搜索所有代码 去找到你在什么地方进行了初始化
因为你可以在任何地方进行初始化
而且初始化这个字段的位置可以和 new A 的地方没任何关系

对于你描述的场景
如下代码应该是比较好的
class A<T> {
public A(Supplier<T> initializer) {
a = CompletableFuture.supplyAsync(initializer);
}

public T getA() {
try {
return a.get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e.getCause());
}
}

private final CompletableFuture<T> a;
}

class B {
public int foo() {
return 0;
}
}

public class Main {
public static void main(String[] args) {
var a = CompletableFuture.supplyAsync(B::new).thenAccept(b -> new A<Integer>(b::foo));
}
}
About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5590 Online   Highest 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 16ms · UTC 03:40 · PVG 11:40 · LAX 20:40 · JFK 23:40
♥ Do have faith in what you're doing.