Java基礎筆記14

來源:互聯網
上載者:User

標籤:有一個   getname   generic   構造   dem   win   demo   ons   屬性   

1.反射.
Class:反射類 任何一個類都有一個Class反射類.(影子)
java.lang.reflect.*;
Field:欄位類
Method:方法類類
Constructor:構造方法類.

2.擷取反射類
//1.擷取反射類 類名.class
Class clazz1=People.class;
//2.通過全類名
Class clazz2=Class.forName("demo.People");
//3.對象擷取
People p=new People();
Class clazz3=p.getClass();
3.通過反射類執行個體化對應的類對象.
clazz.newInstance();

4.通過反射類擷取類中的屬性.
//擷取父類或自己本類中public修飾的屬性
Field ageField= clazz1.getField("age");
ageField.set(p, 156);
//只擷取本類中自己聲明的屬性
Field nameField=clazz1.getDeclaredField("name");
nameField.setAccessible(true);//允許賦值
nameField.set(p, "張三");
System.out.println(p);
5.擷取某個類的全部方法
Method showMethod= clazz1.getMethod("print",int.class);
//調用該方法
showMethod.invoke(p,15);

Method[] ms=clazz1.getDeclaredMethods();
for(Method m:ms){
System.out.println(Modifier.toString(m.getModifiers())+" "
+m.getReturnType().getName()+" "+m.getName()+"("+Arrays.asList(m.getGenericParameterTypes())+")");
}











Java基礎筆記14

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.