小问题

fuli_90121 2002-06-21 12:18:17
以下程序不能编译通过
import java.import java.io.*;
import java.lang.*;
import java.util.*;
public class test {
public static void main(String argv[]) {
String item1="xwf";
String item2;

......
item2=item1.clone();
System.out.println(item1);
System.out.println(item2);
}
}
编译错误信息:
test.java:27: clone() has protected access in java.lang.Object
item2=item1.clone();
^
test.java:27: incompatible types
found : java.lang.Object
required: java.lang.String
item2=item1.clone();
^
请高手赐教
在线



...全文
38 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
fuli_90121 2002-06-21
  • 打赏
  • 举报
回复
protected的定义好象是方法所有者类及其子类可以使用,但在这个程序中
item是String,而它为什么就不能使用String的protected方法呢?可以再解释
详细些吗?
yakuu 2002-06-21
  • 打赏
  • 举报
回复
clone()是String类的protected方法。
如果你要用的话,写一个String的继承类。
fuli_90121 2002-06-21
  • 打赏
  • 举报
回复
to kamanwu(kaman)
错误边变为:
test.java:27: clone() has protected access in java.lang.Object
item2=(String)item1.clone();
^
kamanwu 2002-06-21
  • 打赏
  • 举报
回复
item2=(String)item1.clone();
kamanwu 2002-06-21
  • 打赏
  • 举报
回复
item2=(String)item1.clone();
kamanwu 2002-06-21
  • 打赏
  • 举报
回复
item2=(String)item1.clone();
johnytian 2002-06-21
  • 打赏
  • 举报
回复
public class test implements Cloneable{
public String strValue;
public int intValue;
public test(String str,int i)
{
this.strValue=str;
this.intValue=i;
}
public test()
{
}
public static void main(String argv[]) {
test test1,test2;
test1=new test("Clone test",0);
test2=new test();
try
{
test2=(test)(test1.clone());
}
catch(CloneNotSupportedException e)
{
System.out.println(e.getMessage());
}
System.out.print(test1.strValue+" ");
System.out.println(test1.intValue);
System.out.print(test2.strValue+" ");
System.out.println(test2.intValue);
}
}
kamanwu 2002-06-21
  • 打赏
  • 举报
回复
public final class String
extends Object

String不能被extend.

protected方法再它的子类中能用。但是你的程序不能用。public才可以。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧