简体   繁体   English

检查对象是否在Java中有方法?

[英]Check if object has method in Java?

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, eg ImageView or a TextView for instance. 但是,为了更好地了解我正在做的事情,我正在尝试创建一个会影响对象alpha的类,例如ImageViewTextView 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() ? 如果我只需要确保他们拥有方法.setAlpha() ,我将如何为其创建一个接口,而不单独列出每个对象? Does this make sense? 这有意义吗? Cheers. 干杯。

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 require that method, say so. 如果您需要这种方法,请说明。

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...)并在类没有该方法时捕获NoSuchMethodException

On the other hand, this is not a very good practice in Java. 另一方面,这在Java中不是一个很好的实践。 Try using interfaces instead. 尝试使用接口。

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()来获取具有特定名称的对象(你必须定义参数)。

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
}

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{
...
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM