java 自定义注解 & 泛型

一、自定义注解

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 {

    // 注解的参数,这个不是一个方法,是注解的一个参数
    // 参数类型  参数名 () ;组成

    /**
     *
     * 参数可以添加默认值: 使用default
     */
    String name() default  "";

    String[] schools() default  {"a","b"};
}

2、在需要的可以标记的类,方法、参数等地方 使用 @注解名称(参数列表)

二、泛型:

        1、泛型类:

                

package com.lemon.self.annotation.fanxing;

/**
 * 泛型类
 * @param <T> 泛型标示--类型形参
 *              T 创建对象的时候指定的具体的数据类型
 */
public class Generic<T> {

    public T key;



}

        2、泛型类继承:

                1、子类是泛型类:

                

package com.lemon.self.annotation.fanxing;

/**
 * 泛型继承: 子类是泛型类的写法
 * @param <T>
 */
public class ChildGeneric<T> extends  Generic<T> {
}

                2、子类是普通类:

package com.lemon.self.annotation.fanxing;

/**
 * 子类继承泛型类: 子类是普通类 写法,需要明确泛型类型
 */
public class ChildExtendGenenric  extends  Generic<String>{
}

        3、泛型接口:

        

package com.lemon.self.annotation.fanxing;

/**
 * 泛型接口:
 *  实现类是泛型类,那么泛型类型需要保持一致
 *  实现类是普通类,那么实现类需要指定为具体的类型
 * @param <T>
 */
public interface GenericInterFace<T>{
}

        4、泛型接口继承和实现:

                       1、普通类实现:

        

package com.lemon.self.annotation.fanxing;

/**
 * 实现接口泛型的普通类
 */
public class GenericInterFaceImplFirst implements  GenericInterFace<String>{
}

                        2、泛型类实现:

package com.lemon.self.annotation.fanxing;

/**
 * 泛型接口实现的类是泛型类,同时指定泛型标记
 * @param <T>
 */
public class GenericInteeeerFaceImplThird<T> implements  GenericInterFace<T> {
}

                        3、泛型类继承:

package com.lemon.self.annotation.fanxing;

/**
 * 泛型接口继承
 * @param <T>
 */
public interface GenericInterFaceImplSecond<T> extends GenericInterFace<T>{
}

        5、泛型方法:泛型方法中的类型是调用的时候才指定的,与所在的类的类型没有关系。

泛型方法可以指定为静态的,但是泛型类的成员方法不可以为静态的。

        

package com.lemon.self.annotation.fanxing;

/**
 * 泛型方法:
 *      必须使用泛型标识: <T..> 返回值类型
 */
public class FanxingMethod {


    /**
     * 定义泛型方法
     * @param <T>
     * @return
     */
    public  <T> T index(){
        return null;
    }

  
}

        6、泛型类成员方法:

package com.lemon.self.annotation.fanxing;

/**
 * 泛型类
 * @param <T> 泛型标示--类型形参
 *              T 创建对象的时候指定的具体的数据类型
 */
public class Generic<T> {

    public T key;

    public  T getKey(){
        return null;
    }



}

        7、类型通配符: ?

package com.lemon.self.annotation.fanxing;

/**
 * 泛型方法:
 *      必须使用泛型标识: <T..> 返回值类型
 */
public class FanxingMethod {


    /**
     * 定义泛型方法
     * @param <T>
     * @return
     */
    public  <T> T index(){
        return null;
    }

    /**
     * 类型通配符: ?; 表示的是类型实参,可以代表任意的类型。
     *  类型通配符使用上限: ? extends Number 表示类型可以是Number的一种类型
     *  类型通配符使用下限: ?  super Number 表示只可以使用指定类型的父类类型
     */
    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<String>().getClass();
        Class c2 = new ArrayList<Integer>().getClass();
        System.out.println(c1 == c2);  // true
    }
}

尽管ArrayList<String>和ArrayList<Integer>看上去是不同的类型,但是上面的程序会认为它们是相同的类型。ArrayList<String>和ArrayList<Integer>在运行时事实上是相同的类型。这两种类型都被擦除成它们的“原生”类型,即ArrayList。无论何时,编写泛型代码时,必须提醒自己“它的类型被擦除了”。

     

猜你喜欢

转载自blog.csdn.net/qq_31319235/article/details/121421313