Java鍙嶅皠鏈哄埗

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java鍙嶅皠鏈哄埗相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/system' title='system'>system   灏辨槸   methods   涓嶇敤   鍙橀噺   瀹樻柟   str   lan   鍖哄垎   

鍐欏湪鍓嶉潰锛?br/>浠€涔堟槸java鍙嶅皠鏈哄埗锛熸垜浠張涓轰粈涔堣瀛﹀畠锛?/em>
褰撶▼搴忚繍琛屾椂锛屽厑璁告敼鍙樼▼搴忕粨鏋勬垨鍙橀噺绫诲瀷锛岃繖绉嶈瑷€绉颁负鍔ㄦ€佽瑷€銆傛垜浠涓簀ava骞朵笉鏄姩鎬佽瑷€锛屼絾鏄畠鍗存湁涓€涓潪甯哥獊鍑虹殑鍔ㄦ€佺浉鍏虫満鍒讹紝淇楃О锛氬弽灏勩€?br/> IT琛屼笟閲岃繖涔堣锛屾病鏈夊弽灏勪篃灏辨病鏈夋鏋讹紝鐜版湁鐨勬鏋堕兘鏄互鍙嶅皠涓哄熀纭€銆傚湪瀹為檯椤圭洰寮€鍙戜腑锛岀敤鐨勬渶澶氱殑鏄鏋讹紝濉殑鏈€澶氱殑鏄被锛屽弽灏勮繖涓€姒傚康灏辨槸灏嗘鏋跺拰绫绘弶鍦ㄤ竴璧风殑璋冨拰鍓傘€傛墍浠ワ紝鍙嶅皠鎵嶆槸鎺ヨЕ椤圭洰寮€鍙戠殑鏁查棬鐮栵紒


涓€銆丆lass绫?br/>浠€涔堟槸Class绫伙紵
鍦ㄩ潰鍚戝璞$殑涓栫晫閲岋紝涓囦簨涓囩墿鐨嗘槸瀵硅薄銆傝€屽湪java璇█涓紝static淇グ鐨勪笢瑗夸笉鏄璞★紝浣嗘槸瀹冨睘浜庣被銆傛櫘閫氱殑鏁版嵁绫诲瀷涓嶆槸瀵硅薄锛屼緥濡傦細int a = 5;瀹冧笉鏄潰鍚戝璞★紝浣嗘槸瀹冩湁鍏跺寘瑁呯被 Integer 鎴栬€呭垎瑁呯被鏉ュ讥琛ヤ簡瀹冦€傞櫎浜嗕互涓婁袱绉嶄笉鏄潰鍚戝璞★紝鍏朵綑鐨勫寘鎷被涔熸湁瀹冪殑闈㈠悜瀵硅薄锛岀被鏄痡ava.lang.Class鐨勫疄渚嬪寲瀵硅薄锛堟敞鎰廋lass鏄ぇ鍐欙級銆備篃灏辨槸璇达細
Class A{}
褰撴垜鍒涘缓浜咥绫伙紝閭d箞绫籄鏈韩灏辨槸涓€涓璞★紝璋佺殑瀵硅薄锛焜ava.lang.Class鐨勫疄渚嬪璞°€?br/> 閭d箞杩欎釜瀵硅薄鍙堣鎬庝箞琛ㄧず鍛紵
鎴戜滑鍏堢湅涓€涓嬩笅闈㈣繖娈典唬鐮侊細

1
2
3
4
public class Demo(){
F f=new F();
}
class F{}

杩欓噷鐨凢鐨勫疄渚嬪寲瀵硅薄灏卞彲浠ョ敤f琛ㄨ揪鍑烘潵銆傚悓鐞咶绫讳篃鏄竴涓疄渚嬪寲瀵硅薄锛孋lass绫荤殑瀹炰緥鍖栧璞°€傛垜浠彲浠ョ悊瑙d负浠讳綍涓€涓被閮芥槸Class绫荤殑瀹炰緥鍖栧璞★紝杩欑瀹炰緥鍖栧璞℃湁涓夌琛ㄧず鏂规硶锛?/p>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Demo(){
F f=new F();
//绗竴绉嶈〃杈炬柟寮?/code>
Class c1=F.class;//杩欑琛ㄨ揪鏂瑰紡鍚屾椂涔熷憡璇変簡鎴戜滑浠讳綍涓€涓被閮芥湁涓€涓殣鍚殑闈欐€佹垚鍛樺彉閲廲lass
//绗簩绉嶈〃杈炬柟寮?/code>
Class c2=f.getClass();//杩欑琛ㄨ揪鏂瑰紡鍦ㄥ凡鐭ヤ簡璇ョ被鐨勫璞$殑鎯呭喌涓嬮€氳繃getClass鏂规硶鑾峰彇
//绗笁绉嶈〃杈炬柟寮?/code>
Class c3 = null;
try {
c3 = Class.forName("com.text.F");//绫荤殑鍏ㄧО
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
class F{}

浠ヤ笂涓夌琛ㄨ揪鏂瑰紡锛宑1,c2,c3閮借〃绀轰簡F绫荤殑绫荤被鍨嬶紝涔熷氨鏄畼鏂硅В閲婄殑Class Type銆?br/> 閭d箞闂鏉ヤ簡锛?/p>

1
System.out.println(c1 == c2)?  or  System.out.println(c1 == c3)?

绛旀鏄偗瀹氱殑锛岃繑鍥炲€间负ture銆傝繖琛ㄦ槑涓嶈c1 or c2 or c3閮戒唬琛ㄤ簡F绫荤殑绫荤被鍨嬶紝涔熷氨鏄涓€涓被鍙彲鑳芥槸Class绫荤殑涓€涓疄渚嬪璞°€?br/> 鐞嗚В浜咰lass鐨勬蹇碉紝鎴戜滑涔熷彲浠ラ€氳繃绫荤殑绫荤被鍨嬪垱寤鸿绫荤殑瀵硅薄瀹炰緥锛岀敤c1 or c2 or c3鐨刵ewInstance()鏂规硶锛?/p>

1
2
3
4
5
6
7
8
9
10
11
12
13
Public class Demo1{
try {
Foo foo = (Foo)c1.newInstance();//foo灏辫〃绀篎绫荤殑瀹炰緥鍖栧璞?/code>
foo.print();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}}
class F{
void print(){
}
}

杩欓噷闇€瑕佹敞鎰忕殑鏄紝c1鏄疐绫荤殑绫荤被鍨嬶紝鍒涘缓鍑烘潵鐨勫氨鏄疐绫荤殑瀵硅薄銆傚鏋渁鏄疉绫荤殑绫荤被鍨嬶紝閭d箞鍒涘缓鍑烘潵鐨勫璞′篃搴旇涓庝箣瀵瑰簲锛屽睘浜嶢绫荤殑瀵硅薄銆?/p>

浜屻€佹柟娉曠殑鍙嶅皠
Class绫绘湁涓€涓渶绠€鍗曠殑鏂规硶锛実etName()锛?/p>

1
2
3
4
5
6
7
8
9
10
11
public class Demo2 {
public static void main(String[] args) {
Class c1 = int.class;//int 鐨勭被绫诲瀷
Class c2 = String.class;//String绫荤殑绫荤被鍨?/code>
Class c3 = void.class;
System.out.println(c1.getName());
System.out.println(c2.getName());
System.out.println(c2.getSimpleName());
System.out.println(c3.getName());
}
}

鏈殑鏁版嵁绫诲瀷浠ュ強void鍏抽敭瀛楅兘鏄瓨鍦ㄧ被绫诲瀷鐨勩€?/span>

妗堜緥锛?/p>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class ClassUtil {
public static void printClassMethodMessage(Object obj){
//瑕佽幏鍙栫被鐨勪俊鎭€嬨€嬮鍏堟垜浠鑾峰彇绫荤殑绫荤被鍨?/code>
Class c = obj.getClass();
//鎴戜滑鐭ラ亾Object绫绘槸涓€鍒囩被鐨勭埗绫伙紝鎵€浠ユ垜浠紶閫掔殑鏄摢涓瓙绫荤殑瀵硅薄锛宑灏辨槸璇ュ瓙绫荤殑绫荤被鍨嬨€?/code>
//鎺ヤ笅鏉ユ垜浠鑾峰彇绫荤殑鍚嶇О
System.out.println("绫荤殑鍚嶇О鏄?"+c.getName());
/*
*鎴戜滑鐭ラ亾锛屼竾浜嬩竾鐗╅兘鏄璞★紝鏂规硶涔熸槸瀵硅薄锛屾槸璋佺殑瀵硅薄鍛紵
* 鍦╦ava閲岄潰锛屾柟娉曟槸Method绫荤殑瀵硅薄
*涓€涓垚鍛樻柟娉曞氨鏄竴涓狹ethod鐨勫璞★紝閭d箞Method灏卞皝瑁呬簡瀵硅繖涓垚鍛?/code>
*鏂规硶鐨勬搷浣?/code>
*/
//濡傛灉鎴戜滑瑕佽幏寰楁墍鏈夌殑鏂规硶锛屽彲浠ョ敤getMethods()鏂规硶锛岃繖涓柟娉曡幏鍙栫殑鏄墍鏈夌殑Public鐨勫嚱鏁帮紝鍖呮嫭鐖剁被缁ф壙鑰屾潵鐨勩€傚鏋滄垜浠鑾峰彇鎵€鏈夎绫昏嚜宸卞0鏄庣殑鏂规硶锛屽氨鍙互鐢╣etDeclaredMethods()鏂规硶锛岃繖涓柟娉曟槸涓嶉棶璁块棶鏉冮檺鐨勩€?/code>
Method[] ms = c.getMethods();//c.getDeclaredMethods()
//鎺ヤ笅鏉ユ垜浠嬁鍒拌繖浜涙柟娉曚箣鍚庡共浠€涔堬紵鎴戜滑灏卞彲浠ヨ幏鍙栬繖浜涙柟娉曠殑淇℃伅锛屾瘮濡傛柟娉曠殑鍚嶅瓧銆?/code>
//棣栧厛鎴戜滑瑕佸惊鐜亶鍘嗚繖浜涙柟娉?/code>
for(int i = 0; i < ms.length;i++){
//鐒跺悗鍙互寰楀埌鏂规硶鐨勮繑鍥炲€肩被鍨嬬殑绫荤被鍨?/code>
Class returnType = ms[i].getReturnType();
//寰楀埌鏂规硶鐨勮繑鍥炲€肩被鍨嬬殑鍚嶅瓧
System.out.print(returnType.getName()+" ");
//寰楀埌鏂规硶鐨勫悕绉?/code>
System.out.print(ms[i].getName()+"(");
//鑾峰彇鍙傛暟绫诲瀷--->寰楀埌鐨勬槸鍙傛暟鍒楄〃鐨勭被鍨嬬殑绫荤被鍨?/code>
Class[] paramTypes = ms[i].getParameterTypes();
for (Class class1 : paramTypes) {
System.out.print(class1.getName()+",");
}
System.out.println(")");
}
}
}

鎬荤粨鎬濊矾锛?br/> 閫氳繃鏂规硶鐨勫弽灏勫緱鍒拌绫荤殑鍚嶇О姝ラ锛?br/> 1.鑾峰彇璇ョ被鐨勭被绫诲瀷
2.閫氳繃绫荤被鍨嬭幏鍙栫被鐨勬柟娉曪紙getMethods()锛?br/> 3.寰幆閬嶅巻鎵€鑾峰彇鍒扮殑鏂规硶
4.閫氳繃杩欎簺鏂规硶鐨刧etReturnType()寰楀埌杩斿洖鍊肩被鍨嬬殑绫荤被鍨嬶紝鍙堥€氳繃璇ョ被绫诲瀷寰楀埌杩斿洖鍊肩被鍨嬬殑鍚嶅瓧
5.getName()寰楀埌鏂规硶鐨勫悕绉帮紝getParameterTypes()鑾峰彇杩欎釜鏂规硶閲岄潰鐨勫弬鏁扮被鍨嬬殑绫荤被鍨嬨€?/p>

涓夈€佹垚鍛樺彉閲忕殑鍙嶅皠
棣栧厛鎴戜滑闇€瑕佽璇嗗埌鎴愬憳鍙橀噺涔熸槸瀵硅薄锛屾槸java.lang.reflect.Field绫荤殑瀵硅薄锛岄偅涔堜篃灏辨槸璇碏ield绫诲皝瑁呬簡鍏充簬鎴愬憳鍙橀噺鐨勬搷浣溿€傛棦鐒跺畠灏佽浜嗘垚鍛樺彉閲忥紝鎴戜滑鍙堣濡備綍鑾峰彇杩欎簺鎴愬憳鍙橀噺鍛紵瀹冩湁杩欎箞涓€涓柟娉曪細

1
2
3
4
5
public class ClassUtil {
public static void printFieldMessage(Object obj){
Class c = obj.getClass();
//Field[] fs = c.getFields();
}

杩欓噷鐨刧etFields()鏂规硶鑾峰彇鐨勬墍鏈夌殑public鐨勬垚鍛樺彉閲忕殑淇℃伅銆傚拰鏂规硶鐨勫弽灏勯偅閲宲ublic鐨勬垚鍛樺彉閲忥紝涔熸湁涓€涓幏鍙栨墍鏈夎嚜宸卞0鏄庣殑鎴愬憳鍙橀噺鐨勪俊鎭細
Field[] fs = c.getDeclaredFields();

鎴戜滑寰楀埌瀹冧箣鍚庯紝鍙互杩涜閬嶅巻锛堟棦鐒跺皝瑁呬簡Field鐨勪俊鎭紝閭d箞鎴戜滑灏卞彲浠ュ緱鍒癋ield绫诲瀷锛?/p>

1
2
3
4
5
6
7
8
for (Field field : fs) {
//寰楀埌鎴愬憳鍙橀噺鐨勭被鍨嬬殑绫荤被鍨?/code>
Class fieldType = field.getType();
String typeName = fieldType.getName();
//寰楀埌鎴愬憳鍙橀噺鐨勫悕绉?/code>
String fieldName = field.getName();
System.out.println(typeName+" "+fieldName);
}

鍥涖€佹瀯閫犲嚱鏁扮殑鍙嶅皠
涓嶈鏄柟娉曠殑鍙嶅皠銆佹垚鍛樺彉閲忕殑鍙嶅皠銆佹瀯閫犲嚱鏁扮殑鍙嶅皠锛屾垜浠彧闇€瑕佺煡閬擄細瑕佹兂鑾峰彇绫荤殑淇℃伅锛岄鍏堝緱鑾峰彇绫荤殑绫荤被鍨嬨€?/p>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public static void printConMessage(Object obj){
Class c = obj.getClass();
/*
* 棣栧厛鏋勯€犲嚱鏁颁篃鏄璞★紝鏄痡ava.lang.Constructor绫荤殑瀵硅薄
* 涔熷氨鏄痡ava.lang. Constructor涓皝瑁呬簡鏋勯€犲嚱鏁扮殑淇℃伅
* 鍜屽墠闈㈣鍒扮殑涓€鏍凤紝瀹冧篃鏈変袱涓柟娉曪細
* getConstructors()鏂规硶鑾峰彇鎵€鏈夌殑public鐨勬瀯閫犲嚱鏁?/code>
* getDeclaredConstructors()鏂规硶寰楀埌鎵€鏈夌殑鑷繁澹版槑鐨勬瀯閫犲嚱鏁?/code>
*/
//Constructor[] cs = c.getConstructors();
Constructor[] cs = c.getDeclaredConstructors();
for (Constructor constructor : cs) {
//鎴戜滑鐭ラ亾鏋勯€犳柟娉曟槸娌℃湁杩斿洖鍊肩被鍨嬬殑锛屼絾鏄垜浠彲浠ワ細
System.out.print(constructor.getName()+"(");
//鑾峰彇鏋勯€犲嚱鏁扮殑鍙傛暟鍒楄〃銆嬨€嬪緱鍒扮殑鏄弬鏁板垪琛ㄧ殑绫荤被鍨?/code>
Class[] paramTypes = constructor.getParameterTypes();
for (Class class1 : paramTypes) {
System.out.print(class1.getName()+",");
}
System.out.println(")");
}
}

浜斻€丆lass绫荤殑鍔ㄦ€佸姞杞界被
濡備綍鍔ㄦ€佸姞杞戒竴涓被鍛紵
棣栧厛鎴戜滑闇€瑕佸尯鍒嗕粈涔堟槸鍔ㄦ€佸姞杞斤紵浠€涔堟槸闈欐€佸姞杞斤紵鎴戜滑鏅亶璁や负缂栬瘧鏃跺埢鍔犺浇鐨勭被鏄潤鎬佸姞杞界被锛岃繍琛屾椂鍒诲姞杞界殑绫绘槸鍔ㄦ€佸姞杞界被銆傛垜浠妇涓€涓緥瀛愶細

1
2
3
4
5
6
7
8
9
10
11
12
Class A{
Public static void main(String[] args){
if("B".equal(args[0])){
B b=new B();
b.start();
}
if("C".equal(args[0])){
C c=new C();
C.start();
}
}
}

涓婇潰杩欎竴娈典唬鐮侊紝褰撴垜浠湪鐢╡clipse鎴栬€卪yeclipse鐨勬椂鍊欐垜浠苟涓嶅叧蹇冩槸鍚﹁兘澶熼€氳繃缂栬瘧锛屽綋鎴戜滑鐩存帴鍦╟md浣跨敤javac璁块棶A.java绫荤殑鏃跺€欙紝灏变細鎶涘嚭闂:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
A.java锛?/code>7锛氶敊璇細鎵句笉鍒扮鍙?/code>
B b=new B();
绗﹀彿:  绫籅
浣嶇疆锛?绫籄
A.java锛?/code>7锛氶敊璇細鎵句笉鍒扮鍙?/code>
B b=new B();
绗﹀彿:  绫籅
浣嶇疆锛?绫籄
A.java锛?/code>12锛氶敊璇細鎵句笉鍒扮鍙?/code>
C c=new C();
绗﹀彿:  绫籆
浣嶇疆锛?绫籄
A.java锛?/code>12锛氶敊璇細鎵句笉鍒扮鍙?/code>
C c=new C();
绗﹀彿:  绫籆
浣嶇疆锛?绫籄
4涓敊璇?/code>

鎴栬鎴戜滑鐞嗘墍褰撶劧鐨勮涓鸿繖鏍峰簲璇ユ槸閿欙紝绫籅鏍规湰灏变笉瀛樺湪銆備絾鏄鏋滄垜浠鎬濊€冧竴涓嬶紝灏变細鍙戠幇B涓€瀹氱敤鍚楋紵涓嶄竴瀹氥€侰涓€瀹氱敤鍚楋紵涔熶笉涓€瀹氥€傞偅涔堝ソ锛岀幇鍦ㄦ垜浠氨璁〣绫诲瓨鍦?/p>

1
2
3
4
5
Class B{
Public static void start(){
System.out.print("B...satrt");
}
}

鐜板湪鎴戜滑灏卞厛 javac B.class,璁〣绫诲厛寮€濮嬬紪璇戙€傜劧鍚庡湪杩愯javac A.class銆傜粨鏋滄槸锛?/p>

1
2
3
4
5
6
7
8
9
A.java锛?/code>12锛氶敊璇細鎵句笉鍒扮鍙?/code>
C c=new C();
绗﹀彿:  绫籆
浣嶇疆锛?绫籄
A.java锛?/code>12锛氶敊璇細鎵句笉鍒扮鍙?/code>
C c=new C();
绗﹀彿:  绫籆
浣嶇疆锛?绫籄
2涓敊璇?/code>

鎴戜滑鍐嶆兂锛岃繖涓▼搴忔湁浠€涔堥棶棰樸€傚鏋滀綘璇存病鏈変粈涔堥棶棰橈紵C绫绘湰鏉ュ氨涓嶅瓨鍦ㄥ晩锛侀偅涔堥棶棰樻潵浜咮绫诲凡缁忓瓨鍦ㄤ簡锛屽亣璁炬垜鐜板湪灏辨兂鐢˙锛屾垜浠繖涓▼搴忕敤寰椾簡鍚楋紵绛旀鏄偗瀹氱殑锛岀敤涓嶄簡銆傞偅鐢ㄤ笉浜嗙殑鍘熷洜鏄粈涔堬紵鍥犱负鎴戜滑杩欎釜绋嬪簭鏄仛鐨勭被鐨勯潤鎬佸姞杞斤紝涔熷氨鏄new鍒涘缓瀵硅薄鏄潤鎬佸姞杞界被锛屽湪缂栬瘧鏃跺埢灏遍渶瑕佸姞杞芥墍鏈夌殑锛屽彲鑳戒娇鐢ㄥ埌鐨勭被銆傛墍浠ヤ笉绠′綘鐢ㄤ笉鐢ㄨ繖涓被銆?br/> 鐜板湪B绫绘槸瀛樺湪鐨勶紝浣嗘槸鎴戜滑杩欎釜绋嬪簭浠嶇劧鐢ㄤ笉浜嗭紝鍥犱负浼氫竴鐩存姤C绫绘湁闂锛屾墍浠绫绘垜涔熺敤涓嶄簡銆傞偅涔堝湪瀹為檯搴旂敤褰撲腑锛屾垜浠偗瀹氶渶瑕佸鏋淏绫诲瓨鍦紝B绫绘垜灏辫兘鐢紝褰撶敤C绫荤殑鏃跺€欙紝浣犲啀鍛婅瘔鎴戦敊浜嗐€傚鏋滆灏嗘潵浣犳湁100涓被锛屽彧瑕佸叾涓竴涓被鍑虹幇闂锛屽叾瀹?9涓被浣犻兘鐢ㄤ笉浜嗐€傛墍浠ヨ繖骞朵笉鏄垜浠兂瑕佺殑銆?br/> 鎴戜滑鎯宠鐨勫氨鏄垜鐢ㄩ偅涓被灏卞姞杞介偅涓被锛屼篃灏辨槸甯歌鐨?em>杩愯鏃跺埢鍔犺浇锛屽姩鎬佸姞杞界被銆傚浣曞疄鐜板姩鎬佸姞杞界被鍛紵鎴戜滑鍙互寤鸿繖涔堜竴涓被锛?/p>

1
2
3
4
5
6
7
8
9
10
11
Class All{
Public static void start(){
try{
Class cl= Class.forName(args[0]);
//閫氳繃绫荤被鍨嬶紝鍒涘缓璇ョ被鐨勫璞?/code>
cl.newInstance();
}catch(Exception e){
e.printStackTrace();
}
}
}

鍓嶉潰鎴戜滑鍦ㄥ垎鏋怌lass瀹炰緥鍖栧璞$殑鏂瑰紡鐨勬椂鍊欙紝Class.forName(鈥滅被鐨勫叏绉扳€?锛屽畠涓嶄粎浠呰〃绀轰簡绫荤殑绫荤被鍨嬶紝杩樿〃绀轰簡鍔ㄦ€佸姞杞界被銆傚綋鎴戜滑javac All.java鐨勬椂鍊欙紝瀹冧笉浼氭姤浠讳綍閿欒锛屼篃灏辨槸璇村湪缂栬瘧鐨勬椂鍊欐槸娌℃湁閿欒鐨勩€傚彧鏈夊綋鎴戜滑鍏蜂綋鐢ㄦ煇涓被鐨勬椂鍊欙紝閭d釜绫讳笉瀛樺湪锛屽畠鎵嶄細鎶ラ敊銆?br/> 濡傛灉鍔犺浇鐨勭被鏄疊绫伙紝灏遍渶瑕侊細

1
B bt = (B) cl.newInstance();

涓囦竴鍔犺浇鐨勬槸C绫诲憿锛屽彲浠ユ敼鎴?/p>

1
C ct = (C) cl.newInstance();

浣嗘槸濡傛灉鎴戞兂鐢ㄥ緢澶氱殑绫绘垨鑰呭姞杞藉緢澶氱殑绫伙紝璇ユ€庝箞鍔烇紵鎴戜滑鍙互缁熶竴涓€涓爣鍑嗭紝涓嶈C绫昏繕鏄疊绫绘垨鑰呭叾浠栫殑绫伙紝姣斿瀹氫箟涓€涓爣鍑?/p>

1
Stand s = (Stand) cl.newInstance();

鍙B绫诲拰C绫婚兘鏄繖涓爣鍑嗙殑灏辫浜嗐€?/p>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Class All{
Public static void start(){
try{
Class cl= Class.forName(args[0]);
//閫氳繃绫荤被鍨嬶紝鍒涘缓璇ョ被鐨勫璞?/code>
Stand s = (Stand) cl.newInstance();
s.start();
}catch(Exception e){
e.printStackTrace();
}
}
}
interface Stand {
Public void start();
}

鐜板湪濡傛灉鎴戞兂瑕佺敤B绫伙紝鎴戜滑鍙渶瑕侊細

1
2
3
4
5
Class B implements Stand{
Public void start(){
System.out.print("B...satrt");
}
}

鍔犺浇B绫?缂栬瘧杩愯銆?/p>

1
2
3
javac B.java
javac Stand.java
java Stand B

缁撴灉锛?/p>

1
B...satrt

濡傛灉浠ュ悗鎯崇敤鏌愪竴涓被锛屼笉闇€瑕侀噸鏂扮紪璇戯紝鍙渶瑕佸疄鐜拌繖涓爣鍑嗙殑鎺ュ彛鍗冲彲銆傚彧闇€瑕佸姩鎬佺殑鍔犺浇鏂扮殑涓滆タ灏辫浜嗐€?br/> 杩欏氨鏄?em>鍔ㄦ€佸姞杞界被銆?/p>


以上是关于Java鍙嶅皠鏈哄埗的主要内容,如果未能解决你的问题,请参考以下文章

Java鍙嶅皠

Java鍙嶅皠浠嬬粛

Cookie/Session鏈哄埗璇﹁В

MyBatis鎺㈢┒-----缂撳瓨鏈哄埗璇﹁В

js 鎵ц鏈哄埗

娣卞叆娴呭嚭JavaScript杩愯鏈哄埗