自建toString方法的问题

[复制链接]
查看11 | 回复6 | 2007-1-14 09:32:11 | 显示全部楼层 |阅读模式
我有如下一个类:
class abc {
......
......
......
......
public static String toString()
{
return "5555555555555555";
}
}

编译时报告错误:
abc.java:19: toString() in abc cannot override toString() in java.lang.Object;
toString() and toString() are static
public static String toString()

^
1 error
但是如果将static去掉,就能通过编译,并且public还不能省略。
我在自己的建立的类中定义方法,怎么会与java.lang冲突呢?为什么不能用static,必须用public ?
回复

使用道具 举报

千问 | 2007-1-14 09:32:11 | 显示全部楼层
Instance method and static method can not override each other.
回复

使用道具 举报

千问 | 2007-1-14 09:32:11 | 显示全部楼层
可是我在abc类中的toString方法不与谁冲突呀!
回复

使用道具 举报

千问 | 2007-1-14 09:32:11 | 显示全部楼层
Object.toString() method already exists as an instance method. Read your API doc carefully, and learn all methods on at least Object by heart.
回复

使用道具 举报

千问 | 2007-1-14 09:32:11 | 显示全部楼层
看看object类
你得abc把它的toString给override了
回复

使用道具 举报

千问 | 2007-1-14 09:32:11 | 显示全部楼层
public static String toString() ===〉
public String toString() 就可以了
回复

使用道具 举报

千问 | 2007-1-14 09:32:11 | 显示全部楼层
大家说的对,人家父类的方法不是static的,你干嘛要给加上static呢?
另外,如果没有public,缺省的就是default的修饰词了,要比public缩小了范围,java不允许的,只能增大其修饰范围,比如父类的方法是private,你可以重载为public的方法。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行