[Learn Java for beginners] reflection learning notes and java learning notes

Source: Internet
Author: User

[Learn Java for beginners] reflection learning notes and java learning notes

Example

@ SuppressWarnings ("unused") public class Person {public String Name; private int Age; public Gender; private static String Species = "human"; public Person () {Name = "Alias name"; Age =-1;} public Person (String Name) {name = name;} private Person (String Name, int age) {name = Name; age = age;} private Person (Gender g) {Gender = g;} public void Run () {System. out. println (Name + "Run! ");} Public void Attack () {System. out. println (Name +! ");} Public void Attack (String name) {System. out. println (Name +" "+ name + "! ");} Private void Eat (String food) {System. out. println (Name + "eat" + food);} public void Introduce () {System. out. println ("My Name is" + Name + ", I am" + Age + "years old. ");} Public static void PlayGame (String gameName) {System. out. println ("play" + gameName + "game");} public static void main (String [] args) {System. out. println ("main"); for (String s: args) System. out. println (s) ;}} enum Gender {Male, Female}

No-argument constructor of the reflection class:

@ Test // The non-argument constructor of the reflection Class public void constructor1 () throws Exception {Class clazz = Class. forName ("pro. shaowei. reflect. person "); Constructor c = clazz. getConstructor (); Person p = (Person) c. newInstance (); Person p1 = (Person) clazz. newInstance (); p. introduce (); p. run (); p1.Introduce (); p1.Run ();}

The constructor of the reflection class has the following parameters:

@ Test // The constructor2 () throws Exception {Class clazz = Class. forName ("pro. shaowei. reflect. person "); Constructor c = clazz. getConstructor (String. class); Person p = (Person) c. newInstance ("James"); p. introduce (); p. run ();}

Private constructor of the reflection class:

@ Test // private constructor of the reflection Class public void constructor3 () throws Exception {Class clazz = Class. forName ("pro. shaowei. reflect. person "); // when reflecting private constructors, you must use the getDeclaredConstructor method Constructor c = clazz. getDeclaredConstructor (String. class, int. class); c. setAccessible (true); // violent reflection Person p = (Person) c. newInstance ("James", 25); p. introduce (); p. run ();}

Public parameter-free method of the reflection class:

@ Test // public Non-argument public void method1 () throws Exception {Class clazz = Class. forName ("pro. shaowei. reflect. person "); Person p = (Person) clazz. newInstance (); Method method = clazz. getMethod ("Run"); method. invoke (p );}

Public Parameter Method of the reflection class:

@ Test // public parameter public void method2 () throws Exception {Class clazz = Class. forName ("pro. shaowei. reflect. person "); Person p = (Person) clazz. newInstance (); Method method = clazz. getMethod ("Attack", String. class); method. invoke (p, "Li Si ");}

The reflection class has the following private parameter methods:

@ Test // The Private parameter public void method3 () throws Exception {Class clazz = Class. forName ("pro. shaowei. reflect. person "); Person p = (Person) clazz. newInstance (); Method method = clazz. getDeclaredMethod ("Eat", String. class); method. setAccessible (true); method. invoke (p, "banana ");}

Static Parameter Method of the reflection class:

@ Test // static parametric public void method4 () throws Exception {Class clazz = Class. forName ("pro. shaowei. reflect. person "); Person p = (Person) clazz. newInstance (); Method method = clazz. getDeclaredMethod ("PlayGame", String. class); method. setAccessible (true); method. invoke (p, "mine clearance ");}

The main method of the reflection class:

@ Test // main method of the reflection Class public void method5 () throws Exception {Class clazz = Class. forName ("pro. shaowei. reflect. person "); Person p = (Person) clazz. newInstance (); Method method = clazz. getDeclaredMethod ("main", String []. class); method. setAccessible (true); method. invoke (p, (Object) new String [] {"1", "2 "});}

Public reflection fields:

@ Test // public reflection Class field public void field1 () throws Exception {Class clazz = Class. forName ("pro. shaowei. reflect. person "); Person p = (Person) clazz. newInstance (); Field field = clazz. getField ("Name"); System. out. println (field. get (p); field. set (p, "Wang Wu"); p. introduce ();}

Private reflection fields:

@ Test // public void field2 () throws Exception {Class clazz = Class. forName ("pro. shaowei. reflect. person "); Person p = (Person) clazz. newInstance (); Field field = clazz. getDeclaredField ("Age"); field. setAccessible (true); System. out. println (field. get (p); field. set (p, 7); p. introduce ();}

Private and static reflection fields:

@ Test // reflection Class private static field public void field3 () throws Exception {Class clazz = Class. forName ("pro. shaowei. reflect. person "); Person p = (Person) clazz. newInstance (); Field field = clazz. getDeclaredField ("Species"); field. setAccessible (true); System. out. println (field. get (p); field. set (p, "non-dead"); System. out. println (field. get (p ));}

Related Article

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.