【发布时间】:2016-09-30 00:42:42
【问题描述】:
我很清楚 oop 的多态性和继承概念,但是我处于需要知道实现类的情况。例如:
public CommonReadRepository<?> getReadRepository(String tableName) {
if (tableName == null)
return null;
switch (tableName) {
case "order":
return orderRepository;
...
}
return null;
}
接口orderRepository扩展了CommonReadRepository,由于我的需求,我需要访问orderRepository中定义的一个函数。
CommonReadRepository<?> repository=getReadRepository("order");
有什么方法可以检查CommonReadRepository的实现(子)类或接口吗?
当然,我总是可以这样做:
if(tableName=="order")
return (OrderRepository)CommonReadRepository<?>;
我尝试调试 getReadRepository("order"),但它给了我一个 JdkDynamicAopProxy 的实例,我不确定它是如何工作的。
if(interface is instanceof xyz class) 我不想使用它,因为我有 100 个课程,我想保留它作为最后的手段......或者换句话说 我不知道xyz课
谢谢
【问题讨论】:
-
我不太确定你在问什么...你不能使用
instanceof或getClass()吗? -
如果您需要访问该方法,您应该使用包含它的参数类型。这听起来像是一个设计问题。