使用反射调用没有其实例的超类方法 [英] Invoking super class method without its instance using reflection

查看:53
本文介绍了使用反射调用没有其实例的超类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下演示继承和反射的代码:

Please consider the below code demonstrating inheritance and reflection :

    /*Parent class*/

    package basics;

    public class Vehicle {

        private void parentPrivateMethod() {
            System.out.println("This is the child private method");
        }

        public void print() {
            System.out.println("This is a Vehicle method");
        }

        public void overrideThisMethod() {
            System.out.println("Parent method");
        }

    }


    /*Child class*/

    package basics;

    public class Car extends Vehicle {

        private void childPrivateMethod() {
            System.out.println("This is the child private method");
        }

        public String returnCarName() {
            return "Manza";
        }

        @Override
        public void overrideThisMethod() {
            //super.overrideThisMethod();/*NOTE THIS*/
            System.out.println("Child method");
        }

    }


    /*Tester class*/
    package basics;

    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;

    public class NewTester {

        /**
         * @param args
         * @throws NoSuchMethodException
         * @throws SecurityException
         * @throws InvocationTargetException
         * @throws IllegalAccessException
         * @throws IllegalArgumentException
         * @throws InstantiationException
         */
        public static void main(String[] args) throws SecurityException,
                NoSuchMethodException, IllegalArgumentException,
                IllegalAccessException, InvocationTargetException, InstantiationException {
            // TODO Auto-generated method stub

            Car carInstance = new Car();

            /* Normal method invocation */
            carInstance.overrideThisMethod();

            /* Reflection method invocation */
            Method whichMethod = Car.class.getSuperclass().getMethod(
                    "overrideThisMethod", null);
            whichMethod.invoke(carInstance, null);

            /* Work-around to call the superclass method */
            Method superClassMethod = Car.class.getSuperclass().getMethod(
                    "overrideThisMethod", null);
            superClassMethod.invoke(Car.class.getSuperclass().newInstance(), null);
        }

    }

输出(注释了注意这个"部分)是:

The output(with 'NOTE THIS' part commented) is :

        Child method
        Child method
        Parent method

如果'NOTE THIS'部分没有被注释,超类方法将被调用,给出一个输出:

In case, the 'NOTE THIS' part is un-commented, the superclass method will be invoked, giving an output :

        Parent method
        Child method
        Parent method
        Child method
        Parent method

创建 Car 的实例时,首先运行 Vehicle 的构造函数.因此,我相信,也创建了一个 Vehicle 实例,Car 实例通过super"保存了它的引用.

When an instance of Car is created, the constructor of Vehicle runs first. Hence, I believe, that an instance of Vehicle is created,too, whose reference the Car instance holds via 'super'.

问题:如何在不使用/* Work-around 调用超类方法 */的情况下调用overrideThisMethod"的超类版本?

Question : How can I invoke the superclass version of 'overrideThisMethod' without using the /* Work-around to call the superclass method */ ?

我是否在这里忽略了某些东西/在这里做出了错误的假设?

Am I overlooking something here/making wrong assumptions here?

推荐答案

创建 Car 的实例时,首先运行 Vehicle 的构造函数.因此,我相信,也创建了一个 Vehicle 实例,Car 实例通过super"保存了它的引用.

When an instance of Car is created, the constructor of Vehicle runs first. Hence, I believe, that an instance of Vehicle is created,too, whose reference the Car instance holds via 'super'.

这不太正确;只创建一个对象.Vehicle 的构造函数运行是因为它是 Vehicle 的实例,也是 Car 的实例.没有单独的 Vehicle 对象,并且 super 不是对象引用而是关键字.例如,您不能将 super 作为参数传递给方法.

This is not quite correct; only one object is created. The constructor of Vehicle runs because it is an instance of Vehicle as well as of Car. There is no separate Vehicle object, and super is not an object reference but a keyword. For example you can't pass super to methods as an argument.

super 关键字允许您告诉编译器您要从超类调用方法,而无需检查该方法是否在子类中被覆盖.为此,编译器使用invokespecial JVM 指令生成方法调用.通常编译器会发出一个 invokevirtual 指令.

The super keyword allows you to tell the compiler that you want to call a method from the super class without checking if the method is overridden in subclasses. In order to do this the compiler generates the method call with the invokespecial JVM instruction. Normally the compiler would emit an invokevirtual instruction.

这篇关于使用反射调用没有其实例的超类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆