Re-stepping on Java Road _day27 (reflection, schema design, jdk new features)

Source: Internet
Author: User

1: Reflection (Understanding)
(1) class loading and ClassLoader
(2) Reflection:
Use member variables, construct methods, member methods through bytecode file objects
(3) Use of reflection
A: Get the construction method through reflection and use
B: Get member variables by reflection and use
C: Gets the member method by reflection and uses the
(4) Reflection case
A: Run the contents of the configuration file by reflection
B: Cross-check through reflection
C: Assign a value to the specified value by reflecting any property of an arbitrary object
(5) Dynamic agent

2: Design mode
(1) Decorative design mode
BufferedReader br = new BufferedReader (new InputStreamReader (system.in));

Scanner sc = new Scanner (system.in);
(2) Template design mode

3:JDK new Features
(1) JDK5 (Master)
Packing and unpacking
Generic type
Enhanced for
Static Import
Variable parameters
Enumeration
(2) JDK6 (Learn)
(3) JDK7 (understanding)
The representation of the binary
Separating data with _
The switch statement uses a string
Generic inference (diamond-shaped generics)
Use of multiple catch
Automatic release of resource usage
(4) JDK8 (Learn)
can go to the Internet to learn information

--------------------------------------------------------------------------------------------------------------- -------------------------------

Loading of classes
When a program wants to use a class, if the class has not yet been loaded into memory, the system initializes the class by loading, connecting, and initializing three steps.
Load
This means reading the class file into memory and creating a class object for it.
When any class is used, the system creates a class object.
Connection
Verify that you have the correct internal structure and that it is consistent with other classes
Prepare to allocate memory for static members of the class and set default initialization values
Resolve to replace a symbolic reference in a class's binary data with a direct reference
Initialization is the initial step we've talked about before.


Class initialization time
To create an instance of a class
Access static variables for a class, or assign a value to a static variable
To invoke a static method of a class
Use reflection to force the creation of a Java.lang.Class object for a class or interface
Initializes a subclass of a class
To run a main class directly using the Java.exe command


Class Loader
Responsible for loading the. class file into the inside and generating the corresponding class object for it.
Although we do not need to care about the class loading mechanism, we understand this mechanism and we can better understand the operation of the program.
The composition of the class loader
Bootstrap ClassLoader Root class Loader
Extension ClassLoader Extension Class loader
Sysetm ClassLoader System Class Loader

Reflection
The Java reflection mechanism is in the running state, for any class, can know all the properties and methods of this class, for any one object, can call any of its methods and properties; This dynamically acquired information and the ability to dynamically invoke the object's methods are called the reflection mechanisms of the Java language.
To dissect a class, you must first obtain the bytecode file object for that class. The anatomy uses the method in class. So first get the object of class type corresponding to each bytecode file.

Gets the construction method through reflection and uses the
Get Construction method
GetConstructors
Getdeclaredconstructors
Creating objects
Newinstance ()
Con.newinstance ("Zhangsan", 20);

Gets the member variable by reflection and uses the
Get all Members
Getfields,getdeclaredfields
Get a single member
Getfield,getdeclaredfield
Modify the value of a member
Set (object Obj,object value) sets the field that is represented by this field object on the specified object variable to the specified new value.

Gets the member method by reflection and uses the
Get all methods
GetMethods
Getdeclaredmethods
Get a single method
GetMethod
Getdeclaredmethod
Violent visits
Method.setaccessible (TRUE);

Dynamic Agent
Dynamic Agent
Agent: Should have done their own things, but invited others to do, the person invited is the proxy object.
Example: Spring home to buy tickets for people
Dynamic Agent: This object that is generated during program operation
And in the process of running the object is actually the reflection we just reflected the content, so, dynamic agent is actually through reflection to generate a proxy

In Java, a proxy class and a Invocationhandler interface are provided under the Java.lang.reflect package, and dynamic proxy objects can be generated by using this class and interface. The proxy provided by the JDK can only be proxied for the interface. We have more powerful proxies Cglib
A method in the proxy class creates a dynamic proxy class object
public static Object newproxyinstance (ClassLoader loader,class<?>[] Interfaces,invocationhandler h)
The method that will eventually call Invocationhandler
Invocationhandler
Object Invoke (Object Proxy,method method,object[] args)

The three parameters of a method for creating a dynamic proxy object in a proxy class;
A ClassLoader object that defines which ClassLoader object to load on the generated proxy object
An array of interface objects, which means that I'm going to provide a set of interfaces to the object I need to proxy, and if I provide a set of interfaces to it, then the proxy object declares that the interface is implemented (polymorphic) so that I can invoke the methods in this set of interfaces.
The Invocationhandler object, which indicates which Invocationhandler object the dynamic proxy object will be associated with when calling the method.

Each dynamic proxy class must implement the Invocationhandler interface, and each instance of the proxy class is associated with a handler, and when we call a method through the proxy object, The invocation of this method is forwarded to the Invoke method of this interface by Invocationhandler.

The three parameters of the Invoke method in the Invocationhandler interface:
Proxy: Represents the Dynamic proxy object
Method: Represents the methods being executed
Args: Represents the argument passed in when the target method is called

Proxy.newproxyinstance
The proxy object created is an object that is dynamically generated when the JVM is run, and it is not our invocationhandler type.
Nor is it the type of interface that we define, but the one in which the run is dynamically generated, and the name is in this form,
Start with $, Proxy is medium, and the last number represents the label of the object.
System.out.println (U.getclass (). GetName ());


Enumeration
Enumeration overview
is to list the value of the variable one by one, and the value of the variable is limited to the range of values enumerated. Example: only 7 days a week, only 12 months a year.
Recall a singleton design pattern: A singleton class is a class with only one instance
So many examples are a class with multiple instances, but not an infinite number of instances, but a finite number of instances. This can be an enumeration class.

The format is: enumeration class with only enumerated items
Public enum Enum class name {
Enumeration Item 1, enumeration item 2, enumeration item 3 ...;
}

Precautions
Defining enum classes to use the keyword enum
All enum classes are subclasses of enum
The first line of an enumeration class must be an enumeration item, and the semicolon after the last enumeration item can be omitted, but the semicolon cannot be omitted if there is something else in the enumeration class. It is recommended not to omit
An enumeration class can have a constructor, but it must be private, and it defaults to private. The use of enumeration items is special: enumeration ("");
An enumeration class can also have an abstract method, but an enumeration item must override the method
Enumeration used in switch statements, JDK7 new features

A few common methods in enumeration classes
int CompareTo (E o)
String name ()
int ordinal ()
String toString ()
<T> T valueOf (class<t> type,string name) VALUES ()
Although this method is not found in the JDK document, each enumeration class has that method, and it is convenient to traverse all enumerated values of the enumeration class

Template design mode
Overview of template Design Patterns
The template method pattern is defined as the skeleton of an algorithm, and the specific algorithm is deferred to the subclass to realize
Advantages
Using the template method mode, in defining the skeleton of the algorithm, we can implement the concrete algorithm flexibly, and satisfy the user's flexible and changeable demands.
Disadvantages
If the algorithm skeleton is modified, you need to modify the abstract class

Decorative design pattern
Overview of Decorative Design Patterns
Decorating mode is the use of an instance of a subclass of a decorated class, where the client gives an instance of the subclass to the adornment class. is an alternative to inheritance
Advantages
Using the adornment mode, you can provide more flexible extension objects than inheritance, it can dynamically add the function of objects, and can be arbitrarily combined with these features
Disadvantages
Because they can be assembled at will, there may be some unreasonable logic.

Re-stepping on Java Road _day27 (reflection, schema design, jdk new features)

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.