- using reflection to create objects
(1) public T newinstance (): You can only create objects that are not private, no parameter constructs, like the new Class ()
12 |
Class<Teacher> clz = Teacher. class ; Teacher t2 = clz.newInstance(); |
(2) first get a constructor, call a method in the constructor, T newinstance (Object ... initargs), Initargs represents the constructor's required actual parameters
123 |
Constructor<Teacher> con = clz.getDeclaredConstructor(String. class ); con.setAccessible( true ); //假如访问的是私有的就要把setAccsessable设置为true,因为是有成员不能直接访问 Teacher t = con.newInstance( "will" ); |
Before accessing a private member: set it to be accessible, AccessibleObject: Represents the accessible object, is the parent of the contructor,method,filed;
method: Void Setaccessible (Boolean flag): used to set whether accessible, to set accessible: Obj.setaccessible (true);
- Reflection Access executes the obtained method:
123456 |
Class<InvokeMethodByVarArgsDemo> clz =InvokeMethodByVarArgsDemo.
class
;
Method m = clz.getMethod(
"show"
,
int
[].
class
);
//调用public static void show(int ...is)
m.invoke(
null
,
new int
[]{
1
,
2
,
3
});
//YES
//m.invoke(null,1,2,3);//ERROR
m.invoke(
null
,(Object)
new int
[]{
1
,
2
,
3
});
//YES
m.invoke(
null
,
new Object[]{
new int
[]{
1
,
2
,
3
}});
//YES 访问静态方法不需要实例所以第一个参数可以为null
|
public
object Invoke (Object obj,object . args)
new
object[]{pass the actual parameters}; General practice;
- 12
public
static
<t
extends
Number> list<t> aslist (T ... a) { }
method m = Clz.getmethod (
class
- (2) return: Use the parameter args to assign the result of the method represented by the object on obj
- 123
object ret=
new
object[]{
1
2
3
}
array.getlength (ret)
//gets the length of the array
array.get (ret, i)
//gets the value of the first I in the array ret
- use reflection to get the field and set the value:
1234 |
Class<Cat> clz = Cat. class ; //使用反射得到Car类了的字段,并赋值,和取值 Field[] fs = clz.getDeclaredFields(); Field f = clz.getDeclaredField( "name" ); f.getName() //取出字段的值 |
set the value of the field:
1 |
void 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.
parameter: Obj-the object to which the underlying field belongs (instance object) value-The new value of the field of the obj being modified
123 |
cat C = clz.newinstance (); F.setaccessible ( true ); //first set to access f.set (c, "Lucy" ); //setting instance C fields The value of name is Lucy |
- tbody> 12345
class<?> GetType () //returns a class object that identifies the claim type of the field represented by this Field object.
type Getgenerictype ()
//returns a Type object that represents this Field The Declaration type of the field that the object represents.
parameterizedtype is a type subclass that represents a parameterized type, that is, a generic type
type[] Getactualtypearguments ()
//returns an array of type objects that represent the actual type parameters of this type.
type Getrawtype ()
//returns a type object that represents a class or interface that declares this type.
-
123 |
Type t = this .getClass().getGenericSuperclass(); ParameterizedType pt = (ParameterizedType) t; clz = (Class<T>) pt.getActualTypeArguments()[ 0 ]; //获取的是数组,强转为指定的类型 |
An instance of this represented in the constructor of a generic parent class when the subclasses of the class are instantiated in a generic type .
return value return clz.newinstance (); represents an instance of T
Java Basic Learning Note "12" Reflect reflex (2)