Jes*_* M. 1 java instance-variables this multiple-constructors
我不明白使用构造函数Rational()创建Rational对象时会发生什么.我的书说它将创建一个Rational对象,其值为0但内部存储为0/1.这个(0)如何存储为0/1?不是num和den 0的实例变量的默认值吗?
public class Rational{
public Rational(){
this(0);
}
public Rational(int n){
this(n,1);
}
public Rational(int x, int y){
num = x;
den = y;
}
private int num;
private int den;
}
Run Code Online (Sandbox Code Playgroud)
术语
Rational r = new Rational();
Run Code Online (Sandbox Code Playgroud)
呼吁
public Rational(){
this(0);
}
Run Code Online (Sandbox Code Playgroud)
要求
public Rational(int 0){
this(0,1);
}
Run Code Online (Sandbox Code Playgroud)
要求
public Rational(int 0, int 1){
num = 0;
den = 1;
}
Run Code Online (Sandbox Code Playgroud)
这意味着你的最终对象将是Rational(0,1);
| 归档时间: |
|
| 查看次数: |
171 次 |
| 最近记录: |