Java 泛型/类型调度问题

发布于 2024-09-25 01:45:02 字数 1677 浏览 6 评论 0原文

考虑以下程序:

import java.util.List;
import java.util.ArrayList;

public class TypeTest {

    public static class TypeTestA extends TypeTest {

    }

    public static class TypeTestB extends TypeTest {

    }

    public static final class Printer {
        public void print(TypeTest t) {
            System.out.println("T");
        }

        public void print(TypeTestA t) {
            System.out.println("A");
        }

        public void print(TypeTestB t) {
            System.out.println("B");
        }

        public <T extends TypeTest> void print(List<T> t) {
            for (T tt : t) {
                print(normalize(tt.getClass(), tt));
            }
        }

        private static <T> T normalize(Class<T> clz, Object o) {
            return clz.cast(o);
        }

    }
    public static void main(String[] args) {
        Printer printer = new Printer();
        TypeTest t1 = new TypeTest();
        printer.print(t1);
        TypeTestA t2 = new TypeTestA();
        printer.print(t2);
        TypeTestB t3 = new TypeTestB();
        printer.print(t3);
        System.out.println("....................");
        List<TypeTestB> tb1 = new ArrayList<TypeTestB>();
        tb1.add(t3);
        printer.print(tb1);
    }
}

main 方法现在打印:

T  
A  
B  
....................  
T  

我应该做什么才能让它打印以下内容?

T  
A  
B  
....................  
B  

我想避免为每种可以打印的类型编写如下循环:

   public void printTypeTestB(List<TypeTestB> t) {
        for (TypeTestB tt : t) {
            print(tt);
        }
    }

Consider the following program:

import java.util.List;
import java.util.ArrayList;

public class TypeTest {

    public static class TypeTestA extends TypeTest {

    }

    public static class TypeTestB extends TypeTest {

    }

    public static final class Printer {
        public void print(TypeTest t) {
            System.out.println("T");
        }

        public void print(TypeTestA t) {
            System.out.println("A");
        }

        public void print(TypeTestB t) {
            System.out.println("B");
        }

        public <T extends TypeTest> void print(List<T> t) {
            for (T tt : t) {
                print(normalize(tt.getClass(), tt));
            }
        }

        private static <T> T normalize(Class<T> clz, Object o) {
            return clz.cast(o);
        }

    }
    public static void main(String[] args) {
        Printer printer = new Printer();
        TypeTest t1 = new TypeTest();
        printer.print(t1);
        TypeTestA t2 = new TypeTestA();
        printer.print(t2);
        TypeTestB t3 = new TypeTestB();
        printer.print(t3);
        System.out.println("....................");
        List<TypeTestB> tb1 = new ArrayList<TypeTestB>();
        tb1.add(t3);
        printer.print(tb1);
    }
}

The main method now prints:

T  
A  
B  
....................  
T  

What should I do to make it print the followings?

T  
A  
B  
....................  
B  

I'd like to avoid writing a loop such as the following for each of the type that can be printed:

   public void printTypeTestB(List<TypeTestB> t) {
        for (TypeTestB tt : t) {
            print(tt);
        }
    }

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

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

发布评论

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

评论(3

奈何桥上唱咆哮 2024-10-02 01:45:02

问题的根源在于 Java 方法重载是在编译时根据方法参数表达式的声明类型来解决的。您的程序似乎正在尝试使用运行时分派到不同的方法重载。这在 Java 中根本行不通。

您在示例中使用泛型的事实有点转移注意力。如果将类型参数 替换为 TypeTest,也会遇到同样的问题。

The root of your problem is that Java method overloads are resolved at compile time based on the declared type of the method argument expressions. Your program seems to be trying to use runtime dispatching to different method overloads. That simply doesn't work in Java.

The fact that you are using generics in your example is a bit of a red herring. You would have the same problem if you replaced the type parameter <T> with TypeTest.

似梦非梦 2024-10-02 01:45:02

考虑创建一个了解所有相关子类型的访问者界面。

public class TypeTestFoo {

interface TypeTestVisitor {
    void visit(TypeTestA t);
    void visit(TypeTestB t);
    void visit(TypeTest t);
}

interface TypeTest {
    void accept(TypeTestVisitor visitor);
}

public static class TypeTestA implements TypeTest {
    public void accept(TypeTestVisitor visitor) {
        visitor.visit(this);
    }
}

public static class TypeTestB implements TypeTest {
    public void accept(TypeTestVisitor visitor) {
        visitor.visit(this);
    }
}

public static final class Printer implements TypeTestVisitor {
    public void visit(TypeTestA t) {
        System.out.println("A");
    }

    public void visit(TypeTestB t) {
        System.out.println("B");
    }

    public void visit(TypeTest t) {
        System.out.println("T");
    }

}
public static void main(String[] args) {
    Printer printer = new Printer();
    TypeTest t1 = new TypeTest() {
        public void accept(TypeTestVisitor visitor) {
            visitor.visit(this);
    }};
    t1.accept(printer);    
    TypeTestA t2 = new TypeTestA();
    t2.accept(printer);
    TypeTestB t3 = new TypeTestB();
    t3.accept(printer);
    System.out.println("....................");
    List<TypeTestB> tb1 = new ArrayList<TypeTestB>();
    tb1.add(t3);
    for (TypeTestB each : tb1) {
        each.accept(printer);
    }
}

应该打印出您想要的内容:

T
A
B
....................
B

类型在允许编译时重载的接口中列出。另一方面,这是您放置不想参数化行为的子类型的单点。 Java 不是一种非常动态的语言...:)

Concider creating a visitor interface which knows about all relevant subtypes.

public class TypeTestFoo {

interface TypeTestVisitor {
    void visit(TypeTestA t);
    void visit(TypeTestB t);
    void visit(TypeTest t);
}

interface TypeTest {
    void accept(TypeTestVisitor visitor);
}

public static class TypeTestA implements TypeTest {
    public void accept(TypeTestVisitor visitor) {
        visitor.visit(this);
    }
}

public static class TypeTestB implements TypeTest {
    public void accept(TypeTestVisitor visitor) {
        visitor.visit(this);
    }
}

public static final class Printer implements TypeTestVisitor {
    public void visit(TypeTestA t) {
        System.out.println("A");
    }

    public void visit(TypeTestB t) {
        System.out.println("B");
    }

    public void visit(TypeTest t) {
        System.out.println("T");
    }

}
public static void main(String[] args) {
    Printer printer = new Printer();
    TypeTest t1 = new TypeTest() {
        public void accept(TypeTestVisitor visitor) {
            visitor.visit(this);
    }};
    t1.accept(printer);    
    TypeTestA t2 = new TypeTestA();
    t2.accept(printer);
    TypeTestB t3 = new TypeTestB();
    t3.accept(printer);
    System.out.println("....................");
    List<TypeTestB> tb1 = new ArrayList<TypeTestB>();
    tb1.add(t3);
    for (TypeTestB each : tb1) {
        each.accept(printer);
    }
}

}

This should print out what you wanted:

T
A
B
....................
B

The types are listed in the interface which allows for compile-time overloading. On the other hand, this is a single point where you have put the subtypes for which you wan't to parameterize behavior. Java is not a very dynamic language... :)

橘虞初梦 2024-10-02 01:45:02

复杂的“访客模式”的候选者。

或者简单地将 print() 方法从 Printer 移至 TypeTest

candidate for the convoluted "visitor pattern".

or simply move print() method from Printer to TypeTest

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