简体   繁体   English

从接口参考中获取具体的类实现

[英]Get concrete Class realization from interface reference

Is it possible in java to obtain ConcreetClass.class from interface reference ISomeInterface . 在Java中是否可以从接口引用ISomeInterface获取ConcreetClass.class I would like to avoid 'instance of' keyword. 我想避免使用“ instance of”关键字。
On other words, is there: 换句话说,是否存在:

ISomeInterface intRef = new ConcreetClass();
Class realization = intRef.getRealizationClass();
realization == ConcreetClass.class; // true 

If java doesn't support this operation. 如果java不支持此操作。 Could you recommend me a way to deal with it? 您能推荐我一种解决方法吗?

您应该能够使用getClass()方法获取任何对象的特定类。

getClass returns the class of the instance . getClass返回instanceclass

Class<? extends ISomeInterface> realization = intRef.getClass();
System.out.println(ConcreetClass.class.equals(realization)); //true

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

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