public class cat {
private static int recursionCount = 0;
public static void main(String[] args) {
try {
recursiveMethod();
} catch (StackOverflowError e) {
System.out.println("递归次数:" + recursionCount);
}
}
public static void recursiveMethod() {
recursionCount++;
recursiveMethod();
}
}
程序输出
递归次数:31603
进行31603次递归,创造了31603个方法栈,main方法也要创建一个方法栈,
一共 31603+1 个方法栈
小结:
当程序执行到方法时,会开辟独立的栈空间。
当方法执行完毕就会return到调用它的位置,并且释放栈空间。
返回后继续执行方法后面的代码
main栈被释放,整个程序退出
class Employee {
//名字,性别,年龄,职位,薪水
String name;
char gender;
int age;
String job;
double sal;
//因为要求可以复用构造器,因此老韩先写属性少的构造器
//职位,薪水
public Employee(String job, double sal) {
this.job = job;
this.sal = sal;
}
//名字,性别,年龄
public Employee(String name, char gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
//名字,性别,年龄,职位,薪水
public Employee(String job, double sal, String name, char gender, int age) {
this(name, gender, age);//使用到 前面的 构造器
this.job = job;
this.sal = sal;
}
}