2017-02-17 126 views
1

我有一個基礎抽象類:泛型和抽象類在科特林

abstract class BaseFragment<T : BasePresenter> : Fragment(){ 

    protected var presenter : T? = null 

    abstract fun providePresenter() : T 

    abstract fun getLayoutId() : Int 

    abstract fun onCreate() 

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { 
     val root = inflater?.inflate(getLayoutId(), container, false) 
     presenter = providePresenter() 
     return root 
    } 

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { 
     onCreate() 
    } 

} 

我在Java代碼:

//JAVA 

private ArrayList<BaseFragment> fragments; 

我如何使用它科特林?代碼波紋管工作不

//Kotlin 

val tabs = ArrayList<BaseFragment>() //error: One type argument expected for class BaseFragment<T : BasePresenter> : Fragment 

回答

2

你需要指定一個類型,請T

val tabs = ArrayList<BaseFragment<SomePresenterType>>() 
+0

THX。它應該工作,但不適合我的情況。我需要: 'val tabs = ArrayList >() tabs.add(instanceOf ())' 也許我在我的基礎架構中做錯了什麼? – Skullper

+0

在kotlin中你只需使用'()'來創建實例,所以'tabs.add(ProfileFragment())'應該可以工作 – lukle

+0

@PavliukR你應該在Java代碼中指定完整類型的'BaseFragment'。這是你給出的例子中的一種原始類型。它應該給你一個編譯器警告。 – marstran