public class Y extends X
{
int i = 0;
public int m_Y(int j){
return i + 2 *j;
}
}
public class X
{
int i = 0 ;
public int m_X(int j){
return i + j;
}
}
public class Testing
{
public static void main()
{
X x1 = new X();
X x2 = new Y(); //this is the declare of x2
Y y2 = new Y();
Y y3 = (Y) x2;
System.out.println(x1.m_X(0));
System.out.println(x2.m_X(0));
System.out.println(x2.m_Y(0)); //compile error occur
System.out.println(y3.m_Y(0));
}
}
为什么那一行会有编译错误?我将x2声明为Y类的一个实例,应该能够调用类Y的所有函数,但在blueJ中为什么它显示
" cannot find symbol - method m_Y(int)"
m_Y
未为X
所定义。在命名方法等时,请使用Java命名约定。 - Reimeusx2
声明为类型为X
的变量。Java 是一种强类型语言。 - Nir Alfasi