reflection
Reflection ( RTTI )
Dynamically load a specified class and get all the content in that class. Moreover, the bytecode file is encapsulated into an object, and the contents of the bytecode file are encapsulated into an object, which is convenient for manipulating these members. Simply put: reflection techniques can dissect a class.
The benefits of reflection: greatly enhance the scalability of the program.
Basic steps of reflection:
1. Obtaining the Class object is to obtain the bytecode file object of the specified name.
2. Instantiate the object and obtain the properties, methods or constructors of the class.
3. Access properties, call methods, and call constructors to create objects.
( run-time type identification )包:java.lang.reflection
Class is a class used to describe type information, and each type has one and only one Class object corresponding to it one-to-one. The Class object is the source of reflection.
// How to get the Class object :
//1 . Call the getClass method inherited from the Object class through the object (reference)
//2 , call the .class attribute through the class name
//3 , commonly used through the static method forName () in the Class class
// The role of the Class object: 1. Analyze the structure of the class 2. Implement reflection
// Three classes under the reflect package: Constructor Field Method
There are three ways to get this Class object:
The method in the Class class used, the static forName method .
Most scalable
// 1. Obtained according to the given class name for class loading
String classname = "cn.itcast.reflect.Person";// From the configuration file
Class clazz = Class.forName (classname);// This object represents Person.class
// 2. If you get the object, you don't know what type it is. The type used to get the object
Object obj = new Person();
Class clazz1 = obj.getClass () ;// Get the specific type of the object
// 3. If the Class object of a class is explicitly obtained, it is mainly used for passing parameters
Class clazz2 = Person.class;
The Class class itself does not provide a constructor, so you cannot use the new operator and constructor to explicitly create a Class object.
Class class is used to describe the type of information
Class: The description of the class or the description file of the class
The objects of the Class class correspond to the classes one-to-one
The role of the Class object: 1. Analyze the structure of the class 2. Implement reflection
Class is the source of reflection
There are three ways to obtain a Class object : 1. The getClass method inherited from the Object class 2. Class name.class
Student obj = new Student();
Class c1 = obj.getClass();
Class c2 = Student.class;*/
Class c3 = Class.forName("com.aowin.rtti.Student");//包名+类名
System.out .println ( c3.getName ());// Print result com.aowin.rtti.Student
System.out.println ( c3.getSimpleName ()); print result Student
//create object
/*Object obj = c3.newInstance();//There must be a parameterless constructor in the Student class
call method after object is created
Student stu = (Student)obj;
stu.eat();*/
//get constructor
// Constructor to get private permission with no parameters
Constructor with =c3.getDeclaredConstructor();
con.setAccessible (true);
Object obj=con.newInstance();
Get a constructor with parameters
Constructor con = c3.getConstructor(int.class);//Get the constructor of the specified public permission
Constructor con = c3.getDeclaredConstructor(int.class);//Constructor method to get all the specified permissions
con.setAccessible(true);//If the constructor of private permission is obtained, it should be set to be changeable
Object obj = con.newInstance(1000);
// get properties
Field field = c3.getField("age");//Get the attributes of the specified public permissions, including the attributes inherited from the parent class
Field field = c3.getDeclaredField("age");//Get the attribute of any specified authority
Object obj = c3.newInstance();
field.setAccessible(true);
field.set(obj, 20);//The first parameter is the specified object and the second parameter is the value
Object temp = field.get(obj);//Get the value of the attribute of the specified object and return the value of the field represented in the object obj
System.out.println(temp);*/
//Get the execution method of method reflection
//Method method = c3.getMethod("eat",int.class);
/*Method method = c3.getDeclaredMethod("eat",int.class);
Object obj = c3.newInstance();
method.setAccessible(true);
method.invoke(obj,100);*///Execute the method represented by the method object
get method without parameters
Method me=c3.getMethod("eat");
Object obj=c3.newInstance();
me.setAccessible(true);
me.invoke(obj);
Constructor[] cons = c3.getConstructors();//All constructors that get public permissions
Constructor[] cons = c3.getDeclaredConstructors();//All constructors with arbitrary permissions
for(Constructor temp:cons){//for each循环
System.out.println(temp);
}
Field[] fs = c3.getFields();//Include the properties inherited from the parent class
Field[] fs = c3.getDeclaredFields();//Does not include inherited
for(Field f:fs){
System.out.println(f);
}
Method[] ms = c3.getMethods();//All methods of public permissions, including inherited ones
Method[] ms = c3.getDeclaredMethods();//All methods to obtain arbitrary permissions, excluding inherited ones
for(Method m:ms){
System.out.println(m);
}
str.replace("","");Remove all spaces, including the beginning, the end and the middle
trim() removes leading and trailing spaces
1. Determine whether the type is the same
Traditional transformation : .instanceof if(x instanceof Dog) //This operator uses the old-fashioned keyword. It requires that x must be a reference, so the basic type variable name cannot be used, and the latter Dog must also be Class, so it still cannot be used. Use basic types. At the same time, there is a disadvantage that Dog itself must be a Class name, and cannot be replaced with some kind of variable at all. Compared with the following method, it lacks flexibility.
2. Operate the Class object:
Class.isInstance(obj)//For example
Class c=Dog.class;
if(c.isInstance(x))....obj is also required to be object
a. The keyword instanceof;//Determine whether the object implements the specified interface or inherits the specified class
b. The keyword isInstance
Format: <object instanceof type>, to determine whether an object belongs to the specified type.
student instanceof Person = true;//student inherits the person class
Object obj = new Student();
obj = new String();
if(obj instanceof Student){
System.out.println ("is the student type");
}
if(c3.isInstance(obj)){
System.out.println ("is the student type");
}
System.out.println(c3.getName());
System.out.println(c3.getSimpleName());
when
Object obj = new Student(); // is the same type
The print result is student type
is the student type
com.aowin.rtti.Student
Student
when obj = new String(); // not the same type
The result is com.aowin.rtti.Student
Student
There are three shift operators in java
<< : Left shift operator, num << 1, equivalent to multiplying num by 2
>> : right shift operator, num >> 1, equivalent to dividing num by 2
>>> : unsigned right shift, ignoring the sign bit, and padded with 0
Unsigned right shift, ignoring the sign bit, padded with 0
value >>> num -- num specifies the number of bits to shift the value value by.
The rules for unsigned right shift only remember one point: sign bit extension is ignored, 0 complements the highest bit Unsigned right shift operator >>> only makes sense for 32 -bit and 64 -bit values