reflex
Correlation coupling between multiple modules
1) Service layer
You can read the string content in the configuration file content, and you can parse the string content into corresponding classes through technology to create objects -- this technology is reflection
2) Dao layer - (connection) - MysqlImpl------Mysql
Provides an interface for database operations
concept
Parsing classes, the process of obtaining instance objects from bytecode objects - reflection (occurs at runtime)
Graphic interpretation:
Person.class - > method area (Person.class (bytecode object)) ------ reflection ----- > heap (New person() (instance object))
Class
The object created by the class representing the class is a concrete class (a concrete class is equivalent to a. Class file corresponding to a bytecode object)
1. Get bytecode object
1) By data type class to get the bytecode object
Class <String> c=String.class
2) Object getClass() returns the bytecode object
Class<String> c=(Class<String>"zuo".getclass
3) Pass the string content into the forName static method to return the bytecode object (ensure that there is no problem with the string content)
Class<String> c=(Class<String>)Class.forName("java.lang.String")
2. Get the instance object through the bytecode object
//Bytecode object Class<String> clz=String.class;
1) Bytecode objects call the newInstance parameterless method. By default, parameterless construction is called to return the instance object
String s=clz.newInstance();
2) First obtain the parameterized construction, then call the newInstance parameterized method through the object of the construction method class to assign a value to the parameterized construction, and finally return the instance object
Constructor<String> c=clz.getConstructor(String.class); String s=c.newInstance("zuo");
Method
The object created by the class representing the method is the concrete method
Field
The class creation object representing the attribute is a concrete attribute
Constructor
The class creation object representing the construction method is the concrete construction method
Package
The class creation object representing the package is the concrete package
shortcoming
1. Breaking the encapsulation principle
//Bytecode object Class<String> clz=String.class; //Gets the specified property (privatized property) Fileld f=clz.getDeclaredField("hash"); //Provide instance object String str="zuo"; //Brute force cracking (get privatization attribute) f.setAccessible(true); //Change the hash attribute value of the instance object //The first parameter represents the selected instance object, and the second parameter represents the new object property value passed in //You can assign a value to the privatization attribute f.set(str,21);//str.hashCode() returns 21 //Gets the value of the privatized property of the instance object System.out.println(f.get(str));//21
Class<String> clz = String.class; //Gets the specified method Method m = clz.getDeclaredMethod("charAt", int.class); //Provide instance object String str="asadfvqw"; //Select the specific instance object through the object of the Method class //Execution method: the first parameter represents the selected instance object, and the second parameter represents the actual parameter //Equivalent to -- str.charAt(3) System.out.println(m.invoke(str,3));;
//Get all implemented interfaces Class<String>[] cs = (Class<String>[]) clz.getInterfaces(); //The package name + class name is returned clz.getName() // Returns the package name and related version information clz.getPackage() //Class name clz.getSuperclass() //Judge whether the type of bytecode object is the basic data type int.class.isPrimitive()
2. Skip type detection for generics
1) Generic erasure occurs at compile time and defines the data type of the storage element
2) Reflection skips the type detection of generic type at runtime. You can specify the type of storage element as Object type to store any type of data
List<String> list=new ArrayList<>(); Class<List> clz = (Class<List>) list.getClass(); Method m = clz.getDeclaredMethod("add", Object.class); m.setAccessible(true); m.invoke(list,true);
New features of jdk
jdk1.5 new features
1. Static import
1) You can import static methods and use them directly
2) The static import method is loaded first.
2. Variable parameters
1) Expression: parameter type
2) You can accept any number of parameter values (agree with the parameter type)
3) The bottom layer is implemented according to the array, and each element of the array receives the corresponding received parameter value
4) It can only appear on the far right of the parameter list
3. Enumeration
1) List the values one by one
2) Enumeration constants should be on one line and on the first line
3) You can define properties and common methods
4) A defined constructor can only be private
5) You can define abstract methods (ensure that enumeration constants need to override abstract methods)
6) From jdk1 5. The expression starting switch supports enumeration types
jdk1.8 new features
1) Interface supports defining entity methods (methods that can be modified with default or static)
public class LambdaDemo { public static void main(String[] args) { //Creating an implementation class object can call entity methods in the interface /* CalcImpl c=new CalcImpl(); System.out.println(c.sum(1,2)); //Interface name Call static entity method System.out.println(Calc.jc(3,4));*/ //Anonymous inner classes override abstract methods by default, and objects that create anonymous inner classes call overridden methods /* System.out.println(new Calc() { @Override public int max(int m, int n) { return m>n?m:n; } }.max(2,3));*/ //Lambda expression //(parameter list) - > {override method body} //Calc c=(int m,int n)->{return m>n?m:n;}; //The parameter type can be derived from the previous, so it can be omitted. If the method body has only one sentence, return and {} can be omitted //Ensure that only one abstract method in the interface can be used Calc c=(m,n)->m>n?m:n; System.out.println(c.max(3,4)); } } //Defines the interface that represents the calculator //Functional interface (only one abstract method) - functional programming @FunctionalInterface interface Calc{ //Size (abstract method) int max(int m,int n); //Entity method decorated with default public default int sum(int x,int y){ return x+y; } //Entity method modified with static public static int jc(int x,int y){ return x*y; } } //Implementation class class CalcImpl implements Calc{ @Override public int max(int m, int n) { return m>n?m:n; } }
① Functional interface (only one abstract method) --- functional programming
@FunctionalInterface
② In the main method, creating an implementation class object can call the entity method in the interface
eg: CalcImpl c=new CalcImpl();
③ Interface name Call static entity method
④ Anonymous inner classes override abstract methods by default, and objects that create anonymous inner classes call overridden methods
2) Lambda expressions implement functional interface classes by default and override abstract methods
① (parameter list) - > {override method body}
eg: Calc c=(int m,int n)->{return m>n?m:n;};
② The parameter type can be derived from the previous, so it can be omitted. If the method body has only one sentence, return and {} can be omitted
Ensure that only one abstract method in the interface can be used
eg: Calc c=(m,n)->m>n?m:n;
c.max(3,4);
3) Stream - stream structure
Streaming operation collection element
Provide a large number of functional interfaces (Lambda expressions can be used)
public class StreamDemo { public static void main(String[] args) { List<String > list=new ArrayList<>(); list.add("zuo"); list.add("zai"); list.add("yu"); list.add("zi"); /* //Enhanced for loop for(String s:list){ if(s.startsWith("z")){ System.out.println(s); } }*/ //Flow structure Stream<String> st=list.stream(); /* //filter st.filter(new Predicate<String>() { @Override public boolean test(String s) { return s.startsWith("z"); } }).forEach(new Consumer<String>() { @Override public void accept(String s) { System.out.println(s); } });*/ st.filter(s1->s1.startsWith("z")).sorted((o1,o2)->o1.compareTo(o2)).forEach(s2-> System.out.println(s2)); } }