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 {

    //?未知--List<String>
    //一句话:?接收子类的任意数据类型
    //如果是单个问号的话:那么就告诉你一定是他子类?根据子类里面的泛型去决定的
    public static void displayName(Collection<?> collection) {
        if (collection==null) {
            return; 
        }
        for (Object object : collection) {
            System.out.println(object );
        }
    }
    //缩小范围,限制在子类 
    /*
     public static void displayName(Collection<? extends Animal> collection) {
        if (collection==null) {
            return; 
        }
        for (Animal animal : collection) {
            System.out.println(animal.toString() );
        }
    }
     */
    public static Animal getCat(Animal[] animals,String name) {
        Animal animal2  = null;
        if (animals!=null) {
             for (Animal animal : animals) {
                 if (animal.name.equals(name)) {
                    animal2 = animal;  
                    break;
                }
             }
        }
        return animal2;
        
    }
    public static void main(String[] args) {

        ArrayList<String> strings = new ArrayList<String>();//指定数组里的元素
        strings.add("1111");
        strings.add("2222");
        displayName(strings);
        //1111
        //2222
        System.out.println("======================");
        ArrayList<Integer> integers = new ArrayList<Integer>();
        integers.add(1111);
        integers.add(2222);
        displayName(integers);
        System.out.println("======================");
        ArrayList<Float> floats = new ArrayList<Float>();
        //List Set 的顶级类 Collection
        Set<String> sets = new HashSet<String>();
        sets.add("1111");
        sets.add("2222");
        displayName(sets);//封装、继承、多态
        
        //======================================
        Animal cat = new Cat("小猫", "喵喵喵");
         System.out.println(cat.toString());
         Animal pig = new Cat("小猪", "呼呼呼");
         System.out.println(pig.toString());
         //动物的名字是小猫他能:喵喵喵
         //动物的名字是小猪他能:呼呼呼
         ArrayList<Cat> cats = new ArrayList<Cat>();
         cats.add((Cat) cat);
         cats.add((Cat) pig); 
         for (Cat cat2: cats) {
             System.out.println("=="+cat2);
         }
         displayName(cats);
         
         Animal[] animals = {new Cat("小猫1", "喵喵喵"),new Cat("小猫2", "喵喵喵"),new Cat("小猫3", "喵喵喵")};
        Animal animal =   getCat(animals, "小猫3"); 
        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 {

    private String sound;
    public Cat(String name, String sound) {
        super(name);
        this.sound = sound;
    }
    public String  toString() {
        return super.toString() + "他能:"+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 查询

猜你喜欢

转载自www.cnblogs.com/StevenHuSir/p/Java_T.html