检查Object是否实现了接口 [英] Check if Object implements interface
本文介绍了检查Object是否实现了接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
我有一个小问题。
我想知道如何检查对象是否符合界面。
我知道如果Class明确地实现了接口,就像这样:
展开 | 选择 | 换行 | 行号
解决方案
所以你想知道一个对象是否实现了一个方法,但是该对象并不一定要实现该接口?我不太明白。如果界面存在,你应该使用instanceof来检查兼容性。
你可以解释一下。您是否只想知道类是否有方法?
感谢您的回复。
如果我只是想检查一些方法,但是如果我有一个接口,你的解决方案效果很好非常大,效率不高。
我想要的是这样的:
展开 | 选择 | Wrap | 行号
我正在做一个可重用的类来过滤url输入。我们称之为 Url_Sanitize 。
Url_Sanitize 使用可以是任何类型的过滤器。该过滤器对象的唯一限制是实现一个接口,以便Url_Sanitize可以无所畏惧地(异常)调用接口中指定的过滤器方法。
好的我我认为有解决方案...
如果此人想要传递过滤器对象,则必须扩展过滤器类并实现我的界面。就是这样!
应用于我的例子它会是这样的:
展开 | 选择 | Wrap | 行号
Hello,
I have a little question.
I would like to know how to check if an object is compliant with an interface.
I know that if the Class implements explicitly the Interface, like this:
Expand|Select|Wrap|Line Numbers解决方案So you want to know if an object implements a method, but the object doesn''t necessarily implement the interface? I don''t quite understand. If the interface exists, you should just use to instanceof to check compatibility.
You might explain a little more. Do you just want to know if a class has a method?
Thanks for your reply.
Your solution works well if i just want to check a few methods, but if i have an interface that is quite big, it will not be very efficient.
What I would like is something like this:
Expand|Select|Wrap|Line Numbers
I am doing a reusable class that filters the url input. Let''s call it Url_Sanitize.
Url_Sanitize uses a filter which can be of any type. The only restriction for that filter object, is to implement one interface, so that the Url_Sanitize can fearlessly (of an exception) call the filter methods that are specified in the interface.
Ok i have the solution, i think...
If the person wants to pass a filter object, the filter class must be extended and implement my interface. That''s it!
Applied to my example it would be something like this:
Expand|Select|Wrap|Line Numbers
这篇关于检查Object是否实现了接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文登录 关闭
扫码关注1秒登录发送“验证码”获取 | 15天全站免登陆