【发布时间】:2015-07-10 13:48:02
【问题描述】:
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 命名约定。 -
您将
x2声明为X类型。 Java 是strongly typed language。
标签: java inheritance polymorphism subclass superclass