java基础3

java基础3

注解与反射

注解

注解不仅仅是可以让程序员能读懂的,并且程序也可以读;

Annotation是从JDK5.0开始引入的新技术

作用:

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

格式:

  • 以“@注解名”在代码中存在的,还可以添加一些参数值

内置注解

  • @Override:定义在java.lang.Override中,只适用于修辞方法,表示一个方法声明打算重写父类的另一个方法声明
  • @Deprecated:定义在java.lang.Deprecated中,此注释可以用于修辞方法,属性,类,表示不鼓励继续使用这样的元素。
  • @SuppressWarnings:定义在java.lang.SuppressWarnings中,用来抑制编译时的警告信息,这个需要添加一个参数才能正确使用
    1. @SuppressWarnings(“all”)
    2. @SuppressWarnings(“unchecked”)
    3. @SuppressWarnings(value={“unchecked”,“deprecation”})

反射

  • 反射赋予了java语言的动态性

注:动态语言

简单来说,动态语言与静态语言最大的区别就是在运行过程中结构能不能再改变

正常方式:引入需要的包类名称—>通过new实例化—>取得实例化对象

反射方式:实例化对象—>getclass()—>得到完整的“包类”名称

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZmopDJqQ-1611757631210)(C:\Users\万年历1\Desktop\BAD150D92FCF42628E001FA0F24E4946.png)]

import java.lang.annotation.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

//练习反射操作注解
public class Test01 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
        //获得类对象
        Student student=new Student();

        //通过forname方法获得
        Class c1=Class.forName("kuangstudy.reflect.Student");
        System.out.println(c1.hashCode());
        //通过类名获得
        Class c2=Student.class;
        System.out.println(c2.hashCode());
        //通过对象名获得
        Class c3=student.getClass();
        System.out.println(c3.hashCode());
        //通过基本内置类
        Class c4=Integer.TYPE;
        System.out.println(c4);
        //获得父类类型
        Class c5=c1.getSuperclass();
        System.out.println(c5);


        //获得注解信息
        Annotation[] annotations=c1.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }

        MyAo myAo=(MyAo) c1.getAnnotation(MyAo.class);
        System.out.println(myAo.value());

        //获取类指定的注解
        Field field = c1.getDeclaredField("name");
        FieldAo ann=field.getAnnotation(FieldAo.class);
        System.out.println(ann.col());
        System.out.println(ann.type());
        System.out.println(ann.length());


        //获得指定的方法
        System.out.println("======================");
        Method an= c1.getMethod("setAge",int.class);
        System.out.println(an);

    }
}

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface MyAo{
    String value();
}

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface FieldAo{
    String col();
    String type();
    int length();
}


@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface Methd{
    String value();
}

@MyAo("db")
class Student{
    @FieldAo(col = "db-id",type = "int",length = 10)
    private int id;
    @FieldAo(col = "db-age",type = "int",length = 10)
    private int age;
    @FieldAo(col = "db-name",type = "varchar",length = 3)
    private String name;
    public Student() {
    }

    public Student(int id,int age,String name) {
        this.id = id;
        this.age=age;
        this.name=name;
    }

    @Methd("getId")
    public int getId() {
        return id;
    }

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

}

运行结果:

1531448569
1531448569
1531448569
int
class java.lang.Object
@kuangstudy.reflect.MyAo("db")
db
db-name
varchar
3
======================
public void kuangstudy.reflect.Student.setAge(int)

Process finished with exit code 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值