I. Interface
All methods in the interface default to public static final and need not be written again.
Constants can be defined in interfaces, but they cannot contain instance domains. Before java SE8, methods could not be implemented in interfaces.
A class can implement multiple methods implements interface 1, interface 2... Multiple interfaces are separated by ",".
Cloning: To achieve cloning, the Cloneable interface is implemented, the clone method is redefined, and the public modifier is specified.
The default clone is shallow clone. To achieve deep clone, you need to rewrite clone yourself.
All array types have a public clone method.
Reflection
Concept: Programs that can analyze class capabilities are called reflections and can dynamically manipulate java code programs.
When java runs, the system always maintains a type identifier called runtime for all objects. The class that holds this information is called Class
Get the class method: object. getClass(), class. forName (object name), type.
A Class actually represents a type, which is not necessarily a class, but is actually a generic type.
The virtual machine manages a Class object for each type. You can dynamically create an instance of a class through newInstance().
Filed, Method, Constructor all have a getName method that returns the corresponding name
There is a getModifiers method that returns the modifier usage, which can make use of isPublic in the Modifier class. It can also be printed out by toString.
Field class: The domain used to describe the 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 describing the type to which the domain belongs.
SetAccessible(true) shields access checks in the java language so that private properties can also be queried and set
get(obj) set()
6. Method and Constructor are similar to Field.
Get method pointer: Method M = obj. class. getMethod (method name)
Call method: String n = m.invoke(obj,... Parameter)
Note: If a static method is called, the first parameter is empty
A small example is given in the following appendix:
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+" "); } 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){ 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){ 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(); } } }