Java简单的反射

Java简单的反射

这里只是介绍下反射的简单实现方法。

反射的使用方法

  • 基本类:
/*算术运算*/
public class Arithmetic {
    public int add(int a,int b) {
        return a+b;
    }
    public int mult(int a,int b) {
        return Math.multiplyExact(a, b);
    }
    public int sub(int x,int y) {
        return Math.subtractExact(x, y);
    }
    public int divide(int a,int b) {
        return Math.floorDiv(a, b);
    }
}
  • 常用的实现方法:

public class ReflactArith {
    public static void main(String[] args) {
        int a=6,b=3;
        Arithmetic aObject = new Arithmetic();
    }
}
  • 反射代码实现方法:
public class ReflactArith {
    public static void main(String[] args) {
        int a=6,b=3;
        try {
            Class mClass = Class.forName("Arithmetic");
            Arithmetic aClass = (Arithmetic) mClass.newInstance();
            //mClass.getDeclaredMethods()
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

反射的使用场景:

  • 通过配置文件来配置所需要实例化的子类

public class Test {
    public static void main(String[] args) {
        Productor pa= new ProdcutorA();
        pa.show();

        Productor pb = new ProductorB();
        pb.show();
    }
}
abstract class Productor{
    public abstract void show();
}
class ProdcutorA extends Productor{

    @Override
    public void show() {
        System.out.println("this is productor A");
    }

}
class ProductorB extends Productor{

    @Override
    public void show() {
        System.out.println("this is productor B");
    }

}

如上代码所示,这是一种比较常用并且简单的实例化类的方法。但是有个问题—java代码是编译成class文件的,如果可以今天需要ProductorA,但是等你代码提交了以后,可以又需要ProductorB,这个时候我们就需要修改java代码,然后再编译,这样灵活性就降低了很多。
解决上述问题的方法就是反射:我们写一个简单的配置文件classname.properties,在里面写入如下代码(注:ProductorA这个类名应该是包含包名的,这里我没有包名,所以就这样简写

classname=ProdcutorA

然后Test的代码修改为:

public class Test {
    public static void main(String[] args) {
        Properties properties = new Properties();
        InputStream is = null;
        try {
            is = Test.class.getResourceAsStream("classname.properties");
            properties.load(is);
            is.close();
            String className = properties.getProperty("classname");
            Class mClass= Class.forName(className);
            Productor productor = (Productor) mClass.newInstance();
            Method show = mClass.getMethod("show");
            show.invoke(productor, null);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Test中代码变多了,但是后期维护起来方便了很多,用户需要什么产品只需要修改属性文件里的配置就行,不需要重新再编译代码。


猜你喜欢

转载自blog.csdn.net/yk2fxy/article/details/78363074
今日推荐