【java反射】Class類型的相關操作演練

來源:互聯網
上載者:User

標籤:imp   spring   param   rgs   介面實現   ack   void   e30   操作   

【一】擷取範型介面的實作類別的範型類型

(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

 

【java反射】Class類型的相關操作演練

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.