File tree Expand file tree Collapse file tree 2 files changed +7
-6
lines changed Expand file tree Collapse file tree 2 files changed +7
-6
lines changed Original file line number Diff line number Diff line change @@ -29,7 +29,7 @@ stackoverflow-Java-top-qa
29
29
* [ Java 产生指定范围的随机数] ( https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/generating-random-integers-in-a-range-with-Java.md )
30
30
* [ JavaBean 到底是什么] ( https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/what-is-a-javabean-exactly.md )
31
31
* [ wait()和sleep()的区别] ( https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/difference-between-wait-and-sleep.md )
32
- * [ 能否在一个构造器中调用另一个构造器 ] ( http://stackoverflow.com/questions/285177/how-do-i-call-one-constructor-from-another-in-java )
32
+ * [ 能否在一个构造器( ` constructor ` )中调用另一个构造器 ] ( http://stackoverflow.com/questions/285177/how-do-i-call-one-constructor-from-another-in-java )
33
33
> 编程技巧
34
34
35
35
* [ 去掉烦人的“!=null"(判空语句] ( https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/avoiding-null-statements-in-java.md )
Original file line number Diff line number Diff line change 1
1
##能否在一个构造器中调用另一个构造器
2
2
3
3
###问题
4
- 能否在一个构造器中调用另一个构造器(在同一个类中,不是子类)?如果可以会怎样 ?
5
- 那么调用另一个构造器的最好方法是什么 (如果有几种方法可以选择的话)?
4
+ 能否在一个构造器中调用另一个构造器(在同一个类中,不是子类)?如果可以,怎么做 ?
5
+ 调用另一个构造器的最好方法是什么 (如果有几种方法可以选择的话)?
6
6
7
7
8
8
###回答
9
- 这样做是可以的。
9
+ 可以这样做:
10
10
``` java
11
11
public class Foo
12
12
{
@@ -23,7 +23,8 @@ public class Foo
23
23
}
24
24
}
25
25
```
26
- 如果你想链接到一个特定的父类构造器而不是本类的话,这里应该使用super,而不是this.
27
- 请注意,你只能链接到一个构造器,并且调用语句必须是这个构造器的第一个语句。
26
+ 如果你想调用一个特定的父类构造器,而不是本类的构造器,应该使用super,而不是this.
27
+ 请注意,在构造器中,你只能调用一次其他的构造器。并且调用其他构造器的语句,必须是这个构造器的第一个语句。
28
+
28
29
stackoverflow原址:
29
30
http://stackoverflow.com/questions/285177/how-do-i-call-one-constructor-from-another-in-java
You can’t perform that action at this time.
0 commit comments