黑马程序员-反射机制

来源:互联网 发布:绣花软件免费下载 编辑:程序博客网 时间:2024/06/05 21:02

反射故名思意,就是将一个对象映射出来。
类型能看清里面的架构。
就好比一个镜子里照映出自己的影子一样。

1、如何获取对象的类型名?

class Sample1{}public class ClassReflection {public static void main(String[] args) {//对象oObject o = new Sample1();//打印对象o的类型名System.out.println(o.getClass().getName());//可以直接以类型来获取类型名System.out.println(Sample1.class.getName());}}


2、如何获取对象的构造方法?
class Sample1{String str = "";public Sample1() {str="xxx";}public Sample1(String str) {this.str = str;}@Overridepublic String toString() {return "Sample1 [str=" + str + "]";}}public class ClassReflection {public static void main(String[] args) {try {//new 样品类构造方法Object o2 = Sample1.class.newInstance();System.out.println("o2 : " + o2);//获取全部构造方法Constructor[] con = o2.getClass().getConstructors();//实现带参数构造方法Object o3 = con[1].newInstance("dfajkdf");System.out.println("o3 : " + o3);//数组1的构造方法的方法名System.out.println(con[1].toGenericString());} catch (Exception e) {e.printStackTrace();}}}

2.1、用forName()获取类型
class Sample1{String str = "";public Sample1() {str="xxx";}public Sample1(String str) {this.str = str;}@Overridepublic String toString() {return "Sample1 [str=" + str + "]";}}public class ClassReflection {public static void main(String[] args) {try {//对象oObject o = Class.forName("Sample1").newInstance();System.out.println(o);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}


2.3、获取私有构造方法
class Sample1{String str = "";public Sample1() {str="xxx";}public Sample1(String str) {this.str = str;}@Overridepublic String toString() {return "Sample1 [str=" + str + "]";}}public class ClassReflection {public static void main(String[] args) {try {Class c = Class.forName("Sample1");Constructor cons = c.getDeclaredConstructor();cons.setAccessible(true);Object o = cons.newInstance(null);System.out.print(o);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();} }}



3、如何获取方法?
class Sample1{String str = "";public Sample1() {str="xxx";}public Sample1(String str) {this.str = str;}@Overridepublic String toString() {return "Sample1 [str=" + str + "]";}public void say() {System.out.println("xx");}}public class ClassReflection {public static void main(String[] args) {try {Object o4 = Sample1.class.newInstance();//获取对象的类型的所有公有方法,不包括private和protectedMethod[] ms = o4.getClass().getMethods();//包括父类的方法System.out.println(ms.length);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}


4、实现带参数方法
class Sample1{String str = "";public Sample1() {str="xxx";}public Sample1(String str) {this.str = str;}@Overridepublic String toString() {return "Sample1 [str=" + str + "]";}public String say(String str) {return str;}}public class ClassReflection {public static void main(String[] args) {try {Object o4 = Sample1.class.newInstance();//getMethod() 第一个参数是方法名 第二个参数方法返回值类型Method m = o4.getClass().getMethod("say", String.class);//invoke() 第一个参数指定实现的对象是哪个,第二个参数是以后的参数是实现方法里面的参数//方法里面参数若多个 m.invoke(o4, "hehe",(Object),.....) 就往后加即可System.out.println(m.invoke(o4, "hehe"));} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}


5、获取注解?
//WebService注解@WebService(name="sample1")class Sample1{}public class ClassReflection {public static void main(String[] args) {try {Object o5 = Sample1.class.newInstance();//获取在Sample1类型里面的@WebService注解WebService ws = o5.getClass().getAnnotation(WebService.class);//注解name参数System.out.println(ws.name());//注解portName参数System.out.println(ws.portName());//注解serviceName参数System.out.println(ws.serviceName());//注解targetNamespace参数System.out.println(ws.targetNamespace());//注解endpointInterface参数System.out.println(ws.endpointInterface());//注解wsdlLocation参数System.out.println(ws.wsdlLocation());//注解的类型System.out.println(ws.annotationType());} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}


原创粉丝点击