java通过类名动态调用类
2011/12/23 9:32:15 biline http://biline.iteye.com
我要评论(0)
- 摘要:java通过类名动态调用类,可利用配置文件实现无编译动态增加删除模块。代码是转的啦~嘿嘿,mark一下~packagetf;publicclassTestPack{publicstaticvoidmain(String[]args){try{IAnimalcAnimal=newbird();cAnimal.shout();IAnimalanimal=(IAnimal)java.lang.Class.forName("tf.bird").newInstance();animal.shout();
- 标签:Java
java通过类名动态调用类,可利用配置文件实现无编译动态增加删除模块。代码是转的啦~嘿嘿,mark一下~
package tf;
public class TestPack {
public static void main(String [] args)
{
try
{
IAnimal cAnimal = new bird();
cAnimal.shout();
IAnimal animal =(IAnimal)java.lang.Class.forName("tf.bird").newInstance();
animal.shout();
}
catch (Exception e) {
// TODO: handle exception
}
}
}
//tf.bird 加上包名
//bird.java
package tf;
public class bird implements IAnimal{
public void shout()
{
System.out.println("c c jiji ");
}
}
//IAnimal.java
package tf;
public interface IAnimal {
public void shout();
}
c c jiji
c c jiji