java高级-泛型<T>和注解封装与使用
一、java 泛型
其实就是约束我们的集合和接口和类
为什么要泛型:规范我数据的操作和类型,它常用语一些接口和父子关系中(继承)
泛型能很好体现 java 的继承,封装这两个特点
用途:泛型、反射 ----> 做项目,搭框架 -》模仿和揣测
ssh ssi 散列的数据结构 Vector<E>
//comparator comparable 排序 //Integer Long Float Double Character Boolean Byte ArrayList< 类 > list = new ArrayList< 类 >(); list.add("x");//Spring --> 面向接口编程 --> 以接口为导向 --> 延伸出很多规范和子类 --- 比如:数据库业务的增删改查
//注解、反射、设计模式 (编程思想)、线程池 (jvm)
//方法注解、类注解、属性注解 === 低耦合 == 就是为了拿到类本身
//日志统计:我要统计出你的请求是来自哪个类,哪个方法,并且执行了多长时间,以及他们的模块名称是什么。
//id name class_name user_name create_name description model update_time user_id
二、注解封装与使用
注解 +Aop
注解 +Filter
①封装
package com.steven.demo;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*
黑认情况下一个自定义的 Annotation 可以在任意地方使用, 如自定义的 MyAnnotatlon, 如果没有指定 Target, 则可以在任意地方使用
1、只能在 Annotation 中出现: ANNOTATION_TYPE
2、只能在构造方法中出现: CONSTRUCTOR
3、在属性中出现: FIELD
4、只能在本地变量中出现: LOCAL_VARIIABLE
5、在方法上出现: METHOD
6、在包声明中出现: PACKAGE
7、在参数声明中出现: PARAMETER
8、类、接口 (包括注释类型) 或枚举声明中使用:TYPE@Documented
在 java.1ang, annotation 中定义的注解类型 Documented,
指示某一类型的注释将通过 javadoc 和类似的默认工具进行文档化。应使用此类型来注释这些英型的声明
其注释会影响由其客户端注释的元素的使用。如果类型声明是用 Documented 来注释的, 则其注释将成为注释元素的公共 API 的一部分。@Inherited
在 java.1ang.annotation 中声明 Inherited, 表示该 annotation 是否被该子类断承下去, 如果没有此注解, 说明不能被断承
如自定义的 MyAnnotatlon, 加入 @Inherited, 说明此 Annotation 可以被子类断承
参考文档:http://chenzehe.iteye.com/blog/14882844
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface HKAnnotationClass {
public String name() default "";
public String model() default "";
}
②使用
import java.util.Set;@HKAnnotationClass(model = "Login",name = "用户登录管理") //注解使用
public class Test {//实现代码 }
三、泛型的简单使用 - 案例
package com.steven.demo;import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;@HKAnnotationClass(model = "Login",name = "用户登录管理") //注解使用
public class Test {</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">?未知--List<String> </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">一句话:?接收子类的任意数据类型 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果是单个问号的话:那么就告诉你一定是他子类?根据子类里面的泛型去决定的</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> displayName(Collection<?><span style="color: rgba(0, 0, 0, 1)"> collection) { </span><span style="color: rgba(0, 0, 255, 1)">if</span> (collection==<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (Object object : collection) { System.out.println(object ); } } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">缩小范围,限制在子类 </span> <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> public static void displayName(Collection<? extends Animal> collection) { if (collection==null) { return; } for (Animal animal : collection) { System.out.println(animal.toString() ); } } </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> Animal getCat(Animal[] animals,String name) { Animal animal2 </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">if</span> (animals!=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (Animal animal : animals) { </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (animal.name.equals(name)) { animal2 </span>=<span style="color: rgba(0, 0, 0, 1)"> animal; </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">; } } } </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> animal2; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) { ArrayList</span><String> strings = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<String>();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">指定数组里的元素</span> strings.add("1111"<span style="color: rgba(0, 0, 0, 1)">); strings.add(</span>"2222"<span style="color: rgba(0, 0, 0, 1)">); displayName(strings); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1111 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2222</span> System.out.println("======================"<span style="color: rgba(0, 0, 0, 1)">); ArrayList</span><Integer> integers = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<Integer><span style="color: rgba(0, 0, 0, 1)">(); integers.add(</span>1111<span style="color: rgba(0, 0, 0, 1)">); integers.add(</span>2222<span style="color: rgba(0, 0, 0, 1)">); displayName(integers); System.out.println(</span>"======================"<span style="color: rgba(0, 0, 0, 1)">); ArrayList</span><Float> floats = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<Float><span style="color: rgba(0, 0, 0, 1)">(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">List Set 的顶级类 Collection</span> Set<String> sets = <span style="color: rgba(0, 0, 255, 1)">new</span> HashSet<String><span style="color: rgba(0, 0, 0, 1)">(); sets.add(</span>"1111"<span style="color: rgba(0, 0, 0, 1)">); sets.add(</span>"2222"<span style="color: rgba(0, 0, 0, 1)">); displayName(sets);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">封装、继承、多态 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">======================================</span> Animal cat = <span style="color: rgba(0, 0, 255, 1)">new</span> Cat("小猫", "喵喵喵"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(cat.toString()); Animal pig </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Cat("小猪", "呼呼呼"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(pig.toString()); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">动物的名字是小猫他能:喵喵喵 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">动物的名字是小猪他能:呼呼呼</span> ArrayList<Cat> cats = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<Cat><span style="color: rgba(0, 0, 0, 1)">(); cats.add((Cat) cat); cats.add((Cat) pig); </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (Cat cat2: cats) { System.out.println(</span>"=="+<span style="color: rgba(0, 0, 0, 1)">cat2); } displayName(cats); Animal[] animals </span>= {<span style="color: rgba(0, 0, 255, 1)">new</span> Cat("小猫1", "喵喵喵"),<span style="color: rgba(0, 0, 255, 1)">new</span> Cat("小猫2", "喵喵喵"),<span style="color: rgba(0, 0, 255, 1)">new</span> Cat("小猫3", "喵喵喵"<span style="color: rgba(0, 0, 0, 1)">)}; Animal animal </span>= getCat(animals, "小猫3"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(animal.toString()); }
}
Animal 类
package com.steven.demo;public class Animal {
public String name;
public Animal() {
}
public Animal(String name) {
this.name = name;
}public String toString() {
return "动物的名字是"+name;
}
}
Cat 类
package com.steven.demo;public class Cat extends Animal {
</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String sound; </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Cat(String name, String sound) { </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">(name); </span><span style="color: rgba(0, 0, 255, 1)">this</span>.sound =<span style="color: rgba(0, 0, 0, 1)"> sound; } </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String toString() { </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">super</span>.toString() + "他能:"+<span style="color: rgba(0, 0, 0, 1)">sound; }
}
四、java T 的使用
//泛型 T 表示通用 <T extends Animal>
// 我们的泛型是用来实现父子关系的一个范围的固定
/* public <T> List<T> arrayToList {return null} */ public static <T extends Animal> T getAnimal(T[] animals,String name) { T animal2 = null; if (animals!=null) { for (T animal : animals) { if (animal.name.equals(name)) { animal2 = animal; break; } } } return animal2;}
//springmvc
//+hibenate 增删改
//+jdbcteamkplte/myibats 查询