有关java的问题 (30分)

  • 主题发起人 c3411080
  • 开始时间
C

c3411080

Unregistered / Unconfirmed
GUEST, unregistred user!
我在一个类中加入
public String toString()
{
returndo
uble.toString();
}
把double类型的转换成string,为什么编译时会出现这样的错误:
non-static method tostring() cannot be referenced from a static context
 
>>do
uble.toString();
Double类有2个超载得toString()方法
其中一个是static的,类方法:
public static String toString(double d){...}
一个是非静态的实例方法
public String toString(){...}
如果你用静态调用的话,应该用带参数的方法:
如:
privatedo
uble b;
public String toString(){
returndo
uble.toString(b);
}

而如果用实例方法调用,如:
privatedo
uble b;
public String toString(){
return b+"";
}
因为编译器会自动为b加入toString()调用,所以你
不能在用b.toString()方法了
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
回复
0
查看
1K
天地弦
顶部