博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java泛型探索——泛型类
阅读量:7040 次
发布时间:2019-06-28

本文共 2926 字,大约阅读时间需要 9 分钟。

hot3.png

本文主要讨论一下如何声明泛型类,讨论的范围涉及构造函数、静态成员、内部类。

构造函数

泛型的类型参数首先声明在首部:

public class Pair
{ private final T first; private final U second; private static int count = 0; public Pair(T first, U second) { this.first = first; this.second = second; } public T getFirst { return first; } public U getSecond { return second; } public static void main(String[] args) { Pair
pair = new Pair
(2,"generic test"); // 1 line System.out.println(pair.getFirst); System.out.println(pair.getSecond); }}

当我们调用构造函数时,真实的泛型参数将被传入,如代码中的“1 line”这行所示。

Pair<String, Integer> pair = new Pair("one",2);

构造函数也可以向上述这么写,但会提示warning。

甚至我们还可以这么写:

Pair pair = new Pair("one",2);

这个不会报错,也会提示warning。

静态成员

对于静态成员而言,是类可见的,所以

public class Cell
{ private final int id; private final T value; private static int count = 0; private static synchronized int nextId { return count++; } public Cell(T value) { this.value = value; id = nextId; } public T getValue { return value; } public int getId { return id; } public static synchronized int getCount { return count; }}

我们可以通过Cell.getCount直接获取静态成员,并不需要指定类型参数。

如果指定类型参数反而报错:

Cell<Integer>.getCount // compile-time error

泛型类的静态成员及静态函数是对整个泛型类而言,因此固定类型参数的类进行调用:如Cell<Integer>.getCount。

同样的,像下面这样的代码也是错误的:

class Cell2
{private final T value;private static List
values = new ArrayList
; // illegal public Cell(T value) { this.value=value; values.add(value); } public T getValue { return value; }public static List
getValues { return values; } // illegal}

内部类

对非静态内部类而言,其外部类(outer class)的类型参数对它是可见的。因此内部类可以使用外部类的类型参数:

public class LinkedCollection
extends AbstractCollection
{ private class Node { private E element; private Node next = null; private Node(E elt) { element = elt; } } ....}

而对于静态内部类而言,则类型参数则是不可见的,我们必须自己定义内部类的类型参数:

class LinkedCollection
extends AbstractCollection
{ private static class Node
{ private T element; private Node
next = null; private Node(T elt) { element = elt; } }}

我们在使用外部类中,使用内部类变量时,将外部类类型参数E传入内部类即可:

class LinkedCollection
extends AbstractCollection
{ private static class Node
{ .... } private Node
first = new Node
(null); private Node
last = first;}

在软件工程中,比较推荐使用静态内部类,因为它不持有外部类的引用。因此静态内部类可以像类似外部类一样使用,更简单,更好理解。

tips:

如果内部类的的修饰符是public:

对非静态内部类而言,可以这么访问Node:LinkedCollection<E>.Node

而对静态内部类而言:LinkedCollection.Node<E>。

转载于:https://my.oschina.net/u/2391658/blog/905207

你可能感兴趣的文章
Re: 从零开始的【comic spider】《最简单的实现》(上)
查看>>
Java 单例模式学习理解
查看>>
ios创建可拖动的视图
查看>>
Linux常用的基本命令12
查看>>
ORACLE数据库事务隔离级别介绍
查看>>
DHCP服务和http服务
查看>>
bitnami 使用记录
查看>>
Vsftpd+(linux)文件服务器
查看>>
JEPLUS之循环报表—JEPLUS软件快速开发平台
查看>>
从一个线上问题分析binlog与内部XA事务提交过程
查看>>
网页版式设计与平面构图
查看>>
view桌面模板控制usb权限
查看>>
吾日三省吾身
查看>>
【office培训】【王佩丰】Excel2010视频教程第2讲:单元格格式设置
查看>>
android inflate
查看>>
libxml2的编译与安装
查看>>
详述Google针对Android平板App发布的十大开发准则
查看>>
CentOS 7安装python3笔记
查看>>
XenApp 屏幕录像播放提示版本错误
查看>>
linux 通配符、元字符和特殊字符
查看>>