1
public
class
ProxoolDriver
implements
Driver {
2
????
private
static
final
Log LOG = LogFactory.getLog(ProxoolDriver.
class
);
3
????
static
{
4
????????
try
{
5
???????????? DriverManager.registerDriver(
new
ProxoolDriver());
6
???????? }
catch
(SQLException e) {
7
??????????? System.out.println(e.toString());
8
??????? }
9
??? }
10
}
? ? ? ? ? ? ? ? ? JVM將使用類A的類裝載器, 將類A裝入內存(前提是:類A還沒有裝入內存),不對類A做類的初始化工作.返回類A的Class的對象。
? ? ? ? ? ? ? ? ? 返回引用o運行時真正所指的對象(因為:子對象的引用可能會賦給父對象的引用變量中)所屬的類的Class的對象 。
1
public
class
Person {
2
????
private
String name = "Alfira"
;
3
????
public
void
getName() {
4
??????? System.out.println(name);
5
??? }
6
????
public
void
setName(String name,
int
a) {
7
????????
this
.name = name +
a;
8
??? }
9
}
?1
import
java.lang.reflect.Method;
2
3
public
class
Test {
4
5
????
/**
6
???? *
@param
args
7
?????
*/
8
????
public
static
void
main(String[] args) {
9
???????? show("yerasel.Person"
);
10
??? }
11
12
????
private
static
void
show(String name) {
13
????????
try
{
14
????????????
//
JVM將使用類A的類裝載器,將類A裝入內存(前提是:類A還沒有裝入內存),不對類A做類的初始化工作
15
???????????? Class classtype3 = Person.
class
;
16
????????????
//
獲得classtype中的方法
17
???????????? Method getMethod3 = classtype3.getMethod("getName",
new
Class[] {});
18
???????????? Class[] parameterTypes3 = { String.
class
,
int
.
class
};
19
???????????? Method setMethod3 =
classtype3
20
???????????????????? .getMethod("setName"
, parameterTypes3);
21
22
????????????
//
實例化對象,因為這一句才會輸出“靜態初始化”以及“初始化”
23
???????????? Object obj3 =
classtype3.newInstance();
24
????????????
//
通過實例化后的對象調用方法
25
???????????? getMethod3.invoke(obj3);
//
獲取默認值
26
???????????? setMethod3.invoke(obj3, "Setting new ", 3);
//
設置
27
???????????? getMethod3.invoke(obj3);
//
獲取最新
28
???????????? System.out.println("----------------"
);
29
30
????????????
//
返回運行時真正所指的對象
31
???????????? Person p =
new
Person();
32
???????????? Class classtype = p.getClass();
//
Class.forName(name);
33
????????????
//
獲得classtype中的方法
34
???????????? Method getMethod = classtype.getMethod("getName",
new
Class[] {});
35
???????????? Class[] parameterTypes = { String.
class
,
int
.
class
};
36
???????????? Method setMethod = classtype.getMethod("setName"
, parameterTypes);
37
???????????? getMethod.invoke(p);
//
獲取默認值
38
???????????? setMethod.invoke(p, "Setting new ", 1);
//
設置
39
???????????? getMethod.invoke(p);
//
獲取最新
40
???????????? System.out.println("----------------"
);
41
42
????????????
//
裝入類,并做類的初始化
43
???????????? Class classtype2 =
Class.forName(name);
44
????????????
//
獲得classtype中的方法
45
???????????? Method getMethod2 = classtype2.getMethod("getName",
new
Class[] {});
46
???????????? Class[] parameterTypes2 = { String.
class
,
int
.
class
};
47
???????????? Method setMethod2 =
classtype2
48
???????????????????? .getMethod("setName"
, parameterTypes2);
49
????????????
//
實例化對象
50
???????????? Object obj2 =
classtype2.newInstance();
51
????????????
//
通過實例化后的對象調用方法
52
???????????? getMethod2.invoke(obj2);
//
獲取默認值
53
???????????? setMethod2.invoke(obj2, "Setting new ", 2);
//
設置
54
???????????? getMethod2.invoke(obj2);
//
獲取最新
55
56
???????????? System.out.println("----------------"
);
57
58
???????? }
catch
(Exception e) {
59
??????????? System.out.println(e);
60
??????? }
61
??? }
62
}