简体   繁体   中英

companion object and function with default value

I have a class with default values isEnabled: Boolean = true in function getCustomClass

class CustomClass(_className: String, _isEnabled: Boolean) {

    private val className: String = _className
    private val isEnabled: Boolean = _isEnabled

    companion object {
        fun getCustomClass(className: String, isEnabled: Boolean = true): CustomClass {
            return CustomClass(className, isEnabled)
        }
    }
}

Why can't I use this constructor:

CustomClass.Companion.getCustomClass(MyClass.class.getSimpleName());

It looks like you are calling this from Java. Java does not support default arguments.

You could add @JvmOverloads to the functions so it generates all the additional overload methods that can be called from Java.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM