java中向上的尖括号是什么,什么< T> (尖括号)在Java中是什么意思?

I am currently studying Java and have recently been stumped by angle brackets(<>). What exactly do they mean?

public class Pool{

public interface PoolFactory{

public T createObject();

}

this.freeObjects= new ArrayList(maxsize)

}

What does the mean? Does it means that I can create an object of type T?

解决方案

is a generic and can usually be read as "of type T". It depends on the type to the left of the <> what it actually means.

I don't know what a Pool or PoolFactory is, but you also mention ArrayList, which is a standard Java class, so I'll talk to that.

Usually, you won't see "T" in there, you'll see another type. So if you see ArrayList for example, that means "An ArrayList of Integers." Many classes use generics to constrain the type of the elements in a container, for example. Another example is HashMap, which means "a map with String keys and Integer values."

Your Pool example is a bit different, because there you are defining a class. So in that case, you are creating a class that somebody else could instantiate with a particular type in place of T. For example, I could create an object of type Pool using your class definition. That would mean two things:

My Pool would have an interface PoolFactory with a createObject method that returns Strings.

Internally, the Pool would contain an ArrayList of Strings.

This is great news, because at another time, I could come along and create a Pool which would use the same code, but have Integer wherever you see T in the source.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值