zoukankan      html  css  js  c++  java
  • 019.1泛型

     内容:泛型类、泛型接口;

    泛型擦除:编译器时期有泛型,在运行时期没有

    ######泛型类
    为了是各种类型的一些共有方法对象。

    public class FanLeiDemo
    {
        public static void main(String[] args)
        {
            Util<Student> util = new Util<Student>();
            util.setObj(new Student());
            Student stu = util.getObj();
            System.out.println(stu);
        }
    }
    
    class Util<X>{
        private X obj;
        public X getObj()
        {
            return obj;
        }
    
        public void setObj(X obj)
        {
            this.obj = obj;
        }
    }
    
    class Student{
        private String name;
        private int age;
        public String getName()
        {
            return name;
        }
        public void setName(String name)
        {
            this.name = name;
        }
        public int getAge()
        {
            return age;
        }
        public void setAge(int age)
        {
            this.age = age;
        }
    }

    #######泛型方法

    public class FanLeiDemo
    {
    
        public static void main(String[] args)
        {
            Util<Student> util = new Util<Student>();
            util.setObj(new Student());
            util.show(222);
            util.show("xx");
        }
    }
    
    class Util<X>{
        private X obj;
    
        public X getObj()
        {
            return obj;
        }
    
        public void setObj(X obj)
        {
            this.obj = obj;
        }
        public <W>void show(W w){   //泛型方法
            System.out.println(w);
        }
    }

    泛型接口

    通配符

  • 相关阅读:
    03-HTML之body标签
    02-HTML之head标签
    01-HTML介绍
    第十五章 并发编程
    第十四章 网络编程
    第十三章 模块和包
    第十二章 异常和错误
    第十一章 面向对象进阶
    第九章 常用模块(续)
    003 配置负载均衡
  • 原文地址:https://www.cnblogs.com/-nbloser/p/8733186.html
Copyright © 2011-2022 走看看