开发者

java automatic function calling

开发者 https://www.devze.com 2022-12-19 12:24 出处:网络
i have a math class that stores representation of expressions in n-array lists i currently have three classes AdditionArray MultipleArray and Variable they all implement my Number interface.

i have a math class that stores representation of expressions in n-array lists i currently have three classes AdditionArray MultipleArray and Variable they all implement my Number interface.

public interface Number {

  public Number Multiply(Number number);

in the classes that implement Number i have overloaded opertions like Multiply for example

public class MultipleArray extends ArrayList<Number> implements Number{
  public Number Multiply(AdditionArray number);
  public Number Multiply(Number number){throw new Exception("woops");}

the thing is java is not automatically calling the correct overloaded function at runtime. it seams to be figuring it out at compile time

f开发者_如何学Cor example

Number someNumber = new MultipleArray();
Number someOtherNumber = new AdditonArray();

MultipleArray result2 = someNumber.Multiply(someOtherNumber); //calls the correct function

Number result2 = someNumber.Multiply(someOtherNumber); // throws the woops exception

why is java doing this. and is there another way i can implement this. some sort of factory for example?

Cheers, Mark


In Java, overloading is a type of compile-time polymorphism, while overriding is a type of run-time polymorphism. See What is the difference between method overriding and method overloading in Java?.

This means that the compiler will always determine at compile time which of two overloaded methods to call, as opposed to overridden methods (which are methods with the same signature defined in the child as the ancestor class) which can be dynamically dispatched to based on the run-time type of the class on which the method is being called.


As Bill K said, Java determines this at compile time, and this is by design.

The really standard way to implement this is to have code in the overloaded method with the supertype that checks the type of the subclass and delegates with a cast or throws an exception as appropriate.

Using Generics is another option, which will improve your type safety, but I don't know enough about your code to say if it would work all around. A generic number interface might look like this:

 public interface Number<T extends Number> {
       public T multiply(Number<? extends T> number);
 }

Generics can get weird around the edges, so it might be worth it to just give up on the type safety and go with checking the type of the parameter.


I did not really look at your code, but just to respond to your primary question there--Java does figure out what to call at compile time based on the nearest type. If it did it at runtime, it would be a dynamic language.

Java is amazingly fast for what it does--but it gets a lot of it's speed by not thinking as much at runtime. It tends to be in the area of 10x faster than Python and 100x faster than Ruby.

If you want more flexibility in this, I suggest you don't use Java's bindings at all but create an "Operation" object and manually tie it to your "Numbers". In this way you can make a pretty cool algebraic tree out of objects. If nothing else it's fun--I've done it a few times. It'll still be darn fast.

0

精彩评论

暂无评论...
验证码 换一张
取 消