0

sub calss(dog)のオブジェクトがそのスーパークラス(Animal)によって参照される単純なアップキャストが行われています...

メソッド「callme2()」を呼び出せないのはなぜですか

コード:-

class Animal 
    { 
        public void callme()
        {
            System.out.println("In callme of Animal");
        }

    }

    class Dog extends Animal 
    { 
        public void callme()
        {
            System.out.println("In callme of Dog");
        }

        public void callme2()
        {
            System.out.println("In callme2 of Dog");
        }
    }

    class upcasting
    {
        public static void main (String [] args) throws Exception
        {
            Animal a = new Dog();     
    a.callme(); //-In call me of Dog       
    a.callme2(); // - error why ?
     }
    }
4

1 に答える 1

2

aスーパークラスに割り当てられているためAnimal

あなたは次のようにmethedを呼び出すことができます:

((Dog)a).callme2() 
于 2012-06-16T07:15:03.127 に答える