简体   繁体   English

Java:如何在不指定完整类型的情况下指定函数参数的类型

[英]Java: how to specity a type for a function parameter without specifying a full type

Suppose i have a interface like this, 假设我有一个这样的界面,

interface MyIntf{
  void generate();
}

and a method like below 和下面的方法

void run(Myintf x) {
  x.generate();
}

I could call run() with an object of a class which implements MyIntf. 我可以使用实现MyIntf的类的对象调用run()。

but is it possible in Java to declare run without an explicit name for the interface. 但是在Java中是否可以声明运行而没有接口的显式名称。

ie can i specify run() like this? 即我可以这样指定run()吗?

void run("Some object which has a method called 'void generate()'" x){
  x.generate();
}

and run() can be called with an object of any class which has a method called 和run()可以用任何具有以下方法的类的对象来调用

void generate();

You must then use reflection to do what you want. 然后,您必须使用反射来执行所需的操作。 Something like: 就像是:

void run(Object o) {
    Method m = o.getClass().getMethod("generate", new Class[0]);
    if (m!=null)
        m.invoke(o, new Object[0]);
}

You must also add the necessary try/catch (which I don't know by heart), and I think you can pass null instead of the empty arrays. 您还必须添加必要的try / catch(我不太清楚),我认为您可以传递null而不是空数组。

No, you cannot do that. 不,你不能那样做。 This is exactly what interfaces are for :) Unless you don't use reflection.. 这正是接口的目的:)除非您不使用反射。

Java uses "nominative" rather than "structural" typing. Java使用“主语”类型而不是“结构”类型。

Just because a method has the same name and parameters, doesn't mean it does the same thing (put the camera/gun to you head and shoot). 仅仅因为一个方法具有相同的名称和参数,并不意味着它会做同样的事情(将相机/枪交给您头部射击)。 If you need to make a legacy type conform to a particular interface, use an adapter. 如果需要使旧类型符合特定接口,请使用适配器。 Avoid reflection. 避免反射。

You mean as if you would do something like this?: 您的意思是好像您会做这样的事情?:

void run(Object x) {
  ((Myintf)x).generate();
}

You could do: 您可以这样做:

void run(Object x){
    try{
        //use reflection to try and run a generate() method on x
    } catch (Exception e){}
}

but there's no way to enforce that at compile time. 但是没有办法在编译时强制执行。

It is not possible to specify "some object which has a method called void generate() " in Java in the way that you have in mind. 按照您的想法,无法在Java中指定“某些对象的方法称为void generate()的方法”。

In principle you could do this with reflection: you just pass in an Object and at runtime you check if there is a void generate() method that you can call. 原则上,您可以通过反射来做到这一点:您只需传入一个Object然后在运行时检查是否存在可以调用的void generate()方法。 This means however that you are throwing away the type safety that the compiler provides; 但是,这意味着您将放弃编译器提供的类型安全性。 I don't recommend using this solution. 我不建议使用此解决方案。

public void run(Object obj) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    Method m = obj.getClass().getMethod("generate");
    m.invoke(obj);
}

在Java中执行此操作的唯一方法是使用反射或使用instanceof运算符(不建议这样做),然后将要传递的对象强制转换为正确的类/接口。

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

相关问题 Java HashMap作为方法参数-未指定“值”类型 - Java HashMap as method parameter - not specifying the “value” type 有没有一种方法可以指定嵌套的泛型而不在Java 8+中两次指定类型 - Is there a way of specifying a nested generic without specifying a type twice in Java 8+ Java将Type作为函数参数传递 - Java Passing in Type as Function Parameter 非泛型类型扩展泛型类型而不指定类型参数 - non-generic type extending a generic type without specifying type parameter(s) 不指定类型参数,不能将Java 8方法与lambda参数一起使用 - Cannot use Java 8 method with lambda arguments without specifying type arguments 列出用法而不指定类型 - List usage without specifying the Type 强制类字段为相同的泛型类型而不指定类类型参数 - Enforce class fields to be same generic type without specifying a class type parameter 如果使用泛型类型参数声明一个类,并且在未指定类型的情况下实例化它,它是否默认为Object? - If a class is declared with a generic type parameter, and it's instantiated without specifying the type, does it default to Object? 如何在不指定类型的情况下引用我的Java Enum - How can I reference my Java Enum without specifying its type Java泛型-指定泛型类型 - java generics - specifying the generic type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM