// 静态方法不可以重写,如果子类和父类如下时,它们如何分配地址的呢?
class A {
double f( double x, double y ) {
return x + y;
}
static int g( int n ) {            
return n * n;
}
}
class B extends A {
double f( double x, double y ) {
double m = super.f( x, y );
return m + x * y;
}
static int g( int n ) {                         //和父类A方法名一致。
int m = A.g( n );
return m + n;
}
}public class TheMain {
public static void main(String[] args) {
B b = new B( );
System.out.println( b.f( 10.0, 8.0 ) );
System.out.println( b.g( 3 ) );         //使用对象时,有黄点提示,使用类名时则没有。为什么?
}
}

解决方案 »

  1.   

    java语法如此规定,static方法和变量通过类名直接调用,如果通过对象实例调用会出现警告。
      

  2.   

    静态方法不能重写的。你用eclipse就可以看到连重写项里都没有。
      

  3.   

    虚拟机栈描述的是JAVA方法执行的内存模型:每个方法被执行的时候都会同时创建一个栈针用户存储局部秒了、操作栈、动态链接、方法出口等相关信息。
    从上面这句话可以看出,对于实力方法和静态方法并没有区分说明,那么意思就是都一样的。所以他们都一样的。都是在java栈中开辟空间用于存放关于方法的一些信息。
      

  4.   

    The static method g(int) from the type B should be accessed in a static way,翻译一下就明白了