Java反射中的class.getConstructors()返回的数组中的顺序问题

我最近有用到反射的知识,然后总是出错,发现还是细节掌握的不够牢固,这里要好好感谢一些一篇博文  一个例子让你了解Java反射机制 ,我就是看了这个所以才写这篇博文的。

那里面的代码我直接复制后运行发现总是报错,而且每次都是Class.forName报的异常,这不到这个类,博主用的是一个内部类,但是我复制了限定名之后粘贴进去也无济于事,然后我就只有把Person写成了一个单独的类而非是一个内部类,这样就解决了,里面博主没有说class.getConstructors()返回的数组中的函数是怎么排序的,他是直接用的,然我我对这个很好奇,就尝试了一下.


下面是我的person类的代码:

package inferfaceTest;

class Person {
	private int age;
	private String name;

	public Person() {

	}

	public Person(int age, String name) {
		this.age = age;
		this.name = name;
	}

	public Person(int aaa,int bbb) {

	}
	
	public Person(double aaa,int bbb) {

	}

	public Person(String aaa,String bbb) {

	}
	
	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

注意,这里我谢了5个构造方法

其他地方的代码还和那个博主一样,只是小小修改了一下。


package inferfaceTest;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 测试一下反射
 * @author Lenovo
 *
 */
public class Reflex {

	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

		//Demo1.  通过Java反射机制得到类的包名和类名  
        Demo1();  
        System.out.println("===============================================");  
          
        //Demo2.  验证所有的类都是Class类的实例对象  
        Demo2();  
        System.out.println("===============================================");  
          
        //Demo3.  通过Java反射机制,用Class 创建类对象[这也就是反射存在的意义所在],无参构造  
        Demo3();  
        System.out.println("===============================================");  
		
      //Demo4:  通过Java反射机制得到一个类的构造函数,并实现构造带参实例对象  
        Demo4();  
        System.out.println("===============================================");  
        
	}
	
	
	
	 public static void Demo1()  
	    {  
	        Person person = new Person();  
	        System.out.println("Demo1: 包名: " + person.getClass().getPackage().getName() + ","   
	                + "完整类名: " + person.getClass().getName());  
	    }  
	      
	    /** 
	     * Demo2: 验证所有的类都是Class类的实例对象 
	     * @throws ClassNotFoundException  
	     */  
	    public static void Demo2() throws ClassNotFoundException  
	    {  
	        //定义两个类型都未知的Class , 设置初值为null, 看看如何给它们赋值成Person类  
	        Class<?> class1 = null;  
	        Class<?> class2 = null;  
	          
	        //写法1, 可能抛出 ClassNotFoundException [多用这个写法]  
	      
	        class1 = Class.forName("inferfaceTest.Person");
	   
	        
	        System.out.println("Demo2:(写法1) 包名: " + class1.getPackage().getName() + ","   
	                + "完整类名: " + class1.getName());  
	          
	        //写法2  
	        class2 = Person.class;  
	        System.out.println("Demo2:(写法2) 包名: " + class2.getPackage().getName() + ","   
	                + "完整类名: " + class2.getName());  
	    }  
	      
	    /** 
	     * Demo3: 通过Java反射机制,用Class 创建类对象[这也就是反射存在的意义所在] 
	     * @throws ClassNotFoundException  
	     * @throws IllegalAccessException  
	     * @throws InstantiationException  
	     */  
	    public static void Demo3() throws ClassNotFoundException, InstantiationException, IllegalAccessException  
	    {  
	        Class<?> class1 = null;  
	        class1 = Class.forName("inferfaceTest.Person");  
	        //由于这里不能带参数,所以你要实例化的这个类Person,一定要有无参构造函数哈~  
	        Person person = (Person) class1.newInstance();  
	        person.setAge(20);  
	        person.setName("range");  
	        System.out.println("Demo3: " + person.getName() + " : " + person.getAge());  
	    }  
	    
	    
	    /** 
	     * Demo4: 通过Java反射机制得到一个类的构造函数,并实现创建带参实例对象 
	     * @throws ClassNotFoundException  
	     * @throws InvocationTargetException  
	     * @throws IllegalAccessException  
	     * @throws InstantiationException  
	     * @throws IllegalArgumentException  
	     */  
	    public static void Demo4() throws ClassNotFoundException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException  
	    {  
	        Class<?> class1 = null;  
	        Person person1 = null;  
	        Person person2 = null;  
	          
	        class1 = Class.forName("inferfaceTest.Person");  
	        //得到一系列构造函数集合  
	        Constructor<?>[] constructors = class1.getConstructors();  
	          
	        for(int i=0;i<constructors.length;i++){
	        	System.out.println("i= "+i+"  "+constructors[i].toGenericString());
	        }
	        
	        person2 = (Person) constructors[1].newInstance(20,"leeFeng");  
	        
	        person1 = (Person) constructors[0].newInstance();  
	        person1.setAge(30);  
	        person1.setName("leeFeng");  
	          
	          
	        System.out.println("Demo4: " + person1.getName() + " : " + person1.getAge()  
	                + "  ,   " + person2.getName() + " : " + person2.getAge()  
	                );  
	          
	    }  
	    
	    
	    
	    
	      
	
	    
	   
	 

}


然后看我运行结构


Demo1: 包名: inferfaceTest,完整类名: inferfaceTest.Person
===============================================
Demo2:(写法1) 包名: inferfaceTest,完整类名: inferfaceTest.Person
Demo2:(写法2) 包名: inferfaceTest,完整类名: inferfaceTest.Person
===============================================
Demo3: range : 20
===============================================
i= 0  public inferfaceTest.Person(java.lang.String,java.lang.String)
i= 1  public inferfaceTest.Person(double,int)
i= 2  public inferfaceTest.Person(int,int)
i= 3  public inferfaceTest.Person(int,java.lang.String)
i= 4  public inferfaceTest.Person()
Exception in thread "main" java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at inferfaceTest.Reflex.Demo4(Reflex.java:106)
at inferfaceTest.Reflex.main(Reflex.java:28)

这个就是运行结构,可以看出构造函数是倒序的,接下来我注释掉下面的3个构造方法,再次运行


Demo1: 包名: inferfaceTest,完整类名: inferfaceTest.Person
===============================================
Demo2:(写法1) 包名: inferfaceTest,完整类名: inferfaceTest.Person
Demo2:(写法2) 包名: inferfaceTest,完整类名: inferfaceTest.Person
===============================================
Demo3: range : 20
===============================================
i= 0  public inferfaceTest.Person()
i= 1  public inferfaceTest.Person(int,java.lang.String)
Demo4: leeFeng : 30  ,   leeFeng : 20
===============================================


正序的,这个地方真是防不胜防,容易出错。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值