关于java:为什么我在构造函数中执行某些操作后无法调用“this”

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 this或第一级superas the statement)非常在任何构造函数。JLS§8.8.7 EEA。更多信息:构造函数体forP></

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
public Date(int year) { this(year, 1, 1); }
public Date(int year, int month) { this(year, month, 1); }

that this is not the笔记,最佳的方式表示方法只有一岁。也许你可以allow the value为0。然后它会成为this(year, 0, 0),for example。P></


as other out answers点是stated of the语言规则。P></

我只是想点,在其他点在constructors allowing to Whole of introduces在负载量和复杂性可能会导致错误的inevitably to that。P></

You can have a(weaker)规则,必须super()构造函数呼叫或者确切的说因为是在构造函数或在this()盎司。does that already away with the of Call to the便利隐默认构造函数(超livable)but how你实施"订单吗?P></

这是不可能的(问题formally to write the halting)在静态类型,编译器可以确定,如果任何一个构造函数或确切的说因为是will be called知道at the甚有最小运行时间检查了我在纽约SecondConstructorCallExceptionwhich result as和as NoConstructorCalledExceptionProspect of the阱处理(probably)超类的对象不被initialisedbefore/ at the start of the子类的构造函数。P></

在运行时,会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超类的构造函数,你可以使用super(...)(必要的),然后重定向到this(...)构造函数运行额外的代码使用在线supports干顶没有introducing语言语义的还要更多。P></

如果你在construct need to some甚至更多的复合对象总是要回答方式the is to be工厂(或工厂/器类的方法)。P></

C + +(effect)有相似的规则,但它的拓展提供一表在使用:of before the following构造函数构造函数体:P></

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></