x = new Child();??编译不过啊

解决方案 »

  1.   

    Parent x = new Parent();
    x.someMethod();
    x = new Child();
    x.someMethod();
    java没有你想想得那么聪明
    你的x 定义成Parent类型
    你即使x = new Child();赋值
    类型还是Parent类 自动转换的
    你调用x.someMethod();
    当然是Parent类的方法了你想打出Child someMethod()");
    可以用
    ((Child)x).someMethod();
      

  2.   

    不同意楼上的,问题出在那个static上,你把static去掉看一下结果~
      

  3.   

    这是一个DownCasting 的问题,在你的这个例子里面,无法实现DownCasting,也就是向下转型,只能定义子类,然后转型到父类,所以输出结果不对。Parent x = new Parent();
    x.someMethod();
    Child    y = new Child();
    y.someMethod();这样不用转型,就可以了
      

  4.   

    jokerjava(冷血) 说的不对,
    Parent x = new Parent();
    x.someMethod();
    x = new Child();
    x.someMethod();这是典型的多态性和动态绑定的例子!!!
    在执行someMethod()方法的时候,解释器会问x所指向的对象是什么类型,当然x是指向Child的。
    问题是出在使用的static。
    把static去掉就能得到你想要的结果了。
      

  5.   

    都是static惹祸,
    你去掉static试试,结果就是你想要的了
      

  6.   

    嗯,比我快
    再加一句,
    动态绑定对static无效
      

  7.   

    同意accp(accp) 
    我说错了不知道为什么
      

  8.   

    class Parent
    {
    void someMethod() {
    System.out.println("Parentis someMethod()");
    }
    }class Child extends Parent 
    {
    void someMethod() {
    System.out.println("child someMethod()");
    }
    }
    class aaa{
    public static void main (String[] args)
      {
    Parent x = new Parent();
    x.someMethod();
    x = new Child();;
    x.someMethod();
      }
    }
    结果是
    Parentis someMethod()");
    Child someMethod()");那个static 不可以加
      

  9.   

    动态绑定对static无效to Hodex(小何才露尖尖角) 
    为什么啊
      

  10.   

    你看看相关文档就知道了,static的是类被编译的时候就在类上做的静态绑定的,是编译时确定的入口点,
    她是不能被覆盖的。动态方法上在类的实例对象构造的时候动态棒定的,是运行时确定的。他可以被
    覆盖,实际上是入口点的链。建议多看看面向对象的概念
      

  11.   

    动态绑定对static无效
    ,,,精采,,,
    非常精采,,,
    不错呀,,今晚没有白来,,,
    特别是kadina(次帅)
    一句话惊醒梦中人,,,,,吓得我,,,,,拿起JAVA当了
    如果有机会我一定给你加五分,,,
      

  12.   

    给出一个继续深入的例子:class Base {
    static void doThis() { System.out.println("Base static doThis()"); }
    void doThat() { System.out.println("Base instance doThat()"); }
    }
    class Sub extends Base {
    //static method defined which does not override the Base doThis() only hide it. 
    static void doThis() { System.out.println("Sub static doThis()"); }
    //instance method defined which does override the Base doThat()
    void doThat() { System.out.println("Sub instance doThat()"); }
    }
    public class Test {
    public static void main(String[] args) {
    Base base = new Sub();  // base is an instance of Sub
    Sub sub = (Sub)base;    // legal cast both at compile and run time
    base.doThis();   //Base static doThis()
    base.doThat();   //Sub instance doThat()
    sub.doThis();    //Sub static doThis()
    sub.doThat();    //Sub instance doThat()
    //Think why, try to understand the following concepts: static is not dynamic
    //static binding : bind at compile time
    //Dynamic binding : bind at run time
    }
    }有关Java语言中的static binding(bound)的关键字:
    In Java: static, private, final methods are static bound.
    Those 3 methods does not participate polymorphism. In other words, they are not allowed to be overriden.
    In c++, 
    virtual function can be overriden, others not.
    virtual in C++ --> nothing in Java
    nothing in C++ --> final in Java