amelentev / java-oo

Java Operator Overloading
http://amelentev.github.io/java-oo/
268 stars 31 forks source link

Operator overloading is not supported for sub-class in Intellij IDE? #31

Closed yuemingl closed 8 years ago

yuemingl commented 9 years ago

Is there any one tried it? I just started to use Intellij. If it is true it would be a big problem. I use eclipse before. Java-oo works good for sub-classes in eclipse.

yuemingl commented 9 years ago
public class MyInteger extends BigInteger {
    public MyInteger(String val, int radix) {
        super(val, radix);
    }
}

public class Main {
    public static void main(String[] args) {
        BigInteger a = 1, b = 2;
        BigInteger c = a + b;
        System.out.println(c);

        MyInteger aa = 1, bb =2;
        MyInteger cc = aa + bb; //Error here
        System.out.println(cc);
    }
}
msteinbeck commented 8 years ago

I can confirm this bug. But its not a problem of IntelliJ itself. Compiling this code in terminal also results in a compiler error. Actually the assignment of 1 and 2 to aa and bb is unsupported.

On the other hand the following code works without any problems:

import java.util.HashMap;

class MyMap extends HashMap<Integer, String> {}

public class Main {

    public static void main(String[] args) {
        MyMap m = new MyMap();
        m[0] = "Testing Java-OO";
        m[1] = m[0];
        System.out.println(m[0]);
        System.out.println(m[1]);
    }
}
msteinbeck commented 8 years ago

After some investigation I found the following things:

MyInteger aa = 1, bb =2;

does not compile. You have to create the method valueOfby your own. For example you could do the following

public static class MyInteger extends BigInteger {
    public MyInteger(String val) {
        super(val);
    }

    public static MyInteger valueOf(long val) {
        final BigInteger bi = BigInteger.valueOf(val);
        return new MyInteger(bi.toString());
    }
}
public MyInteger add(BigInteger val) {
    BigInteger bi = super.add(val);
    return new MyInteger(bi.toString());
}

Note: I used BigInteger as parameter type for val to be more flexible in mixing types.

@amelentev I think this issue can be closed.

amelentev commented 8 years ago

Thanks for investigation, @retuxx