栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java 自定义注解 & 泛型

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 {

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

    
    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 ChildGeneric extends  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 GenericInteeeerFaceImplThird implements  GenericInterFace {
}

                        3、泛型类继承:

package com.lemon.self.annotation.fanxing;


public interface GenericInterFaceImplSecond extends 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 generic){
        System.out.println(generic);
    }

    public void showInfo3(Generic 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和ArrayList看上去是不同的类型,但是上面的程序会认为它们是相同的类型。ArrayList和ArrayList在运行时事实上是相同的类型。这两种类型都被擦除成它们的“原生”类型,即ArrayList。无论何时,编写泛型代码时,必须提醒自己“它的类型被擦除了”。

     

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/573032.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号