package com.chad.lambda;
import java.lang.annotation.*;
import java.util.ArrayList;
import java.util.List;
public class TestAnnotation {
//注解可以显示显示值
@MyAnnotation2(name = "查德",school = {"西北大学","西工大"})
// @MyAnnotation2()
public void test(){
}
}
//定义一个注解
//表示注解可以用在哪个地方
@Target(value = {ElementType.METHOD,ElementType.TYPE})//可以用在什么地方
@Retention(value = RetentionPolicy.CLASS)//保持作用的范围
@Documented//是否将我们的注解生成到Javadoc中
@Inherited//子类是否可以继承父类的注解
@interface MyAnnotation{
}
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2{
//注解的参数: 参数类型 + 参数名();
// String name();
String name() default "";
int age() default 0;
int id() default -1;//如果默认为负一,就代表不存在
String[] school();
}
反射的代码演示
package com.chad.lambda;
public class Test03 {
public static void main(String[] args) throws ClassNotFoundException {
Class c1 = Class.forName("com.chad.lambda.User");
System.out.println(c1);
Class c2 = Class.forName("com.chad.lambda.User");
Class c3 = Class.forName("com.chad.lambda.User");
Class c4 = Class.forName("com.chad.lambda.User");
System.out.println(c2.hashCode());
System.out.println(c3.hashCode());
System.out.println(c4.hashCode());
}
}
//实体类pojo
class User{
private String name;
private int age;
private int id;
public User() {
}
public User(String name, int age, int id) {
this.name = name;
this.age = age;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", id=" + id +
'}';
}
}
运行结果
c1是个class对象
他们的hashcode都是一样的,一个类在内存中只有一个Class对象,一个被类加载后,类的整个结构都封装在了Class对象中。
反射的三种方式演示
package com.chad.lambda;
import org.apache.hadoop.yarn.webapp.hamlet.Hamlet;
public class Test04 {
public static void main(String[] args) throws ClassNotFoundException {
Person person = new Student();
System.out.println("这个人的name是:"+ person.name);
//方式一:
//通过对象获得
Class c1 = person.getClass();
//方式二
//通过类获得
Class<Student> c2 = Student.class;
//方式三
//通过全路径
Class<?> c3 = Class.forName("com.chad.lambda.Student");
System.out.println(c1.hashCode());
System.out.println(c2.hashCode());
System.out.println(c3.hashCode());
//获得父类型
Class superclass = c1.getSuperclass();
System.out.println(superclass);
}
}
class Person{
public String name;
public Person() {
}
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(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 = "老师李四";
}
}
只要元素类型与维度一样,就是同一个Class
代码演示
package com.chad.lambda;
import java.lang.annotation.ElementType;
public class Test05 {
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 = Interger.class;
Class c8 = void.class;
Class c9 = Class.class;
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
System.out.println(c4);
System.out.println(c5);
System.out.println(c6);
System.out.println(c7);
System.out.println(c8);
System.out.println(c9);
int[] a = new int[10];
int[] b = new int[100];
Class<? extends int[]> aClass = a.getClass();
Class<? extends int[]> aClass1 = b.getClass();
System.out.println(aClass.hashCode());
System.out.println(aClass1.hashCode());
}
}
只要元素类型与维度一样,就是同一个Class