由于某种原因,我似乎无法在定义它的包之外实现抽象类。package1 中的抽象类不能在 package2 中的类中实现。为什么这不是合法的 Java?
package com.stackoverflow.abstraction.package1;
abstract public class BaseClass {
abstract Long foo();
}
package com.stackoverflow.abstraction.package1;
public class Implement1 extends BaseClass {
@Override
Long foo() {
return null;
}
}
package com.stackoverflow.abstraction.package2;
import com.stackoverflow.abstraction.package1.BaseClass;
/** Compiling this class will output
* - Implement2 is not abstract and does not override abstract method foo() in BaseClass
* - error: method does not override or implement a method from a supertype
*/
public class Implement2 extends BaseClass {
@Override
Long foo() {
return null;
}
}
运行:OS X 10.6.8 - Java(TM) SE Runtime Environment (build 1.6.0_31-b04-415-10M3646) - OpenJDK Runtime Environment (build 1.7.0-u4-b13-20120301) 尝试了两个 Java 版本。当然不是同时 :)