简体   繁体   English

kotlin抽象静态乐趣在伴侣对象

[英]kotlin abstract static fun in companion objects

I learn the use of ViewHolder from an offical sample named UserViewHolder. 我从一个名为UserViewHolder的官方样本中学习了ViewHolder的使用。

public class UserViewHolder extends RecyclerView.ViewHolder {

static UserViewHolder create(LayoutInflater inflater, ViewGroup parent) {
    UserItemBinding binding = UserItemBinding
        .inflate(inflater, parent, false);
    return new UserViewHolder(binding);
  } 

  private UserItemBinding mBinding;

  private UserViewHolder(UserItemBinding binding) {
    super(binding.getRoot());
    mBinding = binding;
  }

  public void bindTo(User user) {
    mBinding.setUser(user);
    mBinding.executePendingBindings();
  }

}

I'm going to write many ViewHolder classes, so I hope I can write an abstract class. 我打算写很多ViewHolder类,所以我希望我能写一个抽象类。 In Java, it looks like: 在Java中,它看起来像:

public abstract  static class BaseViewHolder {

abstract static BaseViewHolder create()

abstract void bindTo()

}

I try to write it using Kotlin , but finally I find that it's not as simple as it in Java. 我尝试使用Kotlin编写它,但最后我发现它并不像Java中那么简单。

abstract class BaseViewHolder(itemView: View):RecyclerView.ViewHolder(itemView) {

abstract fun bindTo(viewModel: BaseViewModel)

}

In Kotlin, if I want a static function, I need to write the function in "companion objects". 在Kotlin中,如果我想要一个静态函数,我需要在“伴随对象”中编写该函数。 But it can't be a "abstract". 但它不可能是“抽象的”。

In Java, a abstract class with abstract classes is common. 在Java中,具有抽象类的抽象类很常见。

But how can I write it in Kotlin? 但我怎么能在Kotlin写呢?

update: 更新:

I have wrote my own SleepViewHolder . 我写了自己的SleepViewHolder I'm going to write lots of ViewHolder, such as AppleViewHolder, BananaViewHolder and so on. 我要写很多ViewHolder,比如AppleViewHolder,BananaViewHolder等等。 So I want to build a BaseViewHolder as a pattern. 所以我想构建一个BaseViewHolder作为模式。 My question is that, in that case, what's the best way to write the pattern BaseViewHolder ? 我的问题是,在这种情况下,编写BaseViewHolder模式的最佳方法是什么? Should I change the constrcuter of it, or make the create function public? 我应该更改它的构造,还是将create函数公开?

open class SleepViewHolder private constructor(private val binding: ItemSleepBinding)
: RecyclerView.ViewHolder(binding.root) {

companion object {
    @JvmStatic
    fun create(inflater: LayoutInflater, parent: ViewGroup): SleepViewHolder {

        val binding: ItemSleepBinding
                = DataBindingUtil.inflate(inflater, R.layout.fragment_base, parent, false)

        return SleepViewHolder(binding)
    }
}

open fun bindTo(viewmodel: SleepViewModel) {
    binding.vm = viewmodel
    binding.executePendingBindings()
}

} }

In Kotlin , unlike Java or C#, classes do not have static methods. Kotlin ,与Java or C#,类没有静态方法。 In most cases, it's recommended to simply use package-level functions instead. 在大多数情况下,建议只使用包级功能。

If you need to write a function that can be called without having a class instance but needs access to the internals of a class (for example, a factory method), you can write it as a member of an object declaration inside that class. 如果您需要编写一个可以在没有类实例但需要访问类的内部(例如,工厂方法)的情况下调用的函数,则可以将其写为该类中对象声明的成员。

Even more specifically, if you declare a companion object inside your class, you'll be able to call its members with the same syntax as calling static methods in Java/C# , using only the class name as a qualifier. 更具体地说,如果在类中声明一个伴随对象,您将能够使用与在Java/C#调用静态方法相同的语法来调用其成员,只使用类名作为限定符。

This is how you can write a companion class 这是你编写伴侣类的方法

class MyClass {
   companion object { } // will be called "Companion"
}
fun MyClass.Companion.foo() { // ...
}

this is how you call foo() function... 这就是你如何调用foo()函数...

MyClass.foo()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Kotlin伴侣对象和反射 - Kotlin companion objects and reflection Kotlin 在抽象类中调用伴随对象 - Kotlin Calling Companion Object in Abstract Class 如何从Java访问Kotlin伴随对象 - How to access Kotlin companion objects from Java Kotlin对象和伴侣对象以及懒惰如何与内存一起工作 - How Kotlin objects & companion objects & lazy works with memory 我可以使用Kotlin随播对象方法隐藏Java静态方法吗? - Can I hide a Java static method with a Kotlin companion object method? 是否可以为 kotlin 中的多个伴随对象创建一个全局通用 function - Is it possible to make a global, generic function for multiple companion objects in kotlin 有没有办法在抽象类和它的实现类之间共享数据而不使用 Kotlin 中的伴随对象 - Is there a way to share data between an abstract class and it's implementation classes without using companion object in Kotlin Java静态方法和Kotlin随播对象有什么区别? 为什么在Andorid数据绑定中出现错误? - What is the difference between Java static method and Kotlin companion object ? Why it is giving error in Andorid Data Binding? 伴随对象Kotlin JNI中的UnsatisfiedLinkError - UnsatisfiedLinkError in companion object Kotlin JNI Kotlin-伴随对象的属性可见性 - Kotlin - attribute visibility to companion object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM