什么是< TYPE>在java中是什么意思 [英] What does the <TYPE> in java mean?

查看:303
本文介绍了什么是< TYPE>在java中是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过声明,接口和类 TYPE< CLASS>

I have seen declarations, interfaces and classes that go TYPE<CLASS>

这是做什么的/意味着?

What does this do/mean?

推荐答案

没有证据,我相信你在谈论 Java的泛型支持...

Without evidence, I believe you're talking about Java's Generics support...


泛型允许你抽象类型

Generics allow you to abstract over types

在Java 5之前,很难提供能够支持多种不同类型对象而无需代码的类对于每种特定的情况,所以人们通常会传递对象

Before Java 5 it was difficult to provide classes that were capable of supporting multiple different types of Objects without having to code for each specific situation, so it was common for people to pass Object instead.

这会导致许多困难的选择要在运行时制作,你必须进行运行时检查以查看是否可以将给定的对象转换为可用的类型...例如

This leads to many difficult choices to make at runtime, you'd have to do a runtime check to see if it was possible to cast a given Object to a usable type...for example

List myIntList = new LinkedList(); // 1
myIntList.add(new Integer(0)); // 2
Integer x = (Integer) myIntList.iterator().next(); // 3    

现在,这是相当明显的,但如果你只是通过列表,您必须检查列表中的每个元素是否正确...

Now, this is reasonably obvious, but if you were passed just a List, you'd have to check each and every element in the list for correctness...

但现在,我们可以做这...

But now, we can do this...

List<Integer> myIntList = new LinkedList<Integer>(); // 1'
myIntList.add(new Integer(0)); // 2'
Integer x = myIntList.iterator().next(); // 3'

这是一个基本上说此列表只包含整数类型对象的合约。

This is a contract that basically says "This list only contains Integer type's of objects".

使用泛型,您可以构造一个能够处理多种不同数据类型或一系列数据类型的类(即约束参数,以便必须扩展它来自特定的父类型)。

With generics you can construct a single class that is capable of handling multiple different data types or a family of data types (ie constraint the parameter so that it must be extended from a particular parent type).

Iterator<? extends Number> itNum;

基本上说,这将包含从 Number ,包括整数 Double Float ...

Basically says, this will contain objects that inherit from Number, include Integer, Long, Double, Float...

通常在方法和类减速中你会看到类似于...的东西。

Often in method and class decelerations you will see something similar to...

public class MyGenericClass<T> {...}

public class MyGenericClass<T extends MyBaseObject> {...}

这允许您参考 T 好像它是具体的对象类型,例如......

This allows you to refer to T as if it were a concrete object type, for example...

public class MyGenericClass<T extends MyBaseObject> {
    private T value;
    public MyGenericClass(T value) {
        this.value = value;
    }
}

这允许编译器(和JVM)基本上用音乐会类型替换标记 T (好吧,它比那复杂一点,但这就是魔术)......

This allows the compiler (and JVM) to essentially "replace" the marker T with a concert type (okay, it's a little more complicated then that, but that's the magic)...

这允许做... ...

This allows to do things like...

... new MyGenericClass<MySuperObject>(new MySuperObject());
... new MyGenericClass<MySuperSuperObject>(new MySuperSuperObject());

并且知道它只接受我指定的对象类型...

And know that it will only ever accept the type of object I specify...

请仔细阅读第一段中的链接,我确信它可以做得更公正然后我可以;)

Have a read through the link in the first paragraph, I'm sure it can do more justice then I can ;)

这篇关于什么是&lt; TYPE&gt;在java中是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆