java反射学习笔记(常用的一些方法)
生活随笔
收集整理的這篇文章主要介紹了
java反射学习笔记(常用的一些方法)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
User類(用于測試)
package com.qingfeng.springbootstudy.entity;import java.io.Serializable;/*** (User)實體類** @author makejava* @since 2020-06-02 12:00:01*/ public class User implements Serializable {private static final long serialVersionUID = -11795320911240008L;/*** ID號*/private Integer id;/*** 用戶名*/private String name;/*** 用戶密碼*/private String password;public String date;public String phone;public User() {}public User(String date, String phone) {this.date = date;this.phone = phone;}public String getDate() {return date;}public void setDate(String date) {this.date = date;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}@Overridepublic String toString() {return "User{" +"id=" + id +", name='" + name + '\'' +", password='" + password + '\'' +", date='" + date + '\'' +", phone='" + phone + '\'' +'}';}public void login(){System.out.println("我是沒有參數的成員方法");}public void login(String name){System.out.println("我的名字是:"+name);} }學習代碼
package com.qingfeng.springbootstudy;import com.qingfeng.springbootstudy.entity.User; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method;@SpringBootTest @SuppressWarnings("all") class SpringBootStudyApplicationTests {@Testvoid contextLoads() {}@Testvoid test1() throws Exception {String path1 = "com.qingfeng.springbootstudy.entity.User";/*** 獲取Class類對象的三種方式:* 作用:* 1.獲取所有的成員變量* 2.獲取所有的成員方法* 3.獲取構造方法* 4.獲取類名* ....*//*User user=new User();Class<?> c1=Class.forName(path1);//加在配置文件Class<?> c2=User.class;//通過類名Class<?> c3=user.getClass();//通過對象System.out.println(c1);System.out.println(c2);System.out.println(c3);*//*** --------------------------------------* a.注意:帶Declared的表示private的* b.如果要獲取私有的,只需要設置:對象.setAccessible(true)即可(暴力反射)** 1.獲取所有的成員變量* Field[] getFields()* Field getField(String name)** Field[] getDeclaredFields()* Field getDeclaredField(String name)* --------------------------------------* 2.獲取構造方法* Constructor<?>[] getConstructors()* Constructor<?> getConstructor(參數類型.class ...)** Constructor<?>[] getDeclaredConstructors()* Constructor<?> getDeclaredConstructor(參數類型.class ...)* --------------------------------------* 3.獲取所有的成員方法* Method[] getMethods()* Method getMethod(String name)** Method[] getDeclaredMethods()* Method getDeclaredMethod(String name)* --------------------------------------* 4.獲取類名(全類名)* String getName()* --------------------------------------* 5.* 獲取方法名(名字)* String getName()* *//*-------------------------------------------------------------------*/Class<?> c = User.class;User user = new User();/*** 獲取成員變量(public)*/Field[] field = c.getFields();//獲取所有的public成員變量for (Field f : field) {System.out.println("成員變量對象:" + f);System.out.println("成員變量名:" + f.getName());}Field field1 = c.getField("date");//獲取單個public成員變量System.out.println(field1);/***操作成員變量*設置值:Object get(Object o)*獲取值:void set(Object o,value)* */field1.set(user, "1997-02-28");//設置public成員變量的值System.out.println(user);Object date = field1.get(user);//獲取public成員變量的值System.out.println(date);/*----------------------------------------------------------------------*//***1.獲取構造方法(public)**/Constructor<?> constructor = c.getConstructor(String.class, String.class);//獲取單個有參數public構造方法System.out.println(constructor);Constructor<?>[] constructors = c.getConstructors();//獲取所有的public構造方法for (Constructor<?> constructor1 : constructors) {System.out.println("構造方法對象:" + constructor1);System.out.println("構造方法名:" + constructor1.getName());//全類名}/*** 2.實例化對象*/Object o = constructor.newInstance("1997-02-28", "18388690352");//適用于有參數的構造System.out.println(o);Object o1 = c.newInstance();//只適用于無參數的構造System.out.println(o1);/*----------------------------------------------------------------------*//*** 獲取成員方法(public)*invoke()方法:用于執行某個方法* */Method login = c.getMethod("login");//獲取無參數的成員方法login.invoke(user);//執行無參數的數的成員方法Method login1 = c.getMethod("login", String.class);//獲取有參數的成員方法login1.invoke(user, "清風");//執行有參數的成員方法System.out.println(login1.getName());//獲取方法的名字Method[] methods = c.getMethods();//獲取所有的public方法for (Method method : methods) {System.out.println("成員方法對象:" + method);System.out.println("成員方法名:" + method.getName());}}}運行結果
成員變量對象:public java.lang.String com.qingfeng.springbootstudy.entity.User.date 成員變量名:date 成員變量對象:public java.lang.String com.qingfeng.springbootstudy.entity.User.phone 成員變量名:phone ---------------------------------------------------------------------------- public java.lang.String com.qingfeng.springbootstudy.entity.User.date User{id=null, name='null', password='null', date='1997-02-28', phone='null'} ---------------------------------------------------------------------------- 1997-02-28 public ----------------------------------------------------------------------------com.qingfeng.springbootstudy.entity.User(java.lang.String,java.lang.String) ---------------------------------------------------------------------------- 構造方法對象:public com.qingfeng.springbootstudy.entity.User() 構造方法名:com.qingfeng.springbootstudy.entity.User 構造方法對象:public com.qingfeng.springbootstudy.entity.User(java.lang.String,java.lang.String) 構造方法名:com.qingfeng.springbootstudy.entity.User ---------------------------------------------------------------------------- User{id=null, name='null', password='null', date='1997-02-28', phone='18388690352'} User{id=null, name='null', password='null', date='null', phone='null'} 我是沒有參數的成員方法 我的名字是:清風 login ---------------------------------------------------------------------------- 成員方法對象:public java.lang.String com.qingfeng.springbootstudy.entity.User.toString() 成員方法名:toString 成員方法對象:public java.lang.String com.qingfeng.springbootstudy.entity.User.getName() 成員方法名:getName 成員方法對象:public java.lang.Integer com.qingfeng.springbootstudy.entity.User.getId() 成員方法名:getId 成員方法對象:public void com.qingfeng.springbootstudy.entity.User.setName(java.lang.String) 成員方法名:setName 成員方法對象:public java.lang.String com.qingfeng.springbootstudy.entity.User.getDate() 成員方法名:getDate 成員方法對象:public java.lang.String com.qingfeng.springbootstudy.entity.User.getPassword() 成員方法名:getPassword 成員方法對象:public void com.qingfeng.springbootstudy.entity.User.login() 成員方法名:login 成員方法對象:public void com.qingfeng.springbootstudy.entity.User.login(java.lang.String) 成員方法名:login 成員方法對象:public void com.qingfeng.springbootstudy.entity.User.setDate(java.lang.String) 成員方法名:setDate 成員方法對象:public void com.qingfeng.springbootstudy.entity.User.setPassword(java.lang.String) 成員方法名:setPassword 成員方法對象:public void com.qingfeng.springbootstudy.entity.User.setId(java.lang.Integer) 成員方法名:setId 成員方法對象:public java.lang.String com.qingfeng.springbootstudy.entity.User.getPhone() 成員方法名:getPhone 成員方法對象:public void com.qingfeng.springbootstudy.entity.User.setPhone(java.lang.String) 成員方法名:setPhone 成員方法對象:public final void java.lang.Object.wait() throws java.lang.InterruptedException 成員方法名:wait 成員方法對象:public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException 成員方法名:wait 成員方法對象:public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException 成員方法名:wait 成員方法對象:public boolean java.lang.Object.equals(java.lang.Object) 成員方法名:equals 成員方法對象:public native int java.lang.Object.hashCode() 成員方法名:hashCode 成員方法對象:public final native java.lang.Class java.lang.Object.getClass() 成員方法名:getClass 成員方法對象:public final native void java.lang.Object.notify() 成員方法名:notify 成員方法對象:public final native void java.lang.Object.notifyAll() 成員方法名:notifyAll總結
以上是生活随笔為你收集整理的java反射学习笔记(常用的一些方法)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: adb-基础命令
- 下一篇: 漂亮的带分步说明的 VBA 自制进度条