一、什么是反射

java reflaction in action中的解释:反射是运行中的程序检查自己和软件运行环境的能力,它可以根据它发现的进行改变。通俗的讲就是反射可以在运行时根据指定的类名获得类的信息 个人理解:就是我们对于创建对象我们除了通过 new关键字创建外,还能通过什么创建呢?private的属属性真的不能获取吗?反射就能做到打破这些所谓的规则反射和new创建对象谁的效率高? new

二、通过类对象调用newinstance()方法,适用于无参构造方法

2.1 类名.class

public class main {
 
    public static void main(string[] args) throws illegalaccessexception, instantiationexception {
        class<person> clazz = person.class;
        person person = clazz.newinstance();
        system.out.println(person instanceof person); // true
    }
}
 
class person {
 
    private integer age;
 
    private string name;
 
    public person() {
    }
}

2.2 class.forname

public class main {
 
    public static void main(string[] args) throws illegalaccessexception, instantiationexception, classnotfoundexception {
        class<?> clazz = class.forname("com.best.test.person");
        person person = (person) clazz.newinstance();
        system.out.println(person instanceof person); // true
    }
}
 
class person {
 
    private integer age;
 
    private string name;
 
    public person() {
    }
}

2.3 对象名.getclass

public class main {
 
    public static void main(string[] args) throws illegalaccessexception, instantiationexception{
        person person = new person();
        class<? extends person> clazz = person.getclass();
        person person1 = clazz.newinstance();
        system.out.println(person1 instanceof person); // true
    }
}
 
class person {
 
    private integer age;
 
    private string name;
 
    public person() {
    }
}

三、getconstructor()和getdeclaredconstructor()

通过类对象的getconstructor()或getdeclaredconstructor()方法获得构造器(constructor)对象并调用其newinstance()方法创建对象,适用于无参和有参构造方法。

3.1 getconstructor()

public class main {
 
    public static void main(string[] args) throws illegalaccessexception, instantiationexception, nosuchmethodexception, invocationtargetexception {
        class<person> clazz = person.class;
        constructor<person> ctor = clazz.getconstructor(integer.class, string.class);
        person person = ctor.newinstance(26, "jak");
        system.out.println(person instanceof person); // true
    }
}
 
class person {
 
    private integer age;
 
    private string name;
 
    public person(integer age, string name) {
        this.age = age;
        this.name = name;
    }
}

3.2 getdeclaredconstructor()

public class main {
 
    public static void main(string[] args) throws illegalaccessexception, instantiationexception, nosuchmethodexception, invocationtargetexception {
        class<person> clazz = person.class;
        constructor<person> ctor = clazz.getdeclaredconstructor(string.class);
        person person = ctor.newinstance("jak");
        
        system.out.println(person instanceof person); // true
    }
}
 
class person {
 
    private integer age;
 
    private string name;
 
    public person(integer age, string name) {
        this.age = age;
        this.name = name;
    }
 
    public person(string name) {
        this.name = name;
    }
}

3.3 getconstructor()和getdeclaredconstructor()区别

getdeclaredconstructor(class<?>… parametertypes) 

这个方法会返回指定参数类型的所有构造器,包括public的和非public的,当然也包括private的。getdeclaredconstructors()的返回结果就没有参数类型的过滤了。

再来看getconstructor(class<?>… parametertypes)

这个方法返回的是上面那个方法返回结果的子集,只返回指定参数类型访问权限是public的构造器。getconstructors()的返回结果同样也没有参数类型的过滤。

到此这篇关于详解java反射创建对象的文章就介绍到这了,更多相关java反射创建对象内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!