First, the interface
-
All methods in the interface default public static final, no need to write again.
-
A constant can be defined in an interface, but cannot contain an instance domain. The method cannot be implemented in the interface until Java SE8.
-
A class can implement multiple methods implements interface 1, interface 2 ... Multiple interfaces are separated by ",".
-
Cloning: To implement a clone first implement the Cloneable interface, redefine the Clone method, and specify the public modifier.
The default clone is a shallow clone, and you need to rewrite clone yourself to implement a deep clone.
-
All array types have a clone method of public.
Second, reflection
-
Concept: A program capable of analyzing class capabilities is called reflection and can dynamically manipulate Java code programs.
-
At the Java runtime, the system always maintains a type identity called Runtime for all objects. The class that holds this information is called class
-
Gets the Class method: object. GetClass (), Class.forName (object name), type. Class
A class actually represents a type, which is not necessarily a class, it is actually a generic type.
A virtual machine manages a class object for each type. You can dynamically create an instance of a class from newinstance ().
-
Filed, method, constructor these three classes have a GetName method that returns the corresponding name
There is a getmodifiers method that returns the modifier usage, which can be judged using IsPublic in the Modifier class. You can also print out the ToString.
-
Field class: The domain used to describe a class
Getfileds () returns an array of Field objects, including the public domain of this class or superclass
getfiled (name) returns the corresponding domain by name
Getdeclaredfields () returns an array of Field objects, including all fields of this class
GetType () Returns a class object that describes the type of domain to which it belongs
Setaccessible (true) blocks access checks in the Java language so that private properties can also be queried and set
Get (obj) set ()
6. Method, constructor is similar to field.
Get method pointer: Method M = Obj.class.getMethod (methods name)
Call Method: String n = m.invoke (obj,... Parameters
Note: If the static method is called, the first argument is empty
The following appendix is a small example:
import java.lang.reflect.constructor;import java.lang.reflect.field;import java.lang.reflect.method;import java.lang.reflect.modifier;import java.util.scanner;import Java.lang.double;/** * created by cnslp on 2017/4/22. */public class reflectiontest { public static void printfiled (Class c) { field[] fields = c.getdeclaredfields (); for (Field f : fields) { string modifier = modifier.tostring ( F.getmodifiers ()); if ( Modifier.length () >0) { system.out.print (modifier+ " ");   &NBsp; } class type = f.gettype (); system.out.print (Type.getname () + " "); system.out.print (F.getname ()); system.out.println (";"); } System.out.println (); } public static void Printconstruct (class c) { constructor[] constructors = c.getconstructors (); for (Constructor constructor : constructors) {          &NBSp; string modifier = modifier.tostring (Constructor.getmodifiers ()); if (Modifier.length () >0) { system.out.print (" " + modifier+ " "); } string name = constructor.getname (); system.out.print (name+ "("); Class[] paramTypes = Constructor.getparametertypes (); int j=0; for (CLASS CC : paramtypes) {    &Nbsp; if (j>0) System.out.print (","); system.out.print (Cc.getname ()); j++; } system.out.println (");"); } System.out.println (""); } public static void Printmethod (class c) { method[] methods = C.getmethods (); for (method method : methods) { string modifier = modifier.tostring (Method.getmodifiers ()); if (Modifier.length () >0) { system.out.print (" " +modifier+ " "); } string name = method.getname (); system.out.print (name+ "("); Class[] paramTypes = Method.getparametertypes (); int j= 0; for (class cc : paramtypes) { if (j>0) system.out.print ("," ); System.out.print (Cc.getname ()); j++; } system.out.println (");"); } } public Static void main (String[] args) { string name; if (args.length>0) name = args[0]; else { scanner in = new scanner (System.in); system.out.println ("Please input a class name "); name = In.nextline (); } try { class c = class.forname (name); class Father = c.getsuperclass (); string modefiers = Modifier.tostring (C.getmodifiers ()); System.out.println (modefiers+ " class " +c.getname () + " extends " +father.getname () + "{"); printfiled (c); printconstruct (c); printmethod (c); } catch (classnotfoundexception e) { e.printstacktrace (); } }}
This article is from the "Red Xiao" blog, please be sure to keep this source http://cnslp.blog.51cto.com/11387491/1918467
Java Fundamentals-Interface and reflection summary