//在泛型为Integer的ArrayList中存放一个String类型的对象package net . xsoftlab . baike ;import java.lang.reflect.Method ;import java.util.ArrayList ;public class TestReflect {public static void main ( String [] args ) throws Exception {ArrayList < Integer > list = new ArrayList < Integer >();Method method = list . getClass (). getMethod ( "add" , Object . class );method . invoke ( list , "Java反射机制实例。" );System . out . println ( list . get ( 0 ));}}
//通过反射取得并修改数组的信息package net . xsoftlab . baike ;import java.lang.reflect.Array ;public class TestReflect {public static void main ( String [] args ) throws Exception {int [] temp = { 1 , 2 , 3 , 4 , 5 };Class <?> demo = temp . getClass (). getComponentType ();System . out . println ( "数组类型: " + demo . getName ());System . out . println ( "数组长度 " + Array . getLength ( temp ));System . out . println ( "数组的第一个元素: " + Array . get ( temp , 0 ));Array . set ( temp , 0 , 100 );System . out . println ( "修改之后数组第一个元素为: " + Array . get ( temp , 0 ));}}
//通过反射机制修改数组的大小package net . xsoftlab . baike ;import java.lang.reflect.Array ;public class TestReflect {public static void main ( String [] args ) throws Exception {int [] temp = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 };int [] newTemp = ( int []) arrayInc ( temp , 15 );print ( newTemp );String [] atr = { "a" , "b" , "c" };String [] str1 = ( String []) arrayInc ( atr , 8 );print ( str1 );}// 修改数组大小public static Object arrayInc ( Object obj , int len ) {Class <?> arr = obj . getClass (). getComponentType ();Object newArr = Array . newInstance ( arr , len );int co = Array . getLength ( obj );System . arraycopy ( obj , 0 , newArr , 0 , co );return newArr ;}// 打印public static void print ( Object obj ) {Class <?> c = obj . getClass ();if (! c . isArray ()) {return ;}System . out . println ( "数组长度为: " + Array . getLength ( obj ));for ( int i = 0 ; i < Array . getLength ( obj ); i ++) {System . out . print ( Array . get ( obj , i ) + " " );}System . out . println ();}}
//将反射机制应用于工厂模式package net . xsoftlab . baike ;interface fruit {public abstract void eat ();}class Apple implements fruit {public void eat () {System . out . println ( "Apple" );}}class Orange implements fruit {public void eat () {System . out . println ( "Orange" );}}class Factory {public static fruit getInstance ( String ClassName ) {fruit f = null ;try {f = ( fruit ) Class . forName ( ClassName ). newInstance ();} catch ( Exception e ) {e . printStackTrace ();}return f ;}}/*** 对于普通的工厂模式当我们在添加一个子类的时候,就需要对应的修改工厂类。 当我们添加很多的子类的时候,会很麻烦。* Java 工厂模式可以参考* http://baike.xsoftlab.net/view/java-factory-pattern** 现在我们利用反射机制实现工厂模式,可以在不修改工厂类的情况下添加任意多个子类。** 但是有一点仍然很麻烦,就是需要知道完整的包名和类名,这里可以使用properties配置文件来完成。** java 读取 properties 配置文件 的方法可以参考* http://baike.xsoftlab.net/view/java-read-the-properties-configuration-file** @author xsoftlab.net*/public class TestReflect {public static void main ( String [] args ) throws Exception {fruit f = Factory . getInstance ( "net.xsoftlab.baike.Apple" );if ( f != null ) {f . eat ();}}}
javaApplication.java
最新推荐文章于 2023-11-22 18:02:50 发布