草庐IT

java - 我如何告诉 Dagger 2 要基于 X 实例化哪个实现?

在模块内部,如果我需要根据模块构造时已知的变量提供接口(interface)的不同实现,我可以将逻辑放在该接口(interface)类型的@Provides方法中。像这样:@ModulepublicclassFooModule{privatefinalStatestate;publicFooModule(Statestate){this.state=state;}@ProvidesFooInterfaceprovideFooImplementation(){switch(state){caseSTATE_1:returnnewFooImpl1();caseSTATE_2:returnn

C++ 模板化虚函数

C++不支持模板化虚拟成员函数,但我有一个理想的场景。我想知道是否有人对实现此目标的方法有想法。#includeclassFoo{public:virtualvoidbar(int){}//makeacloneofmyexistingdata,butwithadifferentpolicyvirtualFoo*cloneforDB()=0;};structDiskStorage{staticvoidstore(intx){std::coutclassFooImpl:publicFoo{public:FooImpl():m_value(0){}templateFooImpl(constF

go - 类型断言是处理接口(interface)时返回结构指针的唯一方法吗?

考虑以下代码:packagemainimport"fmt"//5typeIinterface{Foo()string}typeastruct{iint}func(sa)Foo()string{return"Wearehere!"}func(sa)Bar()string{return"MajorTom!"}//20funcmain(){varxI=&a{i:42}fmt.Println(x.Foo())fmt.Println(x.(*a).Bar())}main的最后一条语句返回了底层结构,但我需要导出该结构才能返回。如果我在一个库中使用一个包,其中唯一导出的符号是接口(interfac

go - 类型断言是处理接口(interface)时返回结构指针的唯一方法吗?

考虑以下代码:packagemainimport"fmt"//5typeIinterface{Foo()string}typeastruct{iint}func(sa)Foo()string{return"Wearehere!"}func(sa)Bar()string{return"MajorTom!"}//20funcmain(){varxI=&a{i:42}fmt.Println(x.Foo())fmt.Println(x.(*a).Bar())}main的最后一条语句返回了底层结构,但我需要导出该结构才能返回。如果我在一个库中使用一个包,其中唯一导出的符号是接口(interfac