简体   繁体   English

如何通过反射从父类调用子类方法

[英]How to invoke child class method from parent class through reflection

I want to create a menu which should be populated by arbitrary methods, which are marked by an annotation. 我想创建一个菜单,该菜单应由任意方法填充,并用注释标记。 The methods should be invoked from inside the base class. 这些方法应从基类内部调用。 Unfortunately 'java.lang.ClassCastException' is thrown since the method.invoke function expects an object which is instance of the child class. 不幸的是,抛出了“ java.lang.ClassCastException”,因为method.invoke函数需要一个作为子类实例的对象。 But i only get the base class. 但是我只得到基类。

Here is what i tried so far : 这是我到目前为止尝试过的:

public abstract Class BaseClass{

    private void invokeSomeMethod(){
        final Method[] methods= getClass().getDeclaredMethods();

        for (Method method : methods) {
            if (method.isAnnotationPresent(MenuFunction.class)) {
                MenuFunction menuFunction = method.getAnnotation(MenuFunction.class);
                menuFunction.invoke(this); //Throws 'java.lang.ClassCastException' 
            }
        }
    }

    @Retention(RetentionPolicy.RUNTIME)
    @Target({ METHOD })
    public @interface MenuFunction {
        String Label();
    }

}

public Class ChildClass extends BaseClass{

     @MenuFunction(Label = "First method")
    public void setHigh(){
      //Arbitrary function 
    }

    @MenuFunction(Label = "Another method")
    public void setLow(){
      //Do something
    }

}

I guess what you want to do is this: 我猜你想做什么是这样的:

public abstract class BaseClass { 公共抽象类BaseClass {

public void invokeSomeMethod() throws InvocationTargetException, IllegalAccessException {
    final Method[] methods = getClass().getDeclaredMethods();
    for (Method method : methods) {
        if (method.isAnnotationPresent(MenuFunction.class)) {
            MenuFunction menuFunction = method.getAnnotation(MenuFunction.class);
            method.invoke(this); //invoke method here'
        }
    }
}

} }

public class ChildClass extends BaseClass{

    @MenuFunction(Label = "hello")
    public void hello() {
        System.out.println("hello");
    }
    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {

        new ChildClass().invokeSomeMethod();
    }
}

Result: 结果:

hello 你好

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

相关问题 如何从具有泛型类型的抽象类中调用方法(使用Java反射) - How To invoke a Method (with java reflection) from abstract class with generic type 如何使用反射在Enum类上调用方法 - How to invoke method on Enum class using reflection 从子类外部访问父类的受保护方法(使用反射或任何可行的方法) - Accessing a parent class's protected method from outside a child class (with reflection or anything that would work) 当父类是引用类型并通过方法传递时,如何从子类访问属性? - How can I access an attribute from a child class when the parent class is the reference type and passed through a method? 如果我在父类中调用父类的方法,它是否会调用同名的子类方法 - If I invoke a parent's method in parent class, does it invoke child class method with the same name 如何从父级父类调用方法? - How can you invoke a method from a grand parent class? 从子类调用父类中的方法 - Call method in parent class from child class 在我的例子中,从子类中调用父抽象类中的函数 - Invoke function in parent abstract class from child class in my case 如何通过反射调用带有@transactional和@service批注的服务类中的方法? - How to invoke a method in a service class with @transactional and @service annotation by reflection? 在Java中使用反射调用Singleton类中的方法 - Invoke a method in Singleton class using reflection in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM