java 通过反射调用方法

package com.dxm.servlet;

public interface MyServlet {
	public void init();
	public void service();
	public void destroy();
}
package com.dxm.servlet;

public class MyServletImpl implements MyServlet {

	@Override
	public void init() {
		System.out.println("init");
	}
	@Override
	public void service() {
		System.out.println("sevice");
	}

	@Override
	public void destroy() {
		System.out.println("destroy");
	}

}
package com.dxm.servlet;

import org.junit.Test;

public class TestServlet {
	@Test
	public void test()
	{
		
		try {
			String className="com.dxm.servlet.MyServletImpl";
			Class clazz = Class.forName(className);
			MyServletImpl my = (MyServletImpl) clazz.newInstance();
			my.init();
			my.service();
			my.destroy();
		} catch (Exception e) {

		}
	}
}

 

 

上一篇:poj 2506 Tiling(java解法)


下一篇:java运行bat命令得到某个windows文件的创建时间