所以我有这门课:
class Test { private int field1; private int field2; public Test() { field1 = // some code that needs field2 = // a lot of cpu time } private Test GetClone() { Test clone = // what do i have to write there to get Test instance // without executing Test class' constructor that takes // a lot of cpu time? clone.field1 = field1; clone.field2 = field2; return clone; } }
private Test(bool qwerty) {} private Test GetClone() { Test clone = new Test(true); clone.field1 = field1; clone.field2 = field2; return clone; }
我没有测试过,但我做得对吗?有没有更好的方法呢?