Annotation是从JDK5.0开始
例如:@Override 重新的注解
public class Test01 extends Object{
//Override 重新的注解
@Override
public String toString(){
return super.toString();
}
//@Deprecated不推荐程序员使用的注解
@Deprecated
public static void test()
{
System.out.println("Deprecated");
}
//镇压警告
@SuppressWarnings("all")
public void test01(){
List list=new ArrayList();
}
public static void main(String[] args) {
test();
}
}
元注解:@Target(用于描述注解使用范围),@Retention(表示什么级别的注解信息),@Documented(注解将被包含在javadoc中),@Inherited(子类可以继承分类中的注解)
@MyAnnotation
public class Test01 extends Object{
@MyAnnotation
public void test(){
}
}
//定义一个注解
//@Target(用于描述注解使用范围)
@Target(value = {ElementType.METHOD,ElementType.TYPE})
@interface MyAnnotation{}
动态语言是运行时可以改变其结构的语言例如:js,php等
静态语言对应的就是运行时不可以改变其结构的语言例如:c++、java
Reflection(反射)是java被视为动态语言的关键
可以实习动态创建对象和编译,体现出很大的灵活性
对性能有影响
java.lang.Class :代表一个类
java.lang.reflect.Method:代表类的方法
java.lang.reflect.Field:代表类的成员变量
java.lang.reflect.Constructor:代表类的构造器
//什么叫反射
public class Test01 {
//通过反射获取的class对象
public static void main(String[] args) throws Exception{
Class c1=Class.forName("com.maple.annotation.User");
System.out.println(c1);
Class c2=Class.forName("com.maple.annotation.User");
Class c3=Class.forName("com.maple.annotation.User");
Class c4=Class.forName("com.maple.annotation.User");
//一个类在内存中只有一个Class对象
//一个类被加载后,类的整个结构都会被封装在Class对象中。
System.out.println(c2.hashCode());
System.out.println(c3.hashCode());
System.out.println(c4.hashCode());
}
}
//实体类:pojo
class User{
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;
}
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;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", id=" + id +
", age=" + age +
'}';
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZOTW3lEw-1589212358895)(C:\Users\yue\AppData\Roaming\Typora\typora-user-images\image-20200511215010567.png)]
//测试Class类创建方式
public class Test01 {
public static void main(String[] args) throws ClassNotFoundException{
Person person =new Student();
System.out.println("这个人是:"+person.name);
//方式1:通过对象获取
Class c1=person.getClass();
System.out.println(c1.hashCode());
//方式2:forname获取
Class c2 = Class.forName("com.maple.annotation.Student");
System.out.println(c2.hashCode());
//方式3:通过类名.class获取
Class c3=Student.class;
System.out.println(c3.hashCode());
//方式4:基本内置类型的包装类都有一个Type属性
Class c4=Integer.TYPE;
System.out.println(c4);
//获得父类类型
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="老师";
}
}
//获取类信息
public class Test01 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
Class c1=Class.forName("com.maple.annotation.User");
//获得类的名字
System.out.println(c1.getName());//获得类名+包名
System.out.println(c1.getSimpleName());//获得类名
//获得类的属性
Field[] fields= c1.getFields();//只能找到public属性
fields= c1.getDeclaredFields();//找到全部属性
for (Field field:fields)
{
System.out.println(field);
}
//获得指定属性的值
Field name= c1.getDeclaredField("name");
System.out.println(name);
//获得类的方法
System.out.println("=========================");
Method[] method = c1.getMethods();//获得本类及父类的全部方法
for (Method method1 : method) {
System.out.println("正常的"+method1);
}
method=c1.getDeclaredMethods(); //获取本类全部方法
for (Method method1 : method) {
System.out.println("getDeclaredMethods"+method1);
}
//获得指定方法
Method getName = c1.getMethod("getName", null);
System.out.println(getName);
//获得指定的构造器
System.out.println("=========================");
Constructor[] constructors = c1.getConstructors();
for (Constructor constructor : constructors) {
System.out.println(constructor);
}
}
}
class User{
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;
}
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;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", id=" + id +
", age=" + age +
'}';
}
}
//通过反射创建动态对象
public class Test02 {
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
//获取Class对象
Class c1 = Class.forName("com.maple.annotation.User");
//构造一个对象
User user=(User)c1.newInstance();//需要一个无参构造器
System.out.println(user);
//通过构造器创造对象
Constructor constructor = c1.getConstructor(String.class, int.class, int.class);
User xiangmong = (User)constructor.newInstance("xiangmong", 001, 18);
System.out.println(xiangmong);
// 通过反射调用普通方法
User user3 = (User)c1.newInstance();
//通过反射获取一个方法
Method setName = c1.getDeclaredMethod("setName", String.class);
//invoke:激活的意思
//(对象,"方法的值")
setName.invoke(user3,"香茗");
System.out.println(user3.getName());
//通过反射操作属性
User user4 = (User)c1.newInstance();
Field name = c1.getDeclaredField("name");
//不能直接操作私有属性
//需要关闭程序的安全监测,属性或方法的setAccessible(true)
name.setAccessible(true);
name.set(user4,"xiaoming");
System.out.println(user4.getName());
}
}
public class Test03 {
public static void main(String[] args) throws Exception {
Class c1 = Class.forName("com.maple.annotation.Student");
//通过反射获得注解
Annotation[] annotations = c1.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
//获得注解的value值
Table table = (Table)c1.getAnnotation(Table.class);
String value=table.value();
System.out.println(value);
//获得类指定的注解
Field f = c1.getDeclaredField("id");
Fieid annotation = f.getAnnotation(Fieid.class);
System.out.println(annotation.columnName());
System.out.println(annotation.length());
System.out.println(annotation.type());
}
}
@Table("db_student")
class Student{
@Fieid(columnName = "db_id",type = "int",length = 10)
private int id;
@Fieid(columnName = "db_age",type = "int",length = 10)
private int age;
@Fieid(columnName = "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;
}
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;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
'}';
}
}
//类名的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Table{
String value();
}
//属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Fieid{
String columnName();
String type();
int length();
}