How to implement concomitant objects in Kotlin
This article is about how Kotlin implements companion objects. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Companion Object (companion object)
Kotlin has no static variables and methods. Correspondingly, companion objects can be used. Concomitant objects allow defined constants and methods, similar to static in Java. With it, you can follow newInstance's fragment pattern.
Class ViewUserActivity: AppCompatActivity () {companion object {const val KEY_USER = "user" fun intent (context: Context, user: User): Intent {val intent = Intent (context, ViewUserActivity::class.java) intent.putExtra (KEY_USER, user) return intent}} override fun onCreate (savedInstanceState: Bundle?) {super.onCreate (savedInstanceState) setContentView (R.layout.activity_cooking) val user = intent.getParcelableExtra (KEY_USER) / /.}
We are familiar with using:
Val intent = ViewUserActivity.intent (context, user) startActivity (intent) Thank you for reading! This is the end of the article on "how to achieve companion objects in Kotlin". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!