Stackoverflow error

后端 未结 3 614
Happy的楠姐
Happy的楠姐 2021-01-21 14:29

The code given below shows a Stackoverflow error when run.But if I make another class CarChange to create objects of Car ,it runs sucessfully. I am a beginner ,doing this code t

相关标签:
3条回答
  • 2021-01-21 15:07

    It sounds like you might have some infinite recursion happening.

    drive() calls testdrive() which class drive() which calls testdriver()...forever, or until you run out of memory, hence your stack overflow error.

    0 讨论(0)
  • 2021-01-21 15:09

    A stackoverflow usually means you have an infinite loop.

    The reason you're receiving this is because you're calling drive from the testdrive method and in that method you're calling drive again.

    0 讨论(0)
  • 2021-01-21 15:09
    Car() {
        c[0] = new Polo();
        i=0;
    }
    

    As Polo is a subclass of Car() - it must be to fit in the Car[] - it will call the Car's constructor when being constructed itself. The Car constructor tries to create a new Polo().

    As Polo is a subclass of Car() - it must be to fit in the Car[] - it will call the Car's constructor when being constructed itself. The Car constructor tries to create a new Polo().

    As Polo is a subclass of Car() - it must be to fit in the Car[] - it will call the Car's constructor when being constructed itself. The Car constructor tries to create a new Polo().

    ... you get the picture?

    0 讨论(0)
提交回复
热议问题