在 Java 中检查对象是否有方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10770539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if object has method in Java?
提问by Connor Deckers
I'm not all too greatly experienced with Java, I know enough to get me through at the moment, but I'm not perfect yet, so I apologize if this is a dumb question.
我对 Java 的经验不是很丰富,我现在知道的足以让我通过,但我还不完美,所以如果这是一个愚蠢的问题,我深表歉意。
I'm trying to write a class that can have any sort of object passed to it, but I need to check if the object passed has a particular method with it. How would I go about testing this?
我正在尝试编写一个可以将任何类型的对象传递给它的类,但我需要检查传递的对象是否具有特定的方法。我将如何进行测试?
I hope this makes sense. Cheers.
我希望这是有道理的。干杯。
EDIT
编辑
Thanks for all the fast replies! I'm not too familiar with interfaces etc, so I'm not entirely sure how to use them. But, to give a better insight into what I'm doing, I'm trying to create a class that will affect the alpha of an object, e.g. ImageView
or a TextView
for instance. How would I create an interface for that, without listing each object individually, when all I need is to make sure that they have the method .setAlpha()
? Does this make sense? Cheers.
感谢所有快速回复!我对接口等不太熟悉,所以我不完全确定如何使用它们。但是,为了更好地了解我在做什么,我正在尝试创建一个会影响对象 alpha 的类,例如ImageView
或 a TextView
。我将如何为此创建一个接口,而不单独列出每个对象,而我只需要确保它们具有该方法.setAlpha()
?这有意义吗?干杯。
回答by duffymo
A better idea would be to create an interface with that method and make that the parameter type. Every object passed to your method will have to implement that interface.
一个更好的主意是使用该方法创建一个接口并将其设为参数类型。传递给您的方法的每个对象都必须实现该接口。
It's called expressing your contract with clients. If you requirethat method, say so.
这叫做表达你与客户的合同。如果您需要该方法,请说出来。
回答by Alexis King
Get the instance's class with Object.getClass()
, then use Class.getMethod(String, Class...)
and catch a NoSuchMethodException
if the class doesn't have that method.
使用 获取实例的类Object.getClass()
,如果该类没有该方法,则使用Class.getMethod(String, Class...)
并捕获 a NoSuchMethodException
。
On the other hand, this is not a very good practice in Java. Try using interfaces instead.
另一方面,这在 Java 中并不是一个很好的做法。尝试改用接口。
回答by Francis Upton IV
You can do this using Java reflection.
您可以使用 Java 反射来做到这一点。
Something like object.getClass().getMethods()
to get an array of the Method
objects, or you can use the getMethod()
to get an object with a particular name (you have to define the parameters).
类似于object.getClass().getMethods()
获取Method
对象数组,或者您可以使用getMethod()
来获取具有特定名称的对象(您必须定义参数)。
回答by rid
First, define an interface that will require all objects implementing it to implement a setAlpha()
method:
首先,定义一个接口,它需要所有实现它的对象来实现一个setAlpha()
方法:
public interface AlphaChanger {
public void setAlpha(int alpha); // concrete classes must implement this
}
Then, define classes that implement this interface:
然后,定义实现此接口的类:
public class Thing implements AlphaChanger {
public void setAlpha(int alpha) { // must be present
// implementation
}
}
Now you can require that all objects passed to your method must implement the AlphaChanger
interface:
现在您可以要求传递给您的方法的所有对象都必须实现该AlphaChanger
接口:
public void yourMethod(AlphaChanger changer) {
changer.setAlpha(10); // you know for sure the method exists
}
回答by Zakaria
You can try and list the classes' methods like this (example : FooBar class):
您可以尝试像这样列出类的方法(例如:FooBar 类):
Class fooBar= FooBar.class;
Method[] methods = fooBar.getDeclaredMethods();
if (methods.length == 0){
...
} else{
...
}