Why cannot I call “this” after I do something in the Constructor
(P)我有两个建筑师给我的日期。The first one just have 3 int parameter represent month,day and year.and the second one,I provide it in case user give string as one parameter to represent month/day/year.(p)(P)Since software rule§35;1:Don't repeat code.I decide to stose the string in second constructor,and use the first constructor to verify whether the date is valid or not.So in the end I call this to provide the same 3 int parameter as the first constructor.(p)(P)好吧,伙计,给我这个错误:错误:这必须是第一次在建设者。(p)(P)I understand that I can have the second constructor this way,but this against software rule§35;1.(p)字母名称(P)请看第一和第二个建筑商:(p)字母名称
因为这是规则语言和EN of the present is the Java语言规范:在电话中另一个构造函数(the this the same class or to(……)的一部分)在类的构造函数中(using the超超(…))必须在第一线去。P></
It's a way to ensure that the parent's state is initialized before
initializing the current object.
为更多的信息,take a look at this explains the detail中后,它的情况。P></
但在你的情况,你可以使用这个软件# which是替代上述规则1 which You have。P></
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public class Date { private int month; private int day; private int year; public Date(int month, int day, int year) { this(month+"/"+day+"/"+year); } public Date(String s) { String[] strSplit = s.split("/"); int m = Integer.parseInt(strSplit[0]); int d = Integer.parseInt(strSplit[1]); int y = Integer.parseInt(strSplit[2]); if (isValidDate(m, d, y)) { this.month = m; this.day = d; this.year = y; } else { System.out.println("Fatal error: Invalid data."); System.exit(0); } } |
为更多的信息,take a look at this explains the detail中后,它的情况。P></
对上述Java as the syntax,呼叫另一个构造函数(Force to the
The first statement of a constructor body may be an explicit invocation of another constructor of the same class or of the direct superclass (§8.8.7.1).
但让我向你展示了你的其他类。是的,你的要求是:solved与工厂方法P></
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | public final class Date { final int year; final int month; final int day; public static Date parse(String dateAsString) { String[] strSplit = dateAsString.split("/"); int m = Integer.parseInt(strSplit[0]); int d = Integer.parseInt(strSplit[1]); int y = Integer.parseInt(strSplit[2]); return new Date(y, m, d); } public Date(int year, int month, int day) { checkValues(year, month, day); this.year = year; this.month = month; this.day = day; } private static void checkValues(int year, int month, int day) { if (!isValidDate(year, month, day)) throw new IllegalArgumentException("Invalid date values."); } private static boolean isValidDate(int year, int month, int day) { // TODO Validation! return true; } } |
在线求职constructors is the干的原则是chaining done by构造函数。想象一下,你也要创建一constructors for that only have a year or a year和month。提供两constructors:纽约P></
1 2 |
that this is not the笔记,最佳的方式表示方法只有一岁。也许你可以allow the value为0。然后它会成为
as other out answers点是stated of the语言规则。P></
我只是想点,在其他点在constructors allowing to Whole of introduces在负载量和复杂性可能会导致错误的inevitably to that。P></
You can have a(weaker)规则,必须
这是不可能的(问题formally to write the halting)在静态类型,编译器可以确定,如果任何一个构造函数或确切的说因为是will be called知道at the甚有最小运行时间检查了我在纽约
在运行时,会entail some of whether构造函数有一直在跟踪我。P></
替代(appears to be the Calling)开季多constructors时报在线(请只在建设?欧洲债券)。我真的只是pushes into the application the问题谁has not to developers搭接是否他们会关注themselves but whether to constructors两次呼叫的任何子类是想给你知道的后果和其部分。P></
执行问题的继承,它已经suffers from the details of the implementation exposes超类子类but this to the other to be the似乎要去的方式!《超级曝光执行details of an as to - unwritten Y等亚类。P></
我argue that the existing where the rules specify超类的构造函数,你可以使用
如果你在construct need to some甚至更多的复合对象总是要回答方式the is to be工厂(或工厂/器类的方法)。P></
C + +(effect)有相似的规则,但它的拓展提供一表在使用
1 | MyClass(/*...*/) : MyBase(/*...*/) {/*...*/ } |
或(c + + 11 onwards):P></
1 | MyClass(/*...*/) : this(/*...*/) { /*...*/ } |
恩,让我在小clearer表是要functionally but is the在线的结果非常相似。与多重继承的想法通知电话的基础constructors of the class at the start of the body unwieldy公平会好的。P></
注:只有在建设?but is that only with the body of the构造函数?P></