How to create primary constructor and multiple secondary constructors in Kotlin?
Follow the code:
class ModelClass( var title: String, var buttonPosTxt: String, var buttonNegTxt: String, var buttonNeuTxt: String, var subTitle: String ) { // PopUp with Title and 1 button constructor( title: String, buttonPosTxt: String ) : this(title, buttonPosTxt, "", "", "") // PopUp with Title and 2 buttons constructor( title: String, buttonPosTxt: String, buttonNegTxt: String ) : this(title, buttonPosTxt, buttonNegTxt, "", "") // PopUp with Title, SubTitle, 2 Buttons constructor( title: String, buttonPosTxt: String, buttonNegTxt: String, subTitle: String ) : this(title, buttonPosTxt, buttonNegTxt, "", subTitle) }
Alternativeways:
class ModelClass( var title: String, var buttonOneTxt: String ){ // Model with Title and 2 buttons constructor( title: String, buttonOneTxt: String, buttonTwoTxt : String ): this(title, buttonOneTxt) // Model with Title, SubTitle, 2 Buttons constructor( title: String, buttonOneTxt: String, buttonTwoTxt: String, subTitle: String ): this(title, buttonOneTxt) // Model with Title, SubTitle, 3 Buttons constructor( title: String, buttonOneTxt: String, buttonTwoTxt: String, buttonThreeTxt: String, subTitle: String ): this(title, buttonOneTxt)
// ..... you can add more constructors in this ways }
Comments
Post a Comment