• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

无信不立

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

【java反射】Class类型的相关操作演练

【一】获取范型接口的实现类的范型类型

(1)范型接口

package org.springframework.context;

import java.util.EventListener;


public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {

    /**
     * Handle an application event.
     * @param event the event to respond to
     */
    void onApplicationEvent(E event);

}
View Code

(2)范型接口实现类

package com.mobile.thinks.login.listen;

import org.springframework.context.ApplicationListener;

import com.mobile.thinks.login.event.BaseEvent;

public class LoginListen implements ApplicationListener<BaseEvent>{

    @Override
    public void onApplicationEvent(BaseEvent event) {
    
        
    }

    
}
View Code

(3)范型接口实现类的范型的填充类

package com.mobile.thinks.login.event;

import org.springframework.context.ApplicationEvent;

public abstract class BaseEvent extends ApplicationEvent {

    public BaseEvent(Object source) {
        super(source);
    }

    
}
View Code

(4)获取范型的填充类的类型

    public static void main(String[] args) {
        LoginListen listen=new LoginListen();
        Class<?> cls =listen.getClass();
        //cls==>class com.mobile.thinks.login.listen.LoginListen
        System.out.println("cls==>"+cls);
        Type[] type=cls.getGenericInterfaces();
        Type types=cls.getGenericSuperclass();
        for(int i=0;i<type.length;i++){
            Type ty=type[i];
            if(ty instanceof ParameterizedType){
                Type[] sTypes=((ParameterizedType)ty).getActualTypeArguments();
                for(int j=0;j<sTypes.length;j++){
                    Type clsa=sTypes[j];
                    //范型类型==>class com.mobile.thinks.login.event.BaseEvent
                    System.out.println("范型类型==>"+(Class)clsa);
                }
            }
        }
    }
View Code

 

posted on 2017-07-27 09:46  无信不立  阅读(233)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3