一、接口

  1. 接口中所有的方法默认public static final,不必再写。


  2. 接口中可以定义常量,但不能含有实例域。在java SE8之前,不能在接口中实现方法。


  3. 一个类可以实现多个方法implements 接口1,接口2。。。多个接口之间用“,”隔开。


  4. 克隆:要实现克隆先实现Cloneable接口,重新定义clone方法,并指定public修饰符。

    默认clone是浅克隆,要实现深克隆需要自己重写clone。


  5. 所有的数组类型都有一个public的clone方法。 


二、反射

  1. 概念:能够分析类能力的程序称为反射,能动态操纵java代码程序。


  2. java运行时,系统始终为所有对象维护一个被称为运行时的类型标识。保存这些信息的类称为Class


  3. 获取Class方法:对象.getClass() 、Class.forName(对象名) 、类型.class

    一个Class实际上表示一个类型,这个类型未必是一个类,它实际上是一个泛型。

    虚拟机为每个类型管理一个Class对象。可以通过newInstance()动态创建一个类的实例。

     

  4. Filed、Method、Constructor这三个类都有一个getName方法,返回对应的名称

    都有一个getModifiers方法,返回修饰符使用情况,可以利用Modifier类中isPublic,这种判断。也可以toString打印出来。



  5. Field类:用于描述类的域

  getFileds()返回一个Field对象的数组,包括这个类或者超类的公有域

  getFiled(name) 根据名称返回对应的域

  getDeclaredFields()返回Field对象数组,包括这个类的全部域

  getType() 返回描述域所属类型的Class对象

  SetAccessible(true) 屏蔽java语言的访问检查,使得私有属性也可以被查询和设置

  get(obj) set()

6. Method、Constructor与Field类似。

  获取方法指针:Method m = obj.class.getMethod(方法名)

  调用方法:   String n = m.invoke(obj,...参数)

  注意:如果是调用静态方法,则第一参数为空


以下附录一个小例子:

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+" ");
            }
            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){
            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){
                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();
        }

    }
}