zoukankan      html  css  js  c++  java
  • Java反射

    Java反射在平常开发中虽然基本上用不着,但是在框架中,基本上都是通过反射来实现,比如说:Sping中来实现IOC。

    通过反射,我们可以在运行时获得一个类的所有属性和方法,通过任意一个对象,可以调用它的任意属性和方法。

    在Java反射中,一切都起源于Class类。此类的定义如下:

    public final class Class<T> extends Object implements Serializable,GenericDeclaration,Type,AnnotatedElement

    此类没有构造方法,要想实例化此类,可以通过

      ·类.class

      .类.getClass()

      ·静态方法forName(String className)

    实例

    package com.fuwh.reflect;
    
    public class ReflectTest01 {
        public static void main(String[] args) throws Exception{
            String s=new String("hello");
            Class<?> c1=s.getClass();
            System.out.println(c1.getName());
            
            Class<?> c2=Class.forName("java.lang.String");
            System.out.println(c2.getName());
            
            Class<?> c3=String.class;
            System.out.println(c3.getName());
        }
    }
    View Code

     通过反射,我们可以不用new一个对象,而取得该类的对象,使用该对象,实现操作。

    实例

    package com.fuwh.model;
    
    public class Info {
        private String name;
        private int age;
        private int score;
        private String address;
        public Info() {
            super();
        }
        
        public Info(String name,int age,int score,String address) {
            this.name=name;
            this.age=age;
            this.score=score;
            this.address=address;
        }
    
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public int getScore() {
            return score;
        }
        public void setScore(int score) {
            this.score = score;
        }
        public String getAddress() {
            return address;
        }
        public void setAddress(String address) {
            this.address = address;
        }
        
        public String showInfo(String title){
            return "操作人:"+title+"
    	name:"+name+",age:"+age+",score:"+score+",address:"+address;
        }
    }
    View Code
    package com.fuwh.reflect;
    
    import java.lang.reflect.Method;
    import java.lang.reflect.Modifier;
    
    public class ReflectTest02 {
        public static void main(String[] args) throws Exception{
            Class<?> c=Class.forName("com.fuwh.model.Info");
            Method[] methods=c.getMethods();
            for(Method m:methods){
                System.out.println(m.getName()+"参数个数:"+m.getParameterCount()
                                    +"修饰符:"+Modifier.toString(m.getModifiers()));
            }
            Method m=c.getMethod("showInfo", String.class);
            String s=(String)m.invoke(c.newInstance(), "老傅");
            System.out.println(s);
        }
    }
    View Code

  • 相关阅读:
    A1023 Have Fun with Numbers (20分)(大整数四则运算)
    A1096 Consecutive Factors (20分)(质数分解)
    A1078 Hashing (25分)(哈希表、平方探测法)
    A1015 Reversible Primes (20分)(素数判断,进制转换)
    A1081 Rational Sum (20分)
    A1088 Rational Arithmetic (20分)
    A1049 Counting Ones (30分)
    A1008 Elevator (20分)
    A1059 Prime Factors (25分)
    A1155 Heap Paths (30分)
  • 原文地址:https://www.cnblogs.com/zerotomax/p/6501289.html
Copyright © 2011-2022 走看看