如何在java中继续使用泛型

发布于 2024-11-17 08:48:24 字数 628 浏览 5 评论 0原文

我有一个接口

public interface Doable<T,U> {
    public U doStuff(T t);
}

我有一个实现 Doable 的抽象类

public abstract class AbstractDoable<T,U> implements Doable<T, U> {
    private SomeClass someClass;

}

现在我需要实现上述类,但我需要为 T 使用不同的类U

请建议我应该如何继续,因为我需要使用不同的 TU 来实现 AbstractDoable 。例如:

ConvertDoable<String,Integer> extends AbstractDoable

ConvertDoable<A,B> extends AbstractDoable

I have a interface

public interface Doable<T,U> {
    public U doStuff(T t);
}

I have an abstract class which implements Doable<T,U>

public abstract class AbstractDoable<T,U> implements Doable<T, U> {
    private SomeClass someClass;

}

Now I need to implement the above classes, but I need to use different classes for T and U.

Please suggest how should I proceed in this as I will need to have multiple implemetations of AbstractDoable with different T and U. For example:

ConvertDoable<String,Integer> extends AbstractDoable

or

ConvertDoable<A,B> extends AbstractDoable

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

潦草背影 2024-11-24 08:48:24

您可以创建您想要的 DoableAbstractDoable 的任何子类。例如:

public class DoNothing implements Doable<String, String> {
    public String doStuff(String input) {
        return input;
    }
}

或者

public class DoSomething extends AbstractDoable<Integer, String> {
    public String doStuff(Integer i) {
        return i.toString();
    }
}

我不确定您的 someClass 成员在 AbstractDoable 中的用途是什么。你能解释一下它的用途吗?

You can create whatever subclasses of Doable or AbstractDoable you want. For example:

public class DoNothing implements Doable<String, String> {
    public String doStuff(String input) {
        return input;
    }
}

or

public class DoSomething extends AbstractDoable<Integer, String> {
    public String doStuff(Integer i) {
        return i.toString();
    }
}

I'm not sure what the purpose of your someClass member is in AbstractDoable. Can you explain what it is for?

强者自强 2024-11-24 08:48:24

如果您担心类膨胀,请考虑匿名类 - 根据需要“动态”定义的类:

public static void main(String[] args) {
    Doable<String, Integer> myDoable = new AbstractDoable<String, Integer>() {
        public Integer doStuff(String t) {
           return new Integer(t); // for example
        }
    }

    Integer i = myDoable.doStuff("0");
}

If you're worried about class bloat, consider anonymous classes - classes defined "on the fly" as you need them:

public static void main(String[] args) {
    Doable<String, Integer> myDoable = new AbstractDoable<String, Integer>() {
        public Integer doStuff(String t) {
           return new Integer(t); // for example
        }
    }

    Integer i = myDoable.doStuff("0");
}
深海里的那抹蓝 2024-11-24 08:48:24

您的具体实现应该在 AbstractDoable 上指定类型

public class ConvertDoable extends AbstractDoable<String,Integer>
{
    public Integer doStuff(String arg){...}
}

,或者继续传递参数化泛型:

public class ConvertDoable<T,U> extends AbstractDoable<T,U>
{
    public U doStuff(T arg){...}
}

Your concrete implementations should either specify the types on AbstractDoable

public class ConvertDoable extends AbstractDoable<String,Integer>
{
    public Integer doStuff(String arg){...}
}

or continue to pass along parameterized generics:

public class ConvertDoable<T,U> extends AbstractDoable<T,U>
{
    public U doStuff(T arg){...}
}
你列表最软的妹 2024-11-24 08:48:24
interface Doable<T,U> {
    public U doStuff(T t);
}

abstract class AbstractDoable<T,U> implements Doable<T, U> {
}

class ConvertDoableStringInteger extends AbstractDoable<String, Integer>{
    public Integer doStuff(String t) {
        return null;
    }   
}

class ConvertDoableIntergerFloat extends AbstractDoable<Integer, Float> {
    public Float doStuff(Integer t) {
        return 0.0f;
    }   
}
interface Doable<T,U> {
    public U doStuff(T t);
}

abstract class AbstractDoable<T,U> implements Doable<T, U> {
}

class ConvertDoableStringInteger extends AbstractDoable<String, Integer>{
    public Integer doStuff(String t) {
        return null;
    }   
}

class ConvertDoableIntergerFloat extends AbstractDoable<Integer, Float> {
    public Float doStuff(Integer t) {
        return 0.0f;
    }   
}
笑红尘 2024-11-24 08:48:24

关于泛型的整个想法是,您创建一个支持所有类型的类(或至少支持从类继承或实现接口的所有类)的单个类。

You之间没有区别,

GenericClass<A,B> foo;

同样在Java(字节码)中,say和

GenericClass<C,D> bar;

因此您需要定义一个类来处理可能提供给它的所有不同对象。

The whole idea about generics is that you make a single class which supports all types of classes (or at least all classes who inherit from a class or implement an interface).

Also in Java (bytecode) there is no difference between say

GenericClass<A,B> foo;

and

GenericClass<C,D> bar;

You thus need to define a single class that can handle all the different objects that might be given to it.

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