Class 获取方法名称的三种方法

 

 

package cn.test.scheduleTest;

import java.util.jar.JarFile;

/*
* Class获取方法名称的三种方法
*/
public class JarUtilTest {

public static void main(String[] args) throws Exception {
jarTest();
}

public static void jarTest() throws Exception {
// 1种
Class className = Class.forName("cn.test.scheduleTest.TimerTest");
System.err.println("className---" + className);
System.err.println("--------1---------");
// 2种
Class TimerTest = cn.test.scheduleTest.TimerTest.class;
System.err.println("TimerTest---" + TimerTest);
className.newInstance();
System.err.println("-------------2----------");
// 3种
cn.test.scheduleTest.TimerTest test3 = new TimerTest();
System.err.println("test3----" + test3.getClass());
System.err.println("----------------3--------");
}
}

-------------------

调用的方法类

 

package cn.test.scheduleTest;

import java.util.Timer;

public class TimerTest {

public static void main(String[] args) {
TimerT();
}

public static void TimerT() {

Timer timer = new Timer();
timer.schedule(new MyTest(), 1000, 2000);
while (true) {
try {
int ch = System.in.read();
if (ch - 'c' == 0) {
timer.cancel();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}

public TimerTest() {
System.err.println("构造函数。。。。。");
}

static {
System.err.println("静态函数。。。");
}
{
System.err.println("非静态函数。。。。");
}
}

---------

以下三种不同的调用方式的打印结果:

静态函数。。。
className---class cn.test.scheduleTest.TimerTest
--------1---------
TimerTest---class cn.test.scheduleTest.TimerTest
非静态函数。。。。
构造函数。。。。。
-------------2----------
非静态函数。。。。
构造函数。。。。。
test3----class cn.test.scheduleTest.TimerTest
----------------3--------

 

OK ----菜鸟级别

posted @ 2013-05-17 14:19  szl_work  阅读(1285)  评论(0)    收藏  举报