• java泛型demo


    1.泛型类

    public class Dog<T> {
        private T age;
    
        public Dog(T age) {
            this.age = age;
        }
    
        public T getAge() {
            return age;
        }
    
        public static void main(String[] args) {
            //Java7之后,尖括号中是不需要填写参数的
            Dog<String> dog=new Dog<>("28");
            System.out.println(dog.getAge());
        }
    }

    普通的类

    public class Dog {
        private Object age;
    
        public Dog(Object age) {
            this.age = age;
        }
    
        public Object getAge() {
            return age;
        }
    
        public static void main(String[] args) {
            Dog dog=new Dog("28");
            System.out.println(dog.getAge());
        }
    }

    这样的代码是完全可以执行了,那为什么还需要泛型类?

    1.安全性

    public class Dog {
        private Object age;
    
        public Dog(Object age) {
            this.age = age;
        }
    
        public Object getAge() {
            return age;
        }
    
        public static void main(String[] args) {
            Dog dog=new Dog("28");
            Integer age=(Integer) dog.getAge();
            System.out.println(age);
        }
    }

    上面的代码编译是完全可以通过的,但是执行的时候就会出现ClassCastException异常
    2.可读性好,省去了反复的强制类型转换。

    对于泛型类,java编译器会将泛型代码转换成普通的非泛型代码,

    所以对于虚拟机来说,是没有泛型类的概念的。
    为什么这么设计呢?应为泛型是jdk6以后才有的,为了向下兼容。

    泛型方法:

    public class TestMethod {
        public static <T> boolean isHas(T[] arr, T elemt){
            for(T t:arr){
                if(t.equals(elemt)){
                    return true;
                }
            }
            return false;
        }
        public <S> boolean isString(S s){
           if(s instanceof String){
               return true;
           }
           return false;
        }
    
        public static void main(String[] args) {
            Integer[] arr={1,5,6,8};
            System.out.println(isHas(arr,8));
            TestMethod testMethod=new TestMethod();
            System.out.println(testMethod.isString(5));
        }
    }

    一个方法是不是泛型和他的类是不是泛型没有任何关系。
    泛型方法需要在方法的返回值前先声明,在从后面的代码中使用。

    泛型接口:
    参照Comparable接口。

    public class TestComparable implements MyComparable<TestComparable>{
    
        private Integer n;
    
        public TestComparable(int n) {
            this.n = n;
        }
    
        @Override
        public boolean isEquals(TestComparable testComparable) {
            return this.n.intValue()==testComparable.getN().intValue()?true:false;
        }
    
        public Integer getN() {
            return n;
        }
    
        public static void main(String[] args) {
            TestComparable testComparable1=new TestComparable(2);
            TestComparable testComparable2=new TestComparable(2);
            System.out.println(testComparable1.isEquals(testComparable2));
        }
    }
    interface MyComparable<T> {
        boolean isEquals(T t);
    }

    类型参数继承某个类

    /**
     * 测试继承class
     */
    public class TestInheritClass<T extends  Father>{
        private T t;
    
        public TestInheritClass(T t) {
            this.t = t;
        }
    
        void output(){
            System.out.println(t.getName());
        }
    
        public static void main(String[] args) {
            Child child=new Child("李逵");
            TestInheritClass<Child> t=new TestInheritClass<>(child);
            t.output();
        }
    }
    class Father{
        private String name;
    
        public String getName() {
            return name;
        }
    
        public Father(String name) {
            this.name = name;
        }
    }
    class Child extends Father{
        public Child(String name) {
            super(name);
        }
    
    }

    测试继承接口

    /**
     * 测试继承接口
     */
    public class TestInheritInterface<T extends IFruits> {
        private T t;
    
        public TestInheritInterface(T t) {
            this.t = t;
        }
        void output(){
            t.shape();
        }
    
        public static void main(String[] args) {
            Apple apple=new Apple();
            TestInheritInterface<Apple> t=new TestInheritInterface<>(apple);
            t.output();
        }
    }
    interface IFruits{
        //形状
        void shape();
    }
    class Apple implements IFruits{
        @Override
        public void shape() {
            System.out.println("苹果是圆形的。");
        }
    }
  • 相关阅读:
    C#获取类以及类下的方法(用于Asp.Net MVC)
    ES6学习笔记
    在nuget上发布自己的程序集教程
    C#创建IIS站点及相应的应用程序池,支持IIS6.0+Windows Server 2003. 使用Builder设计模式
    ASP.Net Mvc实现自定义User Identity用户身份识别系统(2)
    ASP.Net Mvc实现自定义User Identity用户身份识别系统(1)
    C#实现.ini文件读写操作
    C#实现注册表 LocalMachine 目录下CURD工具类
    博客园打赏功能(未申请下js权限使用二维码打赏功能)
    WebServeice 动态代理类
  • 原文地址:https://www.cnblogs.com/guoyansi19900907/p/12127931.html
Copyright © 2020-2023  润新知