Java泛型和泛型类型 [英] Java Generics and generic types

查看:145
本文介绍了Java泛型和泛型类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类ExtA,它包含一个过滤器函数来过滤一个ArrayList:

I have a class ExtA which contains a filter function to filter an ArrayList:

public ExtA<T> filt(...)
{

 //code


}

当我编译它时给我错误:无法找到符号类T.为什么这样?

when I compile it is giving me error: cannot find symbol- class T. Why is this?

推荐答案

你必须告诉它,在这种情况下,T是一个泛型类型:

You have to tell it, that T is a generic type in this case:

public <T> ExtA<T> filt(Func<T, Boolean> a)

您使用符号 T ,但该符号仅在接口声明本身中有效。你在方法中使用的 T 是一个不同的 T 。您必须再次声明它,因为该方法未在接口声明中实现。

You declared your interface with the symbol T, but that symbol is only valid in the interface declaration itself. The T you are using in your method is a different T. You have to declare it again, since the method is not implemented inside the interface declaration.

这篇关于Java泛型和泛型类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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