不带参数的通用方法 [英] Generic method with no parameter
问题描述
我创建了一个不带参数的泛型方法,其中包含以下内容:
private< T>的TableCell< T> createTableCell(){
return new TableCell< T>();
$ b 那么,在我的程序中,如何为具体类型调用此方法?
解决方案通常情况下,类型是推断的,但您可以使用以下语法指定类型:
注意:您的方法定义中有一个错误 - 它没有返回类型:
private< ; T>的TableCell< T> createTableCell(){
return new TableCell< T>();
$ / code>
以下是您可以称之为的方式:
TableCell< SomeType> tableCell = myObject。< SomeType> createTableCell();
如果你的方法没有访问任何字段,考虑使它一个静态
方法,您可以这样称呼它:
TableCell< SomeType> ; tableCell = MyClass。< SomeType> createTableCell();
另外,当你使用这种语法时,许多人会惊叹于在你的精英 - 这是一个不常见的语法。
I create a generic method without parameter, some thing like:
private <T> TableCell<T> createTableCell(){
return new TableCell<T>();
}
So, in my program, how to call this method for a concrete type?
解决方案 Usually, the type is inferred, but you can specify the type with this syntax:
Note: You have an error in your method's definition - it had no return type:
private <T> TableCell<T> createTableCell(){
return new TableCell<T>();
}
Here's how you can call it:
TableCell<SomeType> tableCell = myObject.<SomeType>createTableCell();
If you method doesn't access any fields, consider making it a static
method, which you would call like:
TableCell<SomeType> tableCell = MyClass.<SomeType>createTableCell();
As an aside, when you use this syntax, many will marvel at your "eliteness" - it's a syntax not often seen.
这篇关于不带参数的通用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!