Java Fundamentals-Interface and reflection summary

Source: Internet
Author: User

First, the interface

    1. All methods in the interface default public static final, no need to write again.


    2. A constant can be defined in an interface, but cannot contain an instance domain. The method cannot be implemented in the interface until Java SE8.


    3. A class can implement multiple methods implements interface 1, interface 2 ... Multiple interfaces are separated by ",".


    4. Cloning: To implement a clone first implement the Cloneable interface, redefine the Clone method, and specify the public modifier.

      The default clone is a shallow clone, and you need to rewrite clone yourself to implement a deep clone.


    5. All array types have a clone method of public.


Second, reflection

    1. Concept: A program capable of analyzing class capabilities is called reflection and can dynamically manipulate Java code programs.


    2. At the Java runtime, the system always maintains a type identity called Runtime for all objects. The class that holds this information is called class


    3. Gets the Class method: object. GetClass (), Class.forName (object name), type. Class

      A class actually represents a type, which is not necessarily a class, it is actually a generic type.

      A virtual machine manages a class object for each type. You can dynamically create an instance of a class from newinstance ().

    4. Filed, method, constructor these three classes have a GetName method that returns the corresponding name

      There is a getmodifiers method that returns the modifier usage, which can be judged using IsPublic in the Modifier class. You can also print out the ToString.



    5. Field class: The domain used to describe a 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 that describes the type of domain to which it belongs

Setaccessible (true) blocks access checks in the Java language so that private properties can also be queried and set

Get (obj) set ()

6. Method, constructor is similar to field.

Get method pointer: Method M = Obj.class.getMethod (methods name)

Call Method: String n = m.invoke (obj,... Parameters

Note: If the static method is called, the first argument is empty


The following appendix is a small example:

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+ " ");   &NBsp;         }             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) {          &NBSp; 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) {    &Nbsp;           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 ();         }    }}


This article is from the "Red Xiao" blog, please be sure to keep this source http://cnslp.blog.51cto.com/11387491/1918467

Java Fundamentals-Interface and reflection summary

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.