Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 73223a4

Browse filesBrowse files
committed
Merge pull request giantray#12 from lxy765705425/master
翻译了How do I call one constructor from another in Java?
2 parents ef18c04 + a07456e commit 73223a4
Copy full SHA for 73223a4

File tree

Expand file treeCollapse file tree

2 files changed

+30
-2
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+30
-2
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ stackoverflow-Java-top-qa
2929
* [Java 产生指定范围的随机数](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/generating-random-integers-in-a-range-with-Java.md)
3030
* [JavaBean 到底是什么](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/what-is-a-javabean-exactly.md)
3131
* [wait()和sleep()的区别](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/difference-between-wait-and-sleep.md)
32-
32+
* [能否在一个构造器中调用另一个构造器](http://stackoverflow.com/questions/285177/how-do-i-call-one-constructor-from-another-in-java)
3333
> 编程技巧
3434
3535
* [去掉烦人的“!=null"(判空语句](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/avoiding-null-statements-in-java.md)
@@ -67,7 +67,6 @@ stackoverflow-Java-top-qa
6767
- [Does finally always execute in Java?](http://stackoverflow.com/questions/65035/does-finally-always-execute-in-java)
6868
- [Convert a String to an enum in Java](http://stackoverflow.com/questions/604424/convert-a-string-to-an-enum-in-java)
6969
- [Android SDK installation doesn't find JDK](http://stackoverflow.com/questions/4382178/android-sdk-installation-doesnt-find-jdk)
70-
- [How do I call one constructor from another in Java?](http://stackoverflow.com/questions/285177/how-do-i-call-one-constructor-from-another-in-java)
7170
- [Initialization of an ArrayList in one line](http://stackoverflow.com/questions/1005073/initialization-of-an-arraylist-in-one-line)
7271
- [Java inner class and static nested class](http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class)
7372
- ['Must Override a Superclass Method' Errors after importing a project into Eclipse](http://stackoverflow.com/questions/1678122/must-override-a-superclass-method-errors-after-importing-a-project-into-eclips)
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
##能否在一个构造器中调用另一个构造器
2+
3+
###问题
4+
能否在一个构造器中调用另一个构造器(在同一个类中,不是子类)?如果可以会怎样?
5+
那么调用另一个构造器的最好方法是什么(如果有几种方法可以选择的话)?
6+
7+
8+
###回答
9+
这样做是可以的。
10+
```java
11+
public class Foo
12+
{
13+
private int x;
14+
15+
public Foo()
16+
{
17+
this(1);
18+
}
19+
20+
public Foo(int x)
21+
{
22+
this.x = x;
23+
}
24+
}
25+
```
26+
如果你想链接到一个特定的父类构造器而不是本类的话,这里应该使用super,而不是this.
27+
请注意,你只能链接到一个构造器,并且调用语句必须是这个构造器的第一个语句。
28+
stackoverflow原址:
29+
http://stackoverflow.com/questions/285177/how-do-i-call-one-constructor-from-another-in-java

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.