以 class 作为参数,其中有一个特定的方法

Take a class as a parameter which has a particular method in it

我知道我们可以在一个参数中传递一个 class,它有一个特定的 class 作为它的超级。例如,看这个:

public void sampleMethod(Class<? super MyParentClass> class){
   // my random code 
}

在科特林中:

fun sampleMethod(class:Class<? super MyParentClass>){
   // my random code 
}

但是,我可以要这样的东西吗?:

public void sampleMethod(Class <? hasMethod myMethod> class){
   // my random code 
}

或者在 Kotlin 中:

fun sampleMethod(class:Class<? hasMethod MyParentClass>){
   // my random code 
}

基本上我只想在那个参数中接受一个class,其中有一个方法在里面提到。可能吗?

如果是,我想要 java 和 Kotlin

中的代码

如果没有,那么任何替代方案都可以

编辑


这里的大部分答案都不是我想要的。所以,我在这里多解释一下:

public class MyClass1{
   public static void main(String[] args){
      doRandomTask(new ClassWhichHasMethod()); // works fine
      doRandomTask(new ClassWhichDoesNotHaveMethod()); // gives error as it does not have that method
   }

   static class ClassWhichHasMethod{
      public void theMethodIWant(){} // the method I want to check
   }

   static class ClassWhichDoesNotHaveMethod{ // no method here}

   public static void doRandomTask(Class<? hasMethod theMethodIWant> class){
      // my random code 
   }

}

您需要使用 界面 来做到这一点。当一个对象实现一个接口时,这保证它具有接口定义的方法、属性等。你不需要知道对象是什么class,只要它可以被视为接口的类型即可:

interface MethodHaver {
    fun myCoolMethod(something: String)
}

class Whatever : MethodHaver {
    // need to implement/override the method in the interface to satisfy the contract
    override fun myCoolMethod(something: String) {
        // do a thing
    }
}

class SomethingElse {
    // uses anything that implements MethodHaver
    fun doTheThing(myObject: MethodHaver) {
        // the object definitely has this method, because the MethodHaver type requires it
        myObject.myCoolMethod("wow")
    }
}

你也可以用匿名对象来做,不一定要 classes

SomethingElse().doTheThing(object : MethodHaver {
    override fun myCoolMethod(something: String) {
        // do something
    }
})

或者,在 Kotlin 中,由于接口只有一个方法,您可以将其设为 functional interface,然后您可以传递一个与单个方法的签名相匹配的 lambda:

// note the 'fun' keyword before 'interface'
fun interface MethodHaver {
    fun myCoolMethod(something: String)
}

// pass a lambda that matches the method signature, i.e. takes a String and returns Unit (doesn't return anything)
SomethingElse().doTheThing { something -> // do something }

最后,在 Kotlin 中,您可以跳过接口并改用函数类型

class SomethingElse {
    // accepts any function that takes a String and returns Unit
    fun doTheThing(f: (String) -> Unit) {
        // call the passed function like any other
        f("wow")
    }
}

// has the method you're interested in, but it isn't implementing an interface
class ClassWithMethod {
    fun someMethod(string: String) {
        println(string)
    }
}

fun main() {
    // call it passing a lambda
    SomethingElse().doTheThing { println(it) }

    // or pass a function reference, like a method on a specific object
    val myClass = ClassWithMethod()
    SomethingElse().doTheThing(myClass::someMethod)
}

有了函数引用示例,您不必传递 具有特定方法的对象 ,您只需传递 对方法本身的引用。因此,不必涉及接口(这样类型系统可以确认您正在使用适当的方法传递有效对象),您可以去掉中间人并直接传递方法本身。只要它的签名匹配(参数和 return 类型),它就是有效的

是的,有很多选项取决于您需要做什么!