Java Basic Learning Note "11" Reflect reflex (1)

Source: Internet
Author: User

  1. InputStreamReader converting a byte stream to a character stream
  2. In java1.7, we have added files,paths convection, the tool class of the path operation, and the inside is the operation of the input and output stream, etc.
  3. Syso can only prompt for quick code System.out.println ();
  4. There are three ways to create a class by reflection:
    (1) Use the Class property to get the class object
    12 Class<String> c = String.class;//String.class就表示JVM里的一份表示String类的字节码 Class<String> c2 = String.class;
    (2) Use Class.forName (fully qualified name of Class) static class<?> forname (String className)
    12 Class<String> clazz = (Class<String>) Class.forName("String");//ERROR Class<String> clazz = (Class<String>) Class.forName("java.lang.String");
    (3) object class: Class<?> GetClass () returns the run-time class of this Object.
    1234 List<String> list = newArrayList<String>();Class<?> listClz = list.getClass();//得到的是listClz的真正类型类 System.out.println(listClz);//java.util.ArrayList booleanb = listClz.isInstance(list);//等价于 list instanceof ArrayList
  5. some common methods of class:
    12345678910111213141516 Class<IM> clz = IM.class;clz.getPackage()//获取clz的包clz.getName()//类的全限定名称clz.getSuperclass()//获取直接父类Class<?>[] ins = clz.getInterfaces(); //获取所有的接口,接口可能多个所以数组接收,要操作就循环遍历Class<?>[] ins= clz.getClasses();//获得类public修饰的的内部类/接口String getSimpleName() //返回源代码中给出的底层类的简称。Class<? super T> getSuperclass() //返回表示此 Class 所表示的实体(类、接口、基本类型或 void)的超类的 Class。 Class<?>[] getInterfaces() //确定此对象所表示的类或接口实现的接口。int getModifiers() //返回此类或接口以整数编码的 Java 语言修饰符。Modifier:public static String toString(int mod) //返回描述指定修饰符中的访问修饰符标志的字符串 <U> Class<? extends U> asSubclass(Class<U> clazz) //强制转换该 Class 对象,以表示指定的 class 对象所表示的类的一个子类。boolean isInstance(Object obj) //判定指定的 Object 是否与此 Class 所表示的对象赋值兼容。对象  instanceof  类 int mod = clz.getModifiers(); //获取类的访问修饰符,获取到的为数字类型,要转成对应的类型就要使用相应的方法System.out.println(mod);//1表示public System.out.println(Modifier.toString(mod));
  6. Reflection Constructors:
    1234567 class<employee> CLZ = E Mployee. class //now gets the class object constructor<employee>[] cs = (constructor<employee>[]) clz.getconstructors (); //class constructor<employee> con=clz.getconstructor (String. class ,integer. class //gets the constructor specified by public, with several parameters using a few parameter types of bytecode constructor<t> getdeclaredconstructor (class<?> .... parametertypes) access not controlled by access rights clz.getdeclaredconstructors () //get all the constructor collections
  7. What array is used Type []. Class
  8. 12345 class<methoddemo> clz = Methoddemo. class method[] ms = Clz.getmethods (); //get all the public modifiers in the subclass and its parent class method m = Clz.getmethod ( class //gets the specified method of all public adornments in the subclass and its parent class, followed by variable arguments (type. Class) do the parameters clz.getdeclaredmethods (); //gets all the methods in its own class or a specified method, independent of access rights
  9. 1234 field[] fs = Clz.getfields (); //get all the public fields, including inheritance field f = Clz.getfield ( //the specified public, package width inheritance field[] fs = Clz.getdeclaredfields (); //get all the fields, only get inside the current class, and access rights independent of the f = Clz.getdeclaredfield ( );
  10. Declared generally the method with the word name in the reflection's use is private or protected type



Java Basic Learning Note "11" Reflect reflex (1)

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.