Closed yuemingl closed 8 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);
}
}
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]);
}
}
After some investigation I found the following things:
valueOf
of BigInteger is a static method (obviously) and thus will not be derived to MyInteger.
Thats the reason whyMyInteger aa = 1, bb =2;
does not compile. You have to create the method valueOf
by 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());
}
}
add
of BigInteger returns a BigInteger. As a result the method add
of MyInteger also returns a BigInteger and thus is not assignable to aa
which is a MyInteger. Like valueOf
you have to create your own add
method, for example: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.
Thanks for investigation, @retuxx
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.