返回泛型类 [英] returning generic class
问题描述
当遇到与此类似的代码时,我遇到了编译错误无法将类型T隐式转换为T ...:
class A< T>
{
private T [] stuff;
public A( int n){stuff = 新 T [n]; }
public T get ( int i){ return stuff [i]; }
}
class B< T>
{
private A< T>事情;
public B(){things = new A< T>( 10 跨度>); }
public T get< T>(){ return thing。 get ( 1 ); }
}
错误在 things.get(1)
上面。
如果B的获取不是通用的,则错误消失:
public T get (){ return thing。 get ( 1 )};
但是,我确实需要在这里使用通用方法。
非常感谢任何建议/帮助。
谢谢。
问题是你正在重新定义一个新的T型,而不是一个在班级宣布。尝试使用一个不太通用的:
class A< T>
{
private T [] stuff;
public A( int n){stuff = 新 T [n]; }
public T get ( int i){ return stuff [i]; }
}
class B< T>
{
private A< T>事情;
public B(){things = new A< T>( 10 跨度>); }
public T get (){ return 事物。 get ( 1 ); }
}
我在我的问题中提到这个解决方案,就像我说的,我需要使用泛型方法。所以本质上A类在B之外有其他用途,所以我需要将它分开。由于A的数据,T [] stuff,私有B必须调用它的'公共方法。说,泛型类中的泛型方法可以调用返回类型T的内部类中的方法吗?
B.Get
已经是一个泛型方法,它在实例化时从B
的类定义继承其泛型类型。如果您尝试向其添加第二个泛型,那么T
的新B.Get
- 特定声明将掩盖B-class
T
的特定声明,编译器(正确地)假定它是不同的类型 - 因为你可以否则合法地说
B< int> b = new B< int>();
string s = b.Get< string>();
因为你不能添加任何类型的约束A.Get
或B.Get
可以返回,编译器不允许你掩码T
并返回未屏蔽的类型 - 因为它无法保证它们之间的任何转换。
在VS中尝试使用代码为我展示了:
A< int> a = new A< int>( 7 );
int i = a。 get ( 3 跨度>);
B< int> b = new B< int>();
string s = b。 get ();
VS会抱怨你无法将int
转换为字符串
,并且Intellisense将自动显示b
类型为B< int>
。
I am running into a compiler error "cannot implicitly convert type T to T..." when I have code similar to this:
class A<T>
{
private T[] stuff;
public A(int n) { stuff = new T[n]; }
public T get(int i) { return stuff[i]; }
}
class B<T>
{
private A<T> things;
public B() { things = new A<T>(10); }
public T get<T>() { return things.get(1); }
}
error is on things.get(1)
above.
The error goes away if B's get is not generic:
public T get(){ return things.get(1)};
However, I do have a need to use a generic method here.
any suggestions/help is greatly appreciated.
Thanks.
The problem is that you are are redefining a new T type separate from the one declared on the class level. Try with one less generic:
class A<T> { private T[] stuff; public A(int n) { stuff = new T[n]; } public T get(int i) { return stuff[i]; } } class B<T> { private A<T> things; public B() { things = new A<T>(10); } public T get() { return things.get(1); } }
"I mention this solution in my question and as I say, I have a need to use a generic method. So in essence class A has other uses outside of B and so I need to keep it separate. Since A's data, T[] stuff, is private B has to call its' public method. Generally speaking can a generic method in a generic class call a method in the inner class that returns type T?"
B.Get
is already a generic method that inherits its generic type from the class definition ofB
when it is instantiated. If you try to add a second generic to it, then the newB.Get
-specific declaration ofT
masks theB-class
-specific declaration ofT
and the compiler (rightly) assumes that it's a different type - because you can otherwise legitimately say
B<int> b = new B<int>(); string s = b.Get<string>();
Because you can't add any constraints on what typeA.Get
orB.Get
can return, the compiler won't allow you to maskT
and return the unmasked type - because it can't guarantee any conversion between them is possible.
Try it in VS with the code as I showed:
A<int> a = new A<int>(7); int i = a.get(3); B<int> b = new B<int>(); string s = b.get();
VS will complain you can't convert theint
to astring
, and Intellisense will displayb
as being of typeB<int>
automatically.
这篇关于返回泛型类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!