Re: [問題] package 與 class 名稱相同問題
那就只好使用比較麻煩的方式......
run tw.sub2.test時將會呼叫tw.sub1.test.hello()
package tw.sub1;
public class test {
public void hello() {
System.out.println("Hello");
}
}
========================================
package tw.sub2;
import java.lang.reflect.Method;
public class test {
public static void main(String[] args) {
try {
Class<?> cls = Class.forName("tw.sub1.test");
Method method = cls.getMethod("hello")
method.invoke(cls.newInstance());
} catch (Exception e) {
e.printStackTrace();
}
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.112.115.88
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 3 之 4 篇):