wait:让线程停止 notifyAll:让线程开始
注解(Java.Annotation)
@Deprecated:过时的(不推荐使用,但是可以用)
@Override:重写方法
@SuppressWarnigs(“all”);取消编译时的警告
元注解(注解的注解)
@Target:表示这个注解类型适用的上下文
@Retenrion:运行时级别
@Documented:生产Doc文档时候使用
@Inherited:子类可以继承父
自定义注解
public class Test {
@stop("小明")//value当参数名,可以省略。
public void test(){
}
}
@Target({ElementType.TYPE,ElementType.FIELD,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface stop{
//参数类型 参数名
String value();//没有默认值必须定义
int age() default 0;//有默认值
}
反射机制(java.reflection)
反射会加载类:
public class Test {
public static void main(String[] args) {
Class c1 = zi.class;
String s = new String(String.valueOf(c1));
String aClass = s.replace("class", "");
String trim = aClass.trim();
System.out.println(c1);
try {
Class.forName(trim);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
class fu{
static int b = 10;
static {
System.out.println("父类被加载");
}
}
class zi extends fu{
static final int M=1;
static {
System.out.println("子类被加载");
m = 100;
}
static int m = 300;
}
类加载器
public class Test2 {
public static void main(String[] args) {
///1.获得系统类加载器
ClassLoader loader = ClassLoader.getSystemClassLoader();
System.out.println(loader);
//2.获得系统类加载器的父类加载器--->扩展类加载器
ClassLoader parent = loader.getParent();
System.out.println(parent);
//3.扩展类加载器的父类
ClassLoader parent1 = parent.getParent();
System.out.println(parent1);
}
}
获得类的属性,方法和构造器
public class Test3 {
public static void main(String[] args) throws ClassNotFoundException {
Class c1 = Class.forName("java.lang.Object");
System.out.println(c1.getName());//包名+类名
System.out.println(c1.getSimpleName());//类名
Field[] fields = c1.getDeclaredFields();//获得类的所有属性
for (Field field : fields) {
System.out.println(field);
}
Method[] methods = c1.getDeclaredMethods();//获得类的所有方法
for (Method method : methods) {
System.out.println(method);
}
Constructor[] constructors = c1.getConstructors();//获得类的所有构造器
for (Constructor constructor : constructors) {
System.out.println(constructor);
}
}
}
测试对象关系映射
//使用反射读取注解
/*
1.定义注解
2.在类中使用注解
3.使用反射获取注解
*/
public class Test09 {
public static void main(String[] args) throws Exception {
//通过反射获取注解信息
Class c1 = Class.forName("com.kuang.reflection.Student2");
Annotation[] annotations = c1.getAnnotations();//获得这个类的所有注解信息
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
//获得注解的值
//通过注解的 value方法 获得注解的值
Annotation annotation = c1.getAnnotation(TableKuang.class);
TableKuang annotation1 = (TableKuang) annotation;
System.out.println(annotation1.value());
Field field = c1.getDeclaredField("id");
System.out.println(field);
//获得字段的注解
FieldKuang annotation2 = field.getAnnotation(FieldKuang.class);
//获得注解的参数信息
System.out.println(annotation2.columnName());
System.out.println(annotation2.length());
System.out.println(annotation2.type());
}
}
//学生的实体类
//db : database -->数据库
@TableKuang("db_student")
class Student2{
@FieldKuang(columnName = "id",type = "int",length = 10)
private int id;
@FieldKuang(columnName = "db_age",type = "int",length = 3)
private int age;
@FieldKuang(columnName = "name",type = "varchar",length = 20)
private String name;
public Student2() {
}
public Student2(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 "Student2{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
'}';
}
}
//表名-->类名的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface TableKuang{
String value();
}
//字段类的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface FieldKuang{
//参数类型 参数名
String columnName();//列名
String type();//类型
int length();//长度
}
public class Test3 {
public static void main(String[] args) {
Class c1 = Object.class;//类
Class c2 = Comparable.class;//接口
Class c3 = int[].class;//数组
Class c4 = int[][].class;//二维数组
Class c5 = ElementType.class;//枚举
Class c6 = Override.class;//注解
Class c7 = Integer.class;//基本数据类型
Class c8 = Void.class;//Void
Class c9 = Class.class;//Class
int[] a = new int[10];
Class a1 = a.getClass();
}
}
反射耗时
public class Test {
public static void main(String[] args) throws NoSuchMethodException,
InvocationTargetException, IllegalAccessException {
long a1 = System.currentTimeMillis();
user user = new user();
Class aClass = user.getClass();
Method method = aClass.getMethod("getName");
method.setAccessible(true);//让反射运行快
for (int i = 0; i < 1000000000; i++) {
method.invoke(user);
}
long a2 = System.currentTimeMillis();
System.out.println("耗时:" + (a2 - a1) + "ms");
}
}
class user{
private int age;
private String name;
public user() {
}
public user(int age, String name) {
this.age = age;
this.name = name;
}
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;
}
}