這篇文章主要介紹了詳解java中this.getClass()和super.getClass()的實例的相關資料,需要的朋友可以參考下
##詳解java中this.getClass ()和super.getClass()的實例
前言:
遇到this.getClass()和super.getClass()的回傳值感到疑惑,經過探索豁然開朗。public final Class<?> getClass()傳回值為目前運行時類別的Class物件。
#
package com.cyou.lijiang_hw; import java.lang.reflect.ParameterizedType; public class BaseDAO { public BaseDAO() { System.out.println("==============BaseDAO initialization============="); System.out.println("this.getClass() ---->" + this.getClass()); System.out.println("super.getClass()---->" + super.getClass()); } }子類別:
package com.cyou.lijiang_hw; public class UserDAO extends BaseDAO<User> { public UserDAO() { System.out.println("\n==============UserDAO initialization============="); System.out.println("this.getClass() ---->" + this.getClass()); System.out.println("super.getClass()---->" + super.getClass()); } }測試類別:
#
package com.cyou.lijiang_hw; public class Test { public static void main(String[] args) { UserDAO userDAO = new UserDAO(); } }輸出結果為:
#
==============BaseDAO<T> initialization============= this.getClass() ---->class com.cyou.lijiang_hw.UserDAO super.getClass()---->class com.cyou.lijiang_hw.UserDAO ==============UserDAO initialization============= this.getClass() ---->class com.cyou.lijiang_hw.UserDAO super.getClass()---->class com.cyou.lijiang_hw.UserDAO
以上是Java中關於this.getClass()與super.getClass()的案例的詳細內容。更多資訊請關注PHP中文網其他相關文章!