堆栈溢出错误

编程入门 行业动态 更新时间:2024-10-24 07:25:11
本文介绍了堆栈溢出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

下面给出的代码显示了运行时的Stackoverflow错误.但是,如果我使另一个类CarChange创建Car对象,它将成功运行.我是一个初学者,请执行以下代码以了解在Java中进行向上转换的重要性.

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 to understand the importance of upcasting in java.

public class Car { int i; Car[] c=new Car[2]; Car() { c[0] = new Polo(); i=0; } void drive(){ c[i].testdrive(); //the overloaded method in subclasses polo and swift } void change() { if(i==0) { i++; c[i] = new Swift(); } } public void testdrive() { //overloaded method in subclasses polo and swift System.out.println(" test drive car"); } //class Tester { //main Car c= new Car(); c.drive(); c.change(); c.drive();

推荐答案

stackoverflow通常意味着您有一个无限循环.

A stackoverflow usually means you have an infinite loop.

收到此消息的原因是因为您正在从testdrive方法调用驱动器,并且在该方法中您再次调用drive.

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.

更多推荐

堆栈溢出错误

本文发布于:2023-11-09 09:56:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572077.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:堆栈   错误

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!