zoukankan      html  css  js  c++  java
  • 【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
  • 相关阅读:
    mvc5+ef6+Bootstrap 项目心得--创立之初
    C# StopWatch的使用
    MVC Form异步请求
    bootstrap-datepicker带中文的js文件
    bootstrap
    mvc5+ef6+Bootstrap 项目心得--WebGrid
    mvc5+ef6+Bootstrap 项目心得--身份验证和权限管理
    关于mvc5+EF里面的db.Entry(model).State = EntityState.Modified报错问题
    将DBF文件导入Sqlserver数据库
    MySQL主从报错解决:Failed to initialize the master info structure
  • 原文地址:https://www.cnblogs.com/shangxiaofei/p/7243186.html
Copyright © 2011-2022 走看看