一、自定义注解
1、定义注解使用@inteface,基本注解的定义
package com.lemon.self.annotation.zhujie;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD,ElementType.FIELD,ElementType.TYPE}) //注解的作用目标,可以为字段,方法、类、参数等等,即可在添加注解的地方,可以为多个
@Retention(RetentionPolicy.RUNTIME) // 注解的应用范围: RunTime 运行时候 class class文件中存在 Resource 源代码中存在
public @interface SelfZhujie {
// 注解的参数,这个不是一个方法,是注解的一个参数
// 参数类型 参数名 () ;组成
String name() default "";
String[] schools() default {"a","b"};
}
2、在需要的可以标记的类,方法、参数等地方 使用 @注解名称(参数列表)
二、泛型:
1、泛型类:
package com.lemon.self.annotation.fanxing; public class Generic{ public T key; }
2、泛型类继承:
1、子类是泛型类:
package com.lemon.self.annotation.fanxing; public class ChildGenericextends Generic { }
2、子类是普通类:
package com.lemon.self.annotation.fanxing; public class ChildExtendGenenric extends Generic{ }
3、泛型接口:
package com.lemon.self.annotation.fanxing; public interface GenericInterFace{ }
4、泛型接口继承和实现:
1、普通类实现:
package com.lemon.self.annotation.fanxing; public class GenericInterFaceImplFirst implements GenericInterFace{ }
2、泛型类实现:
package com.lemon.self.annotation.fanxing; public class GenericInteeeerFaceImplThirdimplements GenericInterFace { }
3、泛型类继承:
package com.lemon.self.annotation.fanxing; public interface GenericInterFaceImplSecondextends GenericInterFace { }
5、泛型方法:泛型方法中的类型是调用的时候才指定的,与所在的类的类型没有关系。
泛型方法可以指定为静态的,但是泛型类的成员方法不可以为静态的。
package com.lemon.self.annotation.fanxing;
public class FanxingMethod {
public T index(){
return null;
}
}
6、泛型类成员方法:
package com.lemon.self.annotation.fanxing; public class Generic{ public T key; public T getKey(){ return null; } }
7、类型通配符: ?
package com.lemon.self.annotation.fanxing;
public class FanxingMethod {
public T index(){
return null;
}
public void showInfo(Generic> generic){
System.out.println(generic);
}
public void showInfo2(Generic extends Number> generic){
System.out.println(generic);
}
public void showInfo3(Generic super Number> generic){
System.out.println(generic);
}
}
8、类型擦除:java认为泛型是同一种类型,不论传入的实际类型是哪一个,在编译之后都被擦除掉了,退化成了Object类型:
public class ErasedTypeEquivalence {
public static void main(String[] args) {
Class c1 = new ArrayList().getClass();
Class c2 = new ArrayList().getClass();
System.out.println(c1 == c2); // true
}
}
尽管ArrayList



