java注解和反射

java注解和反射

万人血书感谢西部开源秦疆老师的讲解

注解:

package com.wei.annotation;

import java.util.ArrayList;
import java.util.List;

/***
 * 测试内置注解
 * 
 * @author Wei
 */

@SuppressWarnings("all")
/**
 * @SuppressWarnings注解    
 * 作用是屏蔽一些无关紧要的警告。
 * 使开发者能看到一些他们真正关心的警告。从而提高开发者的效率
 *
 */
public class Test01 extends Object{
	
	//Override  重写的注解
	@Override
	public String toString() {
		return super.toString();	
	}
	
	@Deprecated
	/**
	 * JavaDoc中@Deprecated的字面意义是“这个方法废弃了,不要用它”。
	 * 在项目实际应用中,意思是若某类或某方法加上该注解之后,表示此方法或类不再建议使用,
	 * 调用时也会出现删除线,但并不代表不能用,只是说,不推荐使用,因为还有更好的方法可以调用。
	 * 这个被划去的方法仍然是可以正常使用的,就是一个提示而已。
	 */
	public static void test() {
		System.out.println("@Deprecated");
		List list = new ArrayList();
	}
	
	public static void main(String[] args) {
		test();
	}	
}
package com.wei.annotation;

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

/**
 * 测试元注解
 * @author Wei
 *
 */
public class Test02 {

}

//定义一个注解
//@Target  表示我们的注解可以用在哪些地方
@Target(value = {ElementType.METHOD,ElementType.TYPE})

/**
 * @Retention  表示我们的注解在什么地方还有效
 * runtime>class>sources
 */
@Retention(value = RetentionPolicy.RUNTIME)

//@Documented 表示是否将我们的注解生成在JAVAdoc中
@Documented

//@Inherited 子类可以继承父类
@Inherited
@interface MyAnntation1{
	
}
package com.wei.annotation;

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

//自定义注解
public class Test03 {
	
	@MyAnntation2(schools = "家里蹲大学" , age = 20)
	public void test() {
		
	}
	
	@MyAnntation3("刘立伟")
	public void test02(){
		
	}
}

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

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

反射:

package com.wei.reflect;

//什么叫反射
public class Test01 extends Object{
	public static void main(String[] args) throws ClassNotFoundException {
		//通过反射获取类的class对象
		Class<?> c1 = Class.forName("com.wei.reflect.User");
		System.out.println(c1);
		
		//hashCode 是否相等
		Class c2 = Class.forName("com.wei.reflect.User");
		Class c3 = Class.forName("com.wei.reflect.User");
		Class c4 = Class.forName("com.wei.reflect.User");
	
		//一个类在内存中只有一个Class对象
		//一个类加载后,类的整个结构都会被封装在Class对象中
		System.out.println(c2.hashCode());
		System.out.println(c2.hashCode());
		System.out.println(c2.hashCode());
		System.out.println(c2.hashCode());
	}
}

//实体类 :  pojo 或   entity
class User{
	private String name;
	private int id;
	private int age;
	
	public User() {
		
	}

	public User(String name, int id, int age) {
		super();
		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;
	}
}

未完待续…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值