静态工厂方法中的泛型? (爪哇)
我有一个作业要求我使用工厂模式来实现不可变的数据结构,但问题是抽象类是泛型的,并且静态方法引用泛型类型给我带来了问题。我的作业要求我使用静态方法,所以我开始恐慌。有什么帮助/建议吗? 编辑添加了一些示例代码,这是教授给我们的方法之一的规范 签名:
ExampleClass.method1:ExampleClass,T ->示例类
ExampleClass.method2:示例类-> 我所拥有的两种
public abstract class ExampleClass<T>{
//static method creates a new subclass of Example ("Push" method)
public static Class method1(T x, ExampleClass c){
return new method1(x, f);
}
//Supposed to return an object type T ("pop" method)
public static T method2(ExampleClass c){
return c.method2Dynamic();
}
方法都在 Eclipse 中给我带来了问题。
I have an assignment that is requiring me to use the factory pattern for implementing an immutable data structure, but the problem is that the abstract class is generic, and having static methods make references to generic types is giving me problems. My assignment is requiring me to use static methods so I'm starting to panic. Any help/suggestions?
EDIT added some sample code, and here is the specification for one of the methods the professor gave us
Signature:
ExampleClass.method1 : ExampleClass, T -> ExampleClass
ExampleClass.method2 : ExampleClass - > T
public abstract class ExampleClass<T>{
//static method creates a new subclass of Example ("Push" method)
public static Class method1(T x, ExampleClass c){
return new method1(x, f);
}
//Supposed to return an object type T ("pop" method)
public static T method2(ExampleClass c){
return c.method2Dynamic();
}
both of the methods I have like these are giving me problems in eclipse.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道你到底想做什么,但我们假设问题是你只是在寻找正确的语法:
I don't know what you actually want to do, but let's suppose the problem is you are just looking for the right syntax:
有关泛型方法(而不是到泛型类)。
从到目前为止的描述来看,静态方法与实例方法的问题不应该在这里发挥作用。
See the Java tutorial for help with generic methods (as opposed to generic classes).
From your description thus far, the static method vs. instance method question shouldn't come into play here.
如果您有一个泛型类,则不能在静态方法中使用类型参数,因为它们在那里没有意义。例如,考虑; stingList = new ArrayList() 和一个
ArrayList
。您可以创建一个 ArrayListArrayList; integerList = new ArrayList
.因此,现在您必须拥有 ArrayList 实例,每个实例都有自己的参数类型,以及可以利用该参数类型(如get
)的实例方法。但静态方法属于类而不是实例,因此当您调用静态方法时,您可以像ArrayList.staticMethod()
那样调用它,而不是stringList.staticMethod()
或integerList.staticMethod()
(您也可以这样做,但这实际上没有意义,因为静态方法无法访问实例变量,因此它与在类上调用它执行的操作完全相同) 。但是当你在类上调用它时,该类只是 ArrayList,没有任何类型参数,因为类型参数仅由实例使用。但是,您可以使用具有自己的类型参数的方法,该参数独立于类的类型参数,就像托马斯在他的回答中所示的那样。因此,您可以调用这些方法,例如
ExampleClass.;静态方法()
;请注意,ExampleClass 这里没有类型参数,但该方法有它。 (如果编译器可以从使用的参数或返回类型推断出方法调用中的
,则可以省略它:String s = ExampleClass.method2(new ExampleSubclass;())
; 它通常可以很好地推断)If you have a generified class, you can't use the type parameters in static methods since they don't make sense there. For example, consider
ArrayList<T>
. You can create aArrayList<String> stingList = new ArrayList<String>()
and aArrayList<Integer> integerList = new ArrayList<Integer>
. So now you have to instances of ArrayList, each with their own parameter type, and instance methods that can take advantage of that parameter type likeget
. But static methods belong to the class not the instance, so when you call a static method you call it likeArrayList.staticMethod()
NOTstringList.staticMethod()
orintegerList.staticMethod()
(you can do that as well, but it doesn't really make sense, since the static method cannot access instance variables, so it does the exact same thing as calling it on the class). But when you call it on the class, the class is just ArrayList, without any type parameters, since the type parameters are only used by instances.You can however have methods that have their own type parameter, which is independent of the type parameter of the class, like Thomas shows in his answer. So you can then call those method like
ExampleClass.<String> staticMethod()
; notice that ExampleClass has no type parameter here, but the method does have it. (you can omit the<String>
in the method call if the compiler can infer it from the parameters used, or the return type:String s = ExampleClass.method2(new ExampleSubclass<String>())
; it usually does a pretty good job at inferring it)