Java Learning--the 13th day of basic knowledge--notes

Source: Internet
Author: User
Tags modifiers

Today's content
Reflection
Beanutils class


Reflection
At run time, you can get and invoke members of a class (constructs, member variables, member methods, and so on), including private.


The premise of Reflection:
Gets the bytecode object of the class (class object)

How to get a bytecode object:
(1) object. GetClass ()
Like what:
Student s = new Student ();
Class clazz = S.getclass ();
(2) class name. class
Like what:
Class clazz = Student.class;
(3) Class.forName (String className): This method is most commonly used in three ways
Like what:
Class.forName ("com.itheima_01.Student");//The class name passed in the method must be a full-class name

Class
Construction Method:
Public-Modified
Constructor<t> getconstructor (class<?> ... parametertypes): Gets the (public decorated) construction method for the specified parameter
Constructor<?>[] GetConstructors (): Gets all the (public-modified) construction methods
All permission modifiers are decorated with the
Constructor<t> getdeclaredconstructor (class<?> ... parametertypes) gets (all permission modifiers decorated) The specified constructor method
Constructor<?>[] Getdeclaredconstructors () gets all the constructor methods (all permission modifiers are decorated)

Member variables:
Public-Modified
Field GetField (string name): Gets the specified (public decorated) member variable (the specified String) of the field type Object
Field[] GetFields (): Gets all the (public decorated) member variables of the field type Object array
The amount of all permission modifier adornments
Field Getdeclaredfield (String name): Gets the specified member variable (all permission modifiers decorated), including public, protected, default, and private
Field[] Getdeclaredfields (): Gets all the member variables (all permission modifiers decorated)

Method
Public-Modified
Method GetMethod (String name, Class<?> ... parametertypes): Gets the method type object for the specified (public decorated) methods
Method[] GetMethods (): Gets the array of method type objects for all (public-decorated) methods
All permission modifiers are decorated with the
Method Getdeclaredmethod (String name, Class<?> ... parametertypes): Gets (all permission modifiers decorated) The method type object that specifies all methods
Method[] Getdeclaredmethods (): Gets an array of method type objects (all permission modifiers are decorated) for all methods


Construction Method (Constructor)
Constructor class
T newinstance (Object ... initargs): Call the constructor method to create the object, if not given a parameter, call the null argument construct, if the given argument is called the argument construct

Use (Create Object)
Non-parametric construction
Method One: Call Newinstance () in the constructor class
Steps:
(1) Get the class object
For example: Class clazz = Class.forName ("com.itheima_01.Student");
(2) Get the constructor object constructed by GetConstructor () in class
For example: Constructor c =clazz.getconstructor ();
(3) Create an object using the null parameter construct through the Newinstance () method in the constructor class
For example: Object obj = c.newinstance ();

Mode two: Call Newinstance () in class
Steps:
(1) Get the class object
For example: Class clazz = Class.forName ("com.itheima_01.Student");
(2) Create an object using the null parameter construct through the Newinstance () method in class
For example: Object obj = clazz.newinstance ();

The construction of a parameter
Way: By calling Newinstance (Object ... initargs) in the constructor class
Steps:
(1) Get the class object
For example: Class clazz = Class.forName ("com.itheima_01.Student");
(2) Get the constructor object of the specified argument construct by GetConstructor (CLASS&LT;?&GT; ... parametertypes) in class
For example: Constructor c = clazz.getconstructor (String.class,int.class);
(3) passing the actual arguments through the Newinstance (object ... Initargs) method in the constructor class creates an object through a parameter construct
For example: Object obj = c.newinstance ("Lisi", 30);

Member variable (Field)
Field class
void set (Object obj, Object value): Assigns a value to the member variable of the specified object, specifying a new value
Object get (Object obj): Gets the value of the member variable of the specified object
void Setaccessible (Boolean flag): Remove access, brute force access "method inherited by Parent class"

Use (Value and Assignment)
Member variable for public decoration
Steps:
(1) Get the class object
For example: Class clazz = Class.forName ("com.itheima_01.Student");
(2) Create an object
For example: Object stu = Clazz.newinstance ();
(3) Call the GetField (String name) method in class to get the field type object for the specified member variable
For example: Field f = Clazz.getfield ("Age");
(4) Call the Set (object obj, Object value) method in the field class to assign a value, or call the Get (object obj) method to take a value
For example: F.set (Stu, 28);
For example: Object age = F.get (stu);

Member variables that are not public decorated
Steps:
(1) Get the class object
For example: Class clazz = Class.forName ("com.itheima_01.Student");
(2) Create an object
For example: Object stu = Clazz.newinstance ();
(3) Call the Getdeclaredfield (String name) method in the field class to get the field type object for the specified member variable
For example: Field f = Clazz.getdeclaredfield ("name");
(4) Calling the Setaccessible (Boolean flag) method in the field class, parameter passing True
Example: F.setaccessible (TRUE);
(5) Call the Set (object obj, Object value) method in the field class to assign a value, or call the Get (object obj) method to take a value
For example: F.set (Stu, "John Doe");
For example: Object name = F.get (stu);


Member Methods (method)
Method class
Object Invoke (Object obj, Object ... args): invokes the specified method of the specified object, returns the return value of the specified method,
The Invoke method returns Null if the method object is represented in the lower level without a return value

Use (Invoke method)
Steps:
(1) Get the class object
For example: Class clazz = Class.forName ("com.itheima_01.Student");
(2) Create an object
For example: Object stu = Clazz.newinstance ();
(3) The method class object for the specified method is obtained through the GetMethod (String name, Class<?> ... parametertypes) methods in the class category
For example: Method m = Clazz.getmethod ("SetName", String.class);
(4) Call the Invoke (object obj, Object ... args) method in the methods class to pass the actual arguments of the specified object and method
For example: M.invoke (Stu, "Lisi");

Classroom Case One:
1. A class is known, defined as follows:
Package Com.itheima;
public class DemoClass {
public void Run () {
System.out.println ("Welcome to heima!");
}
}
(1) Write a configuration file in the properties format, and configure the full name of the class.
(2) Write a program, read the properties configuration file, get the full name of the class and load the class,
(3) Running the Run method in a reflective manner.

Classroom Case Two:
2.arraylist<integer> list = new arraylist<integer> ();
This generic type is an integer that holds a string object in the ArrayList


Beanutils class
JavaBean
Role: Used to encapsulate data

Specification:
(1) class to be modified with public
(2) member variables are modified with private
(3) Getters and setters with public modifications
(4) provide null parameter construction
(5) Implementation of the serialization Interface (Serializable), generating serial numbers


Beanutils
Used to simplify our operations on JavaBean, which is a tool class for javabean operations.
It is a third-party class library (not provided by JDK or written by ourselves) and must be imported when using the jar package
is a component provided by Apache Software Alliance
www.apache.org

Beanutils class
Common methods
/**
* Assign a value to the specified member variable, acting like the Setxxx () method
*
* Object Bean:javabean objects, specifying which object
* String Name: Names of member variables
* Object Value: The value assigned to the member variable
*/
Static Voidsetproperty (object bean, String name, object value)

/**
* Gets the value of the specified member variable, acting like the GetXXX () method
*
* Object Bean:javabean objects, specifying which object
* String Name: Names of member variables
*/
static string GetProperty (Object bean, String name)

/**
* Assign a value to a specified member variable, one time can assign a value to multiple member variables of an object at a time
*
* Object Bean:javabean objects, specifying which object
* Map Properties: Encapsulates the name of the member variable and its corresponding value into the Map collection, and can assign values to these multiple member variables at once
*/
Static voidpopulate (Object Bean, Map properties)

Note: The SetProperty and GetProperty methods of beanutils are not directly manipulating member variables, but rather the get and set methods associated with the member variable names

Today's content Highlights
Mastering reflection gets the construction method and uses
Mastering reflection gets member variables and uses
Mastering reflection gets the member method and uses
Mastering the use of variable parameters
Two classroom cases related to reflection
Know the 5 specifications of JavaBean
Mastering the three methods of Beanutils class use
Mastering the code of the three methods of the Mybeanutils class simulation

Java Learning--the 13th day of basic knowledge--notes

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.