小白学Java第七天

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


一、注解

Annotation 注解

1.Annotation 作用

不是程序本身,可以一对程序作出解释
可以被其他程序读取

2. Annotation 的格式

@注解名 存在于代码中 可以添加参数值
@SuppressWarnings(value=“unchecked”)

3. Annotation 使用

可以附加在package、class、method、field上面,相当于添加了额外的辅助信息,可以通过反射机制访问这些数据

4. 内置注解

  • @Override 此注解只适用于方法,表示一个方法重写
  • @Deprecated 此注解可以用于修饰方法、属性、类,表示不鼓励程序员用这样的元素,但是可以使用。
  • @SuppressWarnings 用来抑制编译时的警告信息,需要添加参数 @SuppressWarnings(value=“unchecked”)

5. 元注解

元注解的作用时负责注解其他注解
@Target:用于描述注解的使用范围
@Retention:表示需要在什么级别保存该注解信息
@Document:说明该注解被包含在javadoc中
@Inherited:说明子类可以继承父类中的该注解

6. 自定义注解

使用@interface自定义注解,自动继承了java.lang.annotation.Annotation接口

public class Test01 {

    //当只有一个参数时参数名定义成value,使用注解时可以不用参数名
    //当给定义参数时有default关键字时可以不用传递参数
    @MyAnnotation("张")
    public static void test(){

    }
}

//自定义一个注解
//元注解
@Target({ElementType.TYPE, ElementType.METHOD})//定义注解可以使用的地方
@Retention(RetentionPolicy.RUNTIME)//定义注解使用的时机
@interface MyAnnotation{
    //注解参数的定义 类型 名字();
    //当只有一个参数时参数名定义成value,使用注解时可以不用参数名
    String value();
    //当给定义参数时有default关键字时可以不用传递参数
    int age() default 0;
}

二、反射

Reflection 反射
Reflection 反射是Java被视为动态语言的关键,反射机制允许程序在执行期借助Reflection API 取得任何类的内部信息,并能直接操作任意对象的内部属性及方法

Class c = Class.forName("java.lang.Sring")

1. Java反射机制提供的功能

在运行时判断任意一个对象所属的类>在运行时构造任意一个类的对象
在运行时判断任意一个类所具有的成员变量和方法>在运行时获取泛型信息
在运行时调用任意一个对象的成员变量和方法>在运行时处理注解
生成动态代理

2. Java反射优点和缺点

优点:
可以实现动接创建对象和编译,体现出很大的灵活性
缺点:
对性能有影响。使用反射基本上是一种解释操作,我们可以告诉JVM,我们希望做什么并且它满足我们的要求。这类操作总是慢于直接执行相同的操作。

3. Java反射获取Class类的实例

  • 若已知具体类,通过类的class属性获取
//通过类获得Class类
        Class<Person> c3 = Person.class;
        System.out.println(c3.hashCode());
  • 已知某个类的实例,调用该实例的getClass()方法获取
//通过对象获得Class类
        Class c2 = person.getClass();
        System.out.println(c2.hashCode());
  • 已知一个类的全类名,且该类在类的路径下,可通过Class类的静态方法forName()获取
        //通过forName获得Class类
        Class c1 = Class.forName("com.huang.reflection.Person");
        System.out.println(c1.hashCode());
        Person person = new Person();

4. 通过反射获取运行时类的完整结构

Field、Method、Constructor、Superclass、Interface、Annotation

public class Test02 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
        Class c1 = Class.forName("com.huang.reflection.Student");

        //获得所有属性:只能获得Public修饰的属性
        Field[] fields = c1.getFields();
        for (Field field : fields) {
            System.out.println(field);
        }
        System.out.println("=======================");
        //获得所有属性:能获得private修饰的属性
        Field[] declaredFields = c1.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField);
        }

        System.out.println("Method======================================");

        //获得限于Public修饰的方法方法,包括父类的方法()
        Method[] methods = c1.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
        System.out.println("=======================");
        //获得所有方法,包括父类的方法
        Method[] declaredMethods = c1.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod);
        }
        System.out.println("Constructor======================================");

        Constructor constructor = c1.getConstructor(null);
        System.out.println(constructor);
        Constructor constructor1 = c1.getConstructor(int.class,String.class);
        System.out.println(constructor1);
        System.out.println("Annotation======================================");
        Annotation annotation = c1.getAnnotation(MyAnnotation.class);
        System.out.println(annotation);
        //需要将获得的注解转换为自己定义的注解类型
        MyAnnotation annotation1 = (MyAnnotation) annotation;
        System.out.println(annotation1.value());
        System.out.println(((MyAnnotation) annotation).age());
        System.out.println("Superclass======================================");
        Class superclass = c1.getSuperclass();
        System.out.println(superclass);
        //获得父类的方法
        Method[] superclassMethods = superclass.getMethods();
        for (Method superclassMethod : superclassMethods) {
            System.out.println(superclassMethod);
        }

        System.out.println("Interface======================================");
        Class[] interfaces = c1.getInterfaces();
        for (Class anInterface : interfaces) {

            System.out.println(anInterface);
            Method[] anInterfaceMethods = anInterface.getMethods();
            for (Method anInterfaceMethod : anInterfaceMethods) {
                System.out.println(anInterfaceMethod);
            }
        }
    }
}

  • 20
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值