简体   繁体   English

使用类型变量泛型的调用方法

[英]Call method using type variable generics

I have a scenario that i want to create one support class called D which contains a generic method. 我有一个场景,我想创建一个名为D的支持类,其中包含一个通用方法。 I have set the upper bound for type variable. 我已经为类型变量设置了上限。

class A{
    void show(){
        System.out.println("Hello A");
    }
}

class B extends A{
    void msg(){
        System.out.println("Hello B");
    }
}

class C extends A{
    void msg(){
        System.out.println("Hello C");
    }
}

class D{
    <T extends A> void display(T ob){
        ob.msg(); //here i want to do some tricks
    }
}

First i want to share my objective. 首先,我想分享我的目标。 Here msg() function of B and C class has different implementations. 这里B和C类的msg()函数有不同的实现。 I want to create one support class called D that has one display method, using display method i want to call either msg() function of B or C class dependent on instantiation. 我想创建一个名为D的支持类,它有一个显示方法,使用display方法我想调用依赖于实例化的B或C类的msg()函数。 Can you tell me how can i achieve it? 你能告诉我怎样才能实现它?

You need to have the method msg() in class A , otherwise the display() method in class D does not know if this method exist or not in the object that you're passing to it. 你需要在类A使用方法msg() ,否则类D中的display()方法不知道这个方法是否存在于你传递给它的对象中。 (What if someone makes a class E that extends A but does not have a msg() method, and you pass an E to D.display() ?). (如果有人创建了一个扩展A但没有msg()方法的类E ,并且将E传递给D.display() ?)。

If you don't want to implement the msg() method in class A , then you can make it abstract (and you'll also have to make the class abstract ). 如果您不想在A类中实现msg()方法,那么您可以将其设为abstract (并且您还必须使该类abstract )。

abstract class A {
    public abstract void msg();

    // ...
}

more like an architecture style, I would use an interface for that, so your generic method constrains to <T extends If> void display(T ob) where If is the interface with the abstract method msg 更像是一种架构风格,我会使用一个接口,所以你的泛型方法约束到<T extends If> void display(T ob)其中If是与抽象方法msg的接口

interface If {
    void msg();
}

class A {
    void show() {
        System.out.println("Hello A");
    }
}

class B extends A implements If {
    @Override
    public void msg() {
        System.out.println("Hello B");
    }
}

class C extends A implements If {
    @Override
    public void msg() {
        System.out.println("Hello C");
    }
}

class D {
    <T extends If> void display(T ob) {
        ob.msg(); // here i want to do some tricks
    }
}

You don't need generics for this, there is basic concept called dynamic binding in Java. 你不需要泛型,在Java中有一个叫做动态绑定的基本概念。

abstract class A{
    void show(){
        System.out.println("Hello A");
    }       
     abstract void msg();    
}

class B extends A{
    @Override
    void msg(){
        System.out.println("Hello B");
    }
}

class C extends A{

       @Override
       void msg(){
             System.out.println("Hello C");
       }
}

class D{
    void display(A ob){
        ob.msg(); 
    }
}

Here an appropriate instance provided to method will determine which class method should in called at runtime. 这里为方法提供的适当实例将确定在运行时调用哪个类方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM