0
我想能够访问从抽象类的子类实例化的对象。这是一个例子。如何访问从java的抽象类的子类实例化的对象?
// A.java
public abstract class A {
public int getNewNo() {
int newNo = <Instance of B in this example here>.getNo() + 2;
return newNo;
}
}
// B.java
public class B extends A {
public int getNo() {
return 2;
}
}
// C.java
public class C {
public C(A a) {
System.out.println("The number is "+a.getNewNo());
}
}
// example.java
public void main(String args[]) {
B b = new B();
C c = new C(b);
// should now print out "The number is 4"
}
这可能吗?
谢谢。
你在几秒钟内击败我:) – Reddy
@Reddy而你有了复选标记。多么世界,多么世界:) – sje397