梅兰芳学戏挨打的视频:JAVA问题!

来源:百度文库 编辑:神马品牌网 时间:2024/05/06 08:54:55
public a(double cm) {
this((int)Math.round(cm*MM_PER_CM));
}
帮我看这个构造函数
想知道this()是怎么用的
有什么用
一般不是“this.a=cm”什么的吗

还有这个
this(m*MM_PER_M + cm*MM_PER_CM + mm);

里面的“+”号是把所有元素的值加起来吗?
完整的程序是

public class a {

public static final int CM_PER_M = 100;
public static final int MM_PER_CM = 10;
public static final int MM_PER_M = MM_PER_CM*CM_PER_M;//毫米转换为米为100*10

private int meters = 0;
private int centimeters = 0;
private int millimeters = 0;
public a(int mm)
{
meters = mm/MM_PER_M;
centimeters = (mm - meters*MM_PER_M)/MM_PER_CM;
millimeters = mm - meters*MM_PER_M - centimeters*MM_PER_CM;
}

public a(double cm) {
this((int)Math.round(cm*MM_PER_CM));
}
public a(int m, int cm, int mm) {
this(m*MM_PER_M + cm*MM_PER_CM + mm);
}

public String toString()
{
return meters + "m " + centimeters + "cm " + millimeters + "mm";
}
public static void main(String[] args) {
a i=new a(111,555,666);
System.out.println(i.toString());
}
}
其中
构造函数public a(int mm)
{
meters = mm/MM_PER_M;
centimeters = (mm - meters*MM_PER_M)/MM_PER_CM;
millimeters = mm - meters*MM_PER_M - centimeters*MM_PER_CM;
}
如果去掉的话eclipes就会出现
递归性构造函数调用 a(double)
的报错
请问是怎么回事啊?
我明白是后两个构造函数都输出成毫米值
并把结果给第一个构造函数
但是为什么给第一个呢?
帮我解释下吧!谢谢!

太长了,懒得看了。this表示当前对象,因为对象一般具有方法跟属性,所以“一般不是“this.a=cm”什么的吗 ”这句话不是这样说的,this.a表示当前对象的属性a,但是也可以调用当前对象的方法。例如this((int)Math.round(cm*MM_PER_CM))表示调用当前的带参数的构造方法。如果要调用上面的方法a,就是this.a(XX).

这个a不是有个public a(int mm) 构造函数么?this((int)Math.round(cm*MM_PER_CM))就是调用这个构造函数