静态工厂方法中的泛型? (爪哇)

发布于 2024-09-25 01:10:38 字数 646 浏览 5 评论 0原文

我有一个作业要求我使用工厂模式来实现不可变的数据结构,但问题是抽象类是泛型的,并且静态方法引用泛型类型给我带来了问题。我的作业要求我使用静态方法,所以我开始恐慌。有什么帮助/建议吗? 编辑添加了一些示例代码,这是教授给我们的方法之一的规范 签名:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陈独秀 2024-10-02 01:10:38

我不知道你到底想做什么,但我们假设问题是你只是在寻找正确的语法:

public class ExampleClass<T> {
    public static <T> Class<T> method1(T x, ExampleClass<T> c) {
        return c.method3(x);
    }
    public static <T> T method2(ExampleClass<T> c) {
        return c.method2Dynamic();
    }
    private Class<T> method3(T x) {
        return null;
    }
    private T method2Dynamic() {
        return null;
    }
}

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:

public class ExampleClass<T> {
    public static <T> Class<T> method1(T x, ExampleClass<T> c) {
        return c.method3(x);
    }
    public static <T> T method2(ExampleClass<T> c) {
        return c.method2Dynamic();
    }
    private Class<T> method3(T x) {
        return null;
    }
    private T method2Dynamic() {
        return null;
    }
}
ˉ厌 2024-10-02 01:10:38

有关泛型方法(而不是到泛型类)。

从到目前为止的描述来看,静态方法与实例方法的问题不应该在这里发挥作用。

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.

偏爱你一生 2024-10-02 01:10:38

如果您有一个泛型类,则不能在静态方法中使用类型参数,因为它们在那里没有意义。例如,考虑ArrayList。您可以创建一个 ArrayList; stingList = new ArrayList() 和一个 ArrayList; 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 a ArrayList<String> stingList = new ArrayList<String>() and a ArrayList<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 like get. But static methods belong to the class not the instance, so when you call a static method you call it like ArrayList.staticMethod() NOT stringList.staticMethod() or integerList.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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文