Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example of simple use of reflection in java

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail the simple use of reflection in java. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Package reflect_test;public class Dog implements Animals {private int age = 100; private String xstr= "test..."; public int page = 1000000 public Dog Dog (String xstr) {this.xstr = xstr;} public Dog () {} public Dog (int age) {this.age = age;} @ Overridepublic String scrime (String str) {return str;} public int getAge () {return age;} public void setAge (int age) {this.age = age } private int getAddAge () {return age+=10;} public void getXstr () {System.out.println (xstr) }- -Test class: package reflect_test Import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import org.junit.Before;import org.junit.Test;public class TestDemo {Class clazz= null;Class interfa = null;Object obj = null;@Beforepublic void init () throws ClassNotFoundException, InstantiationException, IllegalAccessException {clazz= Class.forName ("reflect_test.Dog"); interfa = Class.forName ("reflect_test.Animals"); obj = clazz.newInstance () } / / call non-private member functions @ Testpublic void MethodTest () throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {Method m = clazz.getMethod ("scrime", String.class); Object object = m.invoke (obj, "say."); System.out.println (object);} / / call private member functions @ Testpublic void privateMethodTest () throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {Method m = clazz.getDeclaredMethod ("getAddAge"); m.setAccessible (true); Object object = m.invoke (obj) System.out.println (object);} / / get the non-private constructor @ Testpublic void getNotPrivateConstructor () throws Exception {Constructor construct = clazz.getConstructor (int.class); Dog dog = (Dog) construct.newInstance (10); System.out.println ("The age is:" + dog.getAge ());} / / get the private constructor @ Testpublic void getPrivateConstructor () throws Exception {/ / this method distinguishes it from the non-private constructor getDeclaredConstructorConstructor construct = clazz.getDeclaredConstructor (String.class) / / get permission construct.setAccessible (true); Dog dog = (Dog) construct.newInstance ("1000"); dog.getXstr ();} / / get the non-private variable @ Testpublic void getField () throws Exception {Field f = clazz.getDeclaredField ("age"); f.setAccessible (true); Object value = f.get (obj); System.out.println (value);} / / get the private variable @ Testpublic void getPirvateField () throws Exception {Field f = clazz.getField ("page"); Object value = f.get (obj) System.out.println (value);}} this is the end of the article on "simple use of reflection in java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report