Java Basic Learning Note "12" Reflect reflex (2)

Source: Internet
Author: User

  1. 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);
  2. 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);//ERRORm.invoke(null,(Object)new int[]{1,2,3});//YESm.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; TD class= "Code" >
    1
  3. 12 public static <t extends Number> list<t> aslist (T ... a) { } method m = Clz.getmethod ( class
  4. (2) return: Use the parameter args to assign the result of the method represented by the object on obj  
  5. 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
  6. 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 voidset(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
  7. 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.
  8. 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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.