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(value = {ElementType.METHOD, ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
@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;
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 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);
Class c1 = person.getClass();
Class c2 = Class.forName("com.reflection.Student");
Class c3 = Student.class;
System.out.println(c3);
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;
int[] a = new int[10];
int[] b = new int[100];
System.out.println(a.getClass().hashCode() == b.getClass().hashCode());
}
}