Java 如何参考通用参数';s自身参数,如T2中的T<;T>;?
考虑以下接口:Java 如何参考通用参数';s自身参数,如T2中的T<;T>;?,java,generics,inheritance,Java,Generics,Inheritance,考虑以下接口: class SomeParamClass<T> { public T getT() {return null;} } class GetThing<T, TSomeImpl extends SomeParamClass<T>> { TSomeImpl thingcreator; GetThing(TSomeImpl thingcreator) { this.thingcreator = thingcrea
class SomeParamClass<T> {
public T getT() {return null;}
}
class GetThing<T, TSomeImpl extends SomeParamClass<T>> {
TSomeImpl thingcreator;
GetThing(TSomeImpl thingcreator) {
this.thingcreator = thingcreator;
}
T getThing() {
return thingcreator.get(offset);
}
TSomeImpl getOrigClass() {
return thingcreator;
}
}
不必要地重复参数字符串
。它是冗余的,但Java在这种情况下似乎需要它
有没有办法将泛型参数内部参数用作类型
我自己编的这个语法,有没有一个语法可以用于这个
// Pseudocode on refering to generic parameter's parameters
class GetThing<TSomeImpl extends SomeParamClass<T>, TSomeImpl::<T>> {
TSomeImpl thingcreator;
GetThing(TSomeImpl thingcreator) {
this.thingcreator = thingcreator;
}
TSomeImpl::<T> getThing() {
return thingcreator.get(offset);
}
TSomeImpl getOrigClass() {
return thingcreator;
}
}
//引用泛型参数的伪代码
阶级斗争{
TSomeImpl thingcreator;
GetThing(TSomeImpl thingcreator){
this.thingcreator=thingcreator;
}
TSomeImpl::getting(){
返回thingcreator.get(偏移量);
}
TSomeImpl getOrigClass(){
回归造物主;
}
}
这将不仅仅用于:
GetThing<TSomeImpl<String>>
getting
不需要其他参数
澄清:如何重新编写原始类,使其只有一个泛型参数,
List
,并推断出T
参数,因为它是从List
中明确知道的。问题是,您试图使用TList
作为某种别名。这不是泛型的用途
泛型类型参数就是:参数。无法隐藏参数。它们必须是明确的
这个怎么了?为什么它不能满足您的要求
class GetListEntry<T>
{
List<T> list;
GetListEntry(List<T> list) {
this.list = list;
}
T getValueAt(int offset) {
return list.get(offset);
}
}
类GetListEntry
{
名单;
GetListEntry(列表){
this.list=列表;
}
T getValueAt(整数偏移){
返回列表。获取(偏移量);
}
}
也许我不理解这个问题,但TList的作用是什么?看起来像是多余的定义。这样的工作不会:
public class ListOfSomething<T> {
private List<T> things;
public ListOfSomething(List<T> things) {
super();
this.things = things;
}
T getValueAt(int offset) {
return this.things.get(offset);
}
某事物的公共类列表{
私人物品清单;
某些事物的公共列表(列出事物){
超级();
这个东西=东西;
}
T getValueAt(整数偏移){
返回此.things.get(offset);
}
}我会参考迈克尔的答案。似乎没有明显的理由让你那样掩盖你的清单。但是,如果您真的想这样做,我相信您可以通过在变量声明中省略泛型参数来实现。唯一需要指定的地方是构造函数的参数。所以,只是使用
GetListEntry test = new GetListEntry(new ArrayList<String>());
GetListEntry test=newgetlistentry(newarraylist());
应该行得通
你可以试试我的测试代码。那是我试过的地方,效果很好
import java.util.List;
import java.util.ArrayList;
public class MyClass {
public static void main(String args[]) {
ArrayList<String> stringList = new ArrayList<String>();
stringList.add("1");
stringList.add("2");
stringList.add("3");
GetListEntry test = new GetListEntry(stringList);
for(int i = 0; i <= 2; i++)
System.out.println(test.getValueAt(i));
}
static class GetListEntry<T, TList extends List<T>> {
TList list;
GetListEntry(TList list) {
this.list = list;
}
T getValueAt(int offset) {
return list.get(offset);
}
}
}
import java.util.List;
导入java.util.ArrayList;
公共类MyClass{
公共静态void main(字符串参数[]){
ArrayList stringList=新建ArrayList();
stringList.添加(“1”);
添加(“2”);
添加(“3”);
GetListEntry测试=新建GetListEntry(stringList);
对于(inti=0;i这个呢
interface SomeType<T> {
T getT();
}
class SomeParamClass<T> implements SomeType<T> {
public T getT() {return null;}
}
class GetThing<T> {
SomeType<T> thingcreator;
GetThing(SomeType<T> thingcreator) {
this.thingcreator = thingcreator;
}
T getThing() {
return thingcreator.getT();
}
SomeType<T> getOrigClass() {
return thingcreator;
}
}
接口类型{
T getT();
}
类SomeParamClass实现了SomeType{
公共T getT(){return null;}
}
阶级斗争{
某种类型的创造者;
GetThing(SomeType thingcreator){
this.thingcreator=thingcreator;
}
T getting(){
返回thingcreator.getT();
}
SomeType getOrigClass(){
回归造物主;
}
}
你可以这样使用它
new GetThing<String>(new SomeParamClass<>());
newgetthing(newsomeParamClass());
您希望能够调用如下内容:new GetListEntry(new ArrayList)
?问题到底是什么?我还不清楚,TList::
?使用GetListEntry le=new GetListEntry(new ArrayList())有问题吗;
?@Eran它在任何定义或继承中都添加了冗余代码。我知道这里提到的两种可能的语法,但它们并不适用于所有地方。我不想用封闭的引用,因为我不确定我是否想在这方面使用我的dupehammer,但这个问题,如果不是完全重复的话,至少是非常密切相关的:有这与我使用的语法有很大区别。在您的版本中,列表的特定类型不再可见。这与要访问原始类型的情况有关。@TomášZato为什么您的任何代码都会关心正在使用哪个特定的列表实现?这听起来100%像坏design.Liskov替换原则。我在我的示例中仅使用列表作为示例。替代方法是在我的问题中包含更多的类和更多的文本,使其更难理解。@TomášZato更容易理解…以牺牲准确性为代价…uhh…无论如何,无论其是否为列表,原则都是相同的。如果y你关心的是具体类型,你的设计是错误的。你使用泛型的问题是你的设计错误的结果。有时候你需要访问原始代码类型<代码>列表>代码>。考虑一个泛型参数根本不是列表的例子,而是一些具有不同实现的自定义超级接口。.好吧,我想,即使你在另一篇评论中说需要更多的代码,你也应该拿出一个更清晰的例子。因为这篇文章并没有真正解释你想要达到的目标。你是对的,但我很困惑,为什么每个人都会试图通过一个只为最终目的而解决的解决方案来解决一个作为一般抽象问题提出的问题给出的示例,而不是关注一般解决方案(假设存在)@TomášZato如果每个人都以某种方式回答你的问题,那么假设你的问题就是问题,而不是每个人都回答它,这可能是个好主意。这也隐藏了SomeParamClass
@TomášZato的类型SomeParamClass
的类型是SomeType
,而且它没有隐藏。我不明白你为什么需要d是实现类,而不是接口。
new GetThing<String>(new SomeParamClass<>());