Java開發 使用反射判斷一個類的是否繼承指定接口類


前言

  使用反射判斷一個類的是否繼承指定接口類

 

代碼

接口類

public interface DemoService {

    void run();
}

實現類

public class DemoImport implements DemoService{
    @Override
    public void run() {
        Log.e("調試_臨時_log", "this_run");
    }
}

判斷代碼

    private void hasInterfaces() {
        Class<?> d = DemoImport.class;
        Class<?>[] interfacesArray = d.getInterfaces();//獲取這個類的所以接口類數組
        for (Class<?> item : interfacesArray) {
            if (item == DemoService.class) { //判斷是否有繼承的接口
                Log.e("調試_臨時_log", "this_true");
            }
        }
    }

 

 

 

 

End


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM