作者sweetjp6 (水饺)
看板java
标题[问题] 不定长度引数造成的overflow
时间Tue Sep 13 11:56:28 2011
目前在练习不定常数引数的使用方法,
但照着书本打之後发现会出现
Exception in thread "main" java.lang.StackOverflowError
google过後发现有可能是过度呼叫函数的问题,
但小弟一直看不出哪里有重复呼叫,不知道是不是可以麻烦版友们指点一下
小弟谢先!!!!
//////////////////////////////
class Calculator {
double price;
Calculator(double price) {
this.price = price;
}
double CalculatePrice(Land l) {
return l.area() * price;
}
double totalPrice(Land... Lands) {
double total = 0;
for (Land l : Lands) {
* total += totalPrice(l); //问题出现在此
}
return total;
}
}
public class poly {
public static void main(String[] argv) {
Circle c = new Circle(5);
Square s = new Square(5);
Calculator ca = new Calculator(3000.0);
System.out.println("总价值:" + // 使用匿名阵列
ca.totalPrice(new Land[]{c,s}));
}
////////////////////////////
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.117.164.141
1F:→ asilzheng:recursive的中断点没设好?? 09/13 12:38
2F:推 LPH66:虽然是楼上的问题没错 但根本原因是原PO呼叫错函式了 09/13 12:54
3F:推 LPH66:给原PO的提示: CalculatePrice 一个人在那边有点孤单喔 XD 09/13 12:57
4F:→ sweetjp6:原来如此,是我眼残了。谢谢楼上两位大大的提醒~ 09/13 13:38