Java only returns methods of the same name with different value types, and their signatures are different. How can they exist at the same time in the same class?

    // new method
    protected String constructScalar(ScalarNode node) {
        return node.getValue();
    }

    // old method
    protected Object constructScalar(ScalarNode node) {
        return node.getValue();
    }

in an old program, the return value type in the old method, signature is Object, and the class in the dependent library is new. Only when new method, is called, the program call will report java.lang.NoSuchMethodError . How to insert both the new and old methods into it, so that the old program can be called correctly.

Mar.31,2021

java method overloading is not distinguished by return type, after all, if it is distinguished by return type, but we do not assign a return value to a variable, then the compiler is confused about which method to call? So just the return value is different, but the two methods cannot be stuffed into the same class. Your scenario description is not very clear, whether it can be solved by inheritance overwriting the old method.


the compiler cannot pass. Bytecode can exist because Java Source and JVM have different semantics. The way to solve similar problems is to use bridgeMethod bridge method to solve the semantic conflict between the two. The last sentence of the landlord indicates that he wants to cram the above two methods into a class, since the landlord knows that there are two methods for bytecode, and the compiler does not pass. Then the stuffed two methods into one class is to solve this problem in the compiler, then the problem is unsolvable, because this is the semantic rule of Java , and we can't break it. However, from the point of view of the landlord, I guess it is based on a if (condition) to decide whether to use the old version of the method or the new version of the method. Based on reflect , Class.getDeclaredMethod (String name,Class .paramterTypes) can only get the execution method based on the method name and parameter type, but it is obviously not satisfied in the current scenario, because there is still a lack of a judgment condition, that is, return type . Of course, you can also use getDeclaredMethod () to get all the Method , but there is a more convenient API , that is, MethodHandles . According to return type , we can acquire JVM cognition more accurately

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e50fe6-66a9a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e50fe6-66a9a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?