package com.tjych.pattern.prototype; public class test1{ private String s = "sdfsdf"; private int test = 1; private test1 test2 = new test1(); public String getS() { return s; } public void setS(String s) { this.s = s; } public int getTest() { return test; } public void setTest(int test) { this.test = test; } public test1 getTest2() { return test2; } public void setTest2(test1 test2) { this.test2 = test2; } public static void main(String[] args) { test1 t = new test1(); } }
乍一看没有什么问题,但是运行之后发现哦mygod!!!!
Exception in thread "main" java.lang.StackOverflowError at com.tjych.pattern.prototype.test1.<init>(test1.java:8) at com.tjych.pattern.prototype.test1.<init>(test1.java:8) at com.tjych.pattern.prototype.test1.<init>(test1.java:8) at com.tjych.pattern.prototype.test1.<init>(test1.java:8) at com.tjych.pattern.prototype.test1.<init>(test1.java:8) at com.tjych.pattern.prototype.test1.<init>(test1.java:8) at com.tjych.pattern.prototype.test1.<init>(test1.java:8) at com.tjych.pattern.prototype.test1.<init>(test1.java:8) at com.tjych.pattern.prototype.test1.<init>(test1.java:8) at com.tjych.pattern.prototype.test1.<init>(test1.java:8) at com.tjych.pattern.prototype.test1.<init>(test1.java:8) at com.tjych.pattern.prototype.test1.<init>(test1.java:8) at com.tjych.pattern.prototype.test1.<init>(test1.java:8) at com.tjych.pattern.prototype.test1.<init>(test1.java:8)
原来,在new 对象的时候就像进入了是循环一样!!蛋疼!所以要记住哦