本帖最后由 年轻的棒棒 于 2013-5-26 10:42 编辑
通过一个对象获得完整的包名和类名 - package reflect;
- class Demo{
- //other codes...
- }
- public class Hello{
- public static void main(String[] args) {
- Demo demo=new Demo();
- System.out.println(demo.getClass().getName());
- }
- }
复制代码实例化Class类对象- package reflect;
- import reflect.Demo;
- class Hello1{
- public static void main(String[] args) {
- Class<?> demo1=null;
- Class<?> demo2=null;
- Class<?> demo3=null;
- try{
- //一般尽量采用这种形式
- demo1=Class.forName("reflect.Demo");
- }catch(Exception e){
- e.printStackTrace();
- }
- demo2=new Demo().getClass();
- demo3=Demo.class;
- System.out.println("类名称 "+demo1.getName());
- System.out.println("类名称 "+demo2.getName());
- System.out.println("类名称 "+demo3.getName());
- }
- }</font>
复制代码 当我们把Person 中的默认的无参构造函数取消的时候,比如自己定义只定义一个有参数的构造函数之后。会出现错误- package reflect;
- class Person{
- public Person(String name,int age){
- this.age=age;
- this.name=name;
- };
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- @Override
- public String toString(){
- return"["+this.name+" "+this.age+"]";
- }
- private String name;
- private int age;
- }
-
- class Hello3{
- public static void main(String[] args) {
- Class<?> demo=null;
- try{
- demo=Class.forName("reflect.Person");
- }catch(Exception e) {
- e.printStackTrace();
- }
- Person per=null;
- try{
- per=(Person)demo.newInstance();
- }catch(InstantiationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }catch(IllegalAccessException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- per.setName("Rollen");
- per.setAge(20);
- System.out.println(per);
- }
- }
复制代码 所以大家以后再编写使用Class实例化其他类的对象的时候,一定要自己定义无参的构造函数
获得其他类中的全部构造函数
- package reflect;
- import java.lang.reflect.*;
- interface China{
- public static final String name="Rollen";
- public static int age=20;
- public void sayChina();
- public void sayHello(String name,int age);
- }
- class Person1 implements China{
- public Person1() {
-
- }
- public Person1(String sex){
- this.sex=sex;
- }
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- @Override
- public void sayChina(){
- System.out.println("hello ,china");
- }
- @Override
- public void sayHello(String name,int age){
- System.out.println(name+" "+age);
- }
- private String sex;
- }
-
- class Hello4{
- public static void main(String[] args) {
- Class<?> demo=null;
- try{
- demo=Class.forName("reflect.Person1");
- }catch(Exception e) {
- e.printStackTrace();
- }
- Constructor<?>cons[]=demo.getConstructors();
- for(int i =0; i < cons.length; i++) {
- Class<?> p[]=cons[i].getParameterTypes();
- System.out.print("构造方法: ");
- int mo=cons[i].getModifiers();
- System.out.print(Modifier.toString(mo)+" ");
- System.out.print(cons[i].getName());
- System.out.print("(");
- for(int j=0;j<p.length;++j){
- System.out.print(p[j].getName()+" arg"+i);
- if(j<p.length-1){
- System.out.print(",");
- }
- }
- System.out.println("){}");
- }
- }
- }
复制代码取得其他类的全部属性 - package reflect;
- import java.lang.reflect.*;
- class Hello6{
- public static void main(String[] args) {
- Class<?> demo =null;
- try{
- demo = Class.forName("reflect.Person1");
- }catch(Exception e) {
- e.printStackTrace();
- }
- System.out.println("===============本类属性========================");
- // 取得本类的全部属性
- Field[] field = demo.getDeclaredFields();
- for(int i =0; i < field.length; i++) {
- // 权限修饰符
- int mo = field[i].getModifiers();
- String priv = Modifier.toString(mo);
- // 属性类型
- Class<?> type = field[i].getType();
- System.out.println(priv +" "+ type.getName() +" "
- + field[i].getName() +";");
- }
- System.out.println("===============实现的接口或者父类的属性========================");
- // 取得实现的接口或者父类的属性
- Field[] filed1 = demo.getFields();
- for(int j =0; j < filed1.length; j++) {
- // 权限修饰符
- int mo = filed1[j].getModifiers();
- String priv = Modifier.toString(mo);
- // 属性类型
- Class<?> type = filed1[j].getType();
- System.out.println(priv +" "+ type.getName() +" "
- + filed1[j].getName() +";");
- }
- }
- }
复制代码通过反射修改数组大小 - package reflect;
- import java.lang.reflect.*;
- class Hello7{
- public static void main(String[] args) {
- int[] temp={1,2,3,4,5,6,7,8,9};
- int[] newTemp=(int[])arrayInc(temp,15);
- print(newTemp);
- System.out.println("=====================");
- String[] atr={"a","b","c"};
- String[] str1=(String[])arrayInc(atr,8);
- print(str1);
- }
-
- /**
- * 修改数组大小
- * */
- public static Object arrayInc(Object obj,int len){
- Class<?>arr=obj.getClass().getComponentType();
- Object newArr=Array.newInstance(arr, len);
- int co=Array.getLength(obj);
- System.arraycopy(obj,0, newArr,0, co);
- return newArr;
- }
- /**
- * 打印
- * */
- public static void print(Object obj){
- Class<?>c=obj.getClass();
- if(!c.isArray()){
- return;
- }
- System.out.println("数组长度为: "+Array.getLength(obj));
- for(int i =0; i < Array.getLength(obj); i++) {
- System.out.print(Array.get(obj, i)+" ");
- }
- }
- }
复制代码 有时候取得方法时会有一些异常- package reflect;
- import java.lang.reflect.*;
- class Hello5{
- public static void main(String[] args) {
- Class<?> demo=null;
- try{
- demo=Class.forName("reflect.Person1");
- }catch(Exception e) {
- e.printStackTrace();
- }
- Method method[]=demo.getMethods();
- for(int i=0;i<method.length;++i){
- Class<?> returnType=method[i].getReturnType();
- Class<?> para[]=method[i].getParameterTypes();
- int temp=method[i].getModifiers();
- System.out.print(Modifier.toString(temp)+" ");
- System.out.print(returnType.getName()+" ");
- System.out.print(method[i].getName()+" ");
- System.out.print("(");
- for(int j=0;j<para.length;++j){
- System.out.print(para[j].getName()+" "+"arg"+j);
- if(j<para.length-1){
- System.out.print(",");
- }
- }
- Class<?> exce[]=method[i].getExceptionTypes();
- if(exce.length>0){
- System.out.print(") throws ");
- for(int k=0;k<exce.length;++k){
- System.out.print(exce[k].getName()+" ");
- if(k<exce.length-1){
- System.out.print(",");
- }
- }
- }else{
- System.out.print(")");
- }
- System.out.println();
- }
- }
- }
复制代码 结果如下- public java.lang.String getSex ()
- public void setSex (java.lang.String arg0)
- public void sayChina ()
- public void sayHello (java.lang.String arg0,int arg1)
- public final native java.lang.Class getClass ()
- public native int hashCode ()
- public boolean equals (java.lang.Object arg0)
- public java.lang.String toString ()
- public final native void notify ()
- public final native void notifyAll ()
- public final void wait () throws java.lang.InterruptedException
- public final void wait (long arg0,int arg1) throws java.lang.InterruptedException
- public final native void wait (long arg0) throws java.lang.InterruptedException
复制代码
|