Learn reflection with Teacher Wang (v): Generate and manipulate objects using reflection

Source: Internet
Author: User

Learn reflection with Teacher Wang (v): Using reflection to generate objects

Teacher: Wang Shaohua QQ Group No.:483773664

Learning content

Using reflection to generate objects



Template class

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M01/82/88/wKioL1dYDEqzGqT4AABe_nCwo-o114.png " data_ue_src= "E:\My knowledge\temp\1548d3d3-a5fe-4016-8825-df80481b0317.png" >

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M02/82/88/wKioL1dYDEqz5I1YAAAgOCOQNhA065.png " data_ue_src= "E:\My knowledge\temp\c40c778f-b317-4cf7-9bfd-f0ccc9deb11b.png" >

I. Object Creation (i.), non-reflective method

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M02/82/89/wKiom1dYC0HCIrfAAAALRo5ULME324.png " data_ue_src= "E:\My knowledge\temp\631de28c-a896-4052-b631-347837944a1a.png" >

(b), reflective mode

There are two ways to build an object by constructing a method, one is constructed by means of no parameter, and one is constructed by means of a parameter.

1. Non-parametric construction method 1.1 If no parameter is constructed by public

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M02/82/88/wKioL1dYDEvy25cdAAAj4WXY2pY461.png " data_ue_src= "E:\My knowledge\temp\489901f5-0064-4751-b4b8-3a8702acccaa.png" >

You can also use the newinstance () method of the class object to create instances of the class object's corresponding classes

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M00/82/89/wKiom1dYC0KTpi2SAAAo9Nqr6W8459.png " data_ue_src= "E:\My knowledge\temp\0bde742a-b945-47dd-8c16-3f3e67e44757.png" >

1.2 If the non-parametric construction method is privatized

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M00/82/89/wKiom1dYC0Kj-1ASAAA8sVEbXsw774.png " data_ue_src= "E:\My knowledge\temp\6b2fc1bc-57da-4410-bb3a-dd92ba4c9481.png" >

Conclusion

If a non-parametric construction access is privatized, it is important to construct the object using the constructor method, rather than using the newinstance () method of the class object to create an instance of the class object's counterpart. Because the newinstance () method of the class object is essentially using the public parameterless construction method in the construct class.

2 Creating an object using a parameterized construction method

The class object is first used to get the specified constructor object, and then the Newinstance () method of the constructor object is called to create the corresponding instance of the class object. In this way, you can choose to use the specified constructor of a class to create an instance.

The following three steps are required to create a Java object using the specified constructor

    1. Get the Class object

    2. Use the GetConstructor () method of the class object to hunt for the specified constructor

    3. Call Constructor's Nesinstance () method to create a Java object

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M00/82/88/wKioL1dYDEzBb__4AAA0rNeTdzA262.png " data_ue_src= "E:\My knowledge\temp\6515c84f-33ee-4f63-b12b-e2e780a93b7f.png" >

Second, practice

(a) demand

Write a method that generates a corresponding instance of the class based on the passed-in class name

(b) Reference code:
1234 publicT createObject(String className) throws Exception{        Class<T> tClass = (Class<T>) Class.forName(className);        returntClass.newInstance();    }
12345 public static void test3 () throws exception{          objectfactory<person> objectfactory = new objectfactory<> (); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; person person = objectfactory.createobject ( " chapter09_ 05.Person " &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; system.out.println (person); &NBSP;&NBSP;&NBSP;&NBSP; }




From for notes (Wiz)

Learn reflection with Teacher Wang (v): Generate and manipulate objects using reflection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.