Java学习_Day 01(学习内容:狂神说注解与反射P1-P8)

P1 注解和反射:什么是注解

public class Test01 {
    // 重写的注解
    @Override
    public String toString() {
        return super.toString();
    }

P2 注解和反射:内置注解

package com.annotation;

import java.util.ArrayList;

public class Test01 {
    // 重写的注解
    @Override
    public String toString() {
        return super.toString();
    }

    // 不推荐程序使用的注解
    @Deprecated
    public static void test(){
        System.out.println("Hi");
    }

    // 抑制警告的注解
    @SuppressWarnings("all")
    public void test02(){
        ArrayList list = new ArrayList();
    }

    public static void main(String[] args) {
        test();
    }
}

P3 注解和反射:元注解

package com.annotation;

import java.lang.annotation.*;

// 测试元注解
@MyAnnotation
public class Test02 {
    @MyAnnotation
    public void test(){

    }
}
// 定义一个注解
// Target表示注解的使用范围
@Target(value = {ElementType.METHOD, ElementType.TYPE})
// 表示注解的有效阶段
@Retention(value = RetentionPolicy.RUNTIME)
// 表示注解是否能够生成在Javadoc中
@Documented
// 子类可继承父类的注解
@Inherited
@interface MyAnnotation{

}

P4 注解和反射:自定义注解

package com.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

public class Test03 {
    @MyAnnotation2(name = "Rye",schools = "南邮")
    public void test(){

    }
    @MyAnnotation3("Jay")
    public void test2(){

    }
}

@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2{
    // 注解的参数 : 参数类型 + 参数名()
    String name() default "";
    int age() default 0;
    // 默认值为-1,代表不存在
    int id() default -1;

    String[] schools() default {"清华大学"};
}


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

P5 注解和反射:反射概述

function f(){
    var x = "var a = 3; var b = 5; alert(a + b)"
    eval(x)
}

P6 注解和反射:获得反射对象

package com.reflection;
// 什么叫反射
public class Test02 {
    public static void main(String[] args) throws ClassNotFoundException {
        Class c1 = Class.forName("com.reflection.User");
        System.out.println(c1);
        // 一个类在内存中只有一个class对象
        // 一个类被加载后,类的整个结构都会被封装在class对象中。
    }
}

class User{
    private String name;
    private int id;
    private int age;

    public User() {
    }

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

    public String getName() {
        return name;
    }

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

    public int getId() {
        return id;
    }

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

    public int getAge() {
        return age;
    }

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

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

P7 注解和反射:得到class类的几种方式

package com.reflection;

public class Test03 {
    public static void main(String[] args) throws ClassNotFoundException {
        Person person = new Student();
        System.out.println("这个人是" + person.name);
        // 方式1
        Class c1 = person.getClass();
        // 方式2
        Class c2 = Class.forName("com.reflection.Student");
        // 方式3
        Class c3 = Student.class;
        System.out.println(c3);
        // 方式4
        Class c4 = Integer.TYPE;
        
        // 获得父类类型
        Class c5 = c1.getSuperclass();
        System.out.println(c5);
    }
}

class Person{
    public String name;

    public Person() {
    }

    public Person(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                '}';
    }
}

class Student extends Person{
    public Student(){
        this.name = "学生";
    }
}

class Teacher extends Person{
    public Teacher(){
        this.name = "老师";
    }
}

P8 注解和反射:所有类型的Class对象

package com.reflection;

import java.lang.annotation.ElementType;

public class Test04 {
    public static void main(String[] args) {
        Class c1 = Object.class;
        Class c2 = Comparable.class;
        Class c3 = String[].class;
        Class c4 = int[][].class;
        Class c5 = Override.class;
        Class c6 = ElementType.class;
        Class c7 = Integer.class;
        Class c8 = void.class;
        Class c9 = Class.class;
        // 只要元素类型与维度一样,就是同一个Class
        int[] a = new int[10];
        int[] b = new int[100];
        System.out.println(a.getClass().hashCode() == b.getClass().hashCode());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值