String 类实现了Clonable接口????

zhudonhua 2005-07-01 08:40:58
如题
...全文
246 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
晨星 2005-07-15
  • 打赏
  • 举报
回复
还有,引用传递的许多特性在String和Integer这两个类的对象上是看不出来的,如果你仔细研究一些这两个类的所有方法,你会发现,这两个类都是那种“常量类”,你可以获得它里面的数据,你也可以利用他们构造新的数据,但就是无发改变他们里边的任何东西。
这就是为什么你可以通过:
str = str + "World";
来利用原来str所引用的对象再加“World”一起构造一个新对象并让str引用到这个新对象,却无法改变原来那个str所引用的对象本身。
但StringBuffer类可以,这个类有许多可以改变自己的函数。
晨星 2005-07-15
  • 打赏
  • 举报
回复
str = str + " World!";
这句是改变了参数str引用的对象,这句执行完以后,str引用到一个新对String对象,而并非改变了str原来所引用的那个对象的内容。而又因为局部修改参数引用并不影外部的调用者,所以外面原来的那个变量名(引用)还是引用着它原来引用的那个String对象"Hello"。
str = new Integer(100);
一样的道理,里面修改了引用,不影响外面那个引用本来所引用的对象。

如果楼主熟悉C语言的指针,理解这些东西应该很容易。
zeiku 2005-07-14
  • 打赏
  • 举报
回复
学习中!!!
zhudonhua 2005-07-07
  • 打赏
  • 举报
回复

Hello
1000
为什么不变啊??
我在
public static void test(String str)
{
str = str + " World!";
}
public static void test(Integer str)
{
str = new Integer(100);
}
也改了啊!!!
chg2008 2005-07-07
  • 打赏
  • 举报
回复
是啊 你都修改了,他能不变吗
afeng666 2005-07-07
  • 打赏
  • 举报
回复
public static void test(Student stu)
{
stu = stu.modify("zeiku");
}

你在这里不是已经把name改了吗?
zhudonhua 2005-07-07
  • 打赏
  • 举报
回复
class Student
{
private String name ;
private String num;
public Student(String name , String num)
{
this.name = name;
this.num = num;
}

public Student modify(String name)
{
this.name =name;
return this;
}

public String toString()
{
return name + " "+num ;
}
}
public class StringTest
{
public static void test(String str)
{
str = str + " World!";
}
public static void test(Integer str)
{
str = new Integer(100);
}
public static void test(Student stu)
{
stu = stu.modify("zeiku");
}
public static void main(String []args)
{
String str = new String("Hello");
test(str);
prt(""+str);

Integer inte = new Integer(1000);
test(inte);
prt(""+inte.intValue());

Student stu = new Student("zhudonhua","3030612060");
test(stu);
prt(""+stu);
}
public static void prt(String str)
{
System.out.println(str);
}
}

我自己定义一个类,但是他打印出的结果?????
运行结果:
Hello
1000
zeiku 3030612060
为什么最后一行不是zhudonhua 3030612060??????
humanity 2005-07-06
  • 打赏
  • 举报
回复
不可变 量 克隆? 除了浪费内存之外没什么吸引人的.
Yanbin_Q 2005-07-06
  • 打赏
  • 举报
回复
参数值肯定是不能被改变的了
yangbc 2005-07-06
  • 打赏
  • 举报
回复
搂主是不是还没搞明白参数传递问题啊
晨星 2005-07-06
  • 打赏
  • 举报
回复
那里不明白?
zhudonhua 2005-07-06
  • 打赏
  • 举报
回复
//:StringTest.java
class Student implements Cloneable
{
private String name ;
private String num;

public Student(String name , String num)
{
this.name = name;
this.num = num;
}

public Student modify(String name)
{
this.name =name;
return this;
}

public String toString()
{
return name + " "+num ;
}
}
public class StringTest
{
public static void test(String str)
{
str = str + " World!";
}
public static void test(Integer str)
{
Integer inte= str;
inte = new Integer(100);
}
public static void test(Student stu)
{
stu = stu.modify("zeiku");
}
public static void main(String []args)
{
String str = new String("Hello");
test(str);
prt(""+str);

Integer inte = new Integer(1000);
test(inte);
prt(""+inte.intValue());

Student stu = new Student("zhudonhua","3030612060");
test(stu);
prt(""+stu);
}
public static void prt(String str)
{
System.out.println(str);
}
}


看看,这是为什么???
yangbc 2005-07-01
  • 打赏
  • 举报
回复
String类型的构造方法没有说深拷贝

实现克隆方法的类你都可以构造出一个和原来实例状态一模一样的对象,而String继承自Object的clone方法在Object中也好像只是克隆了一下对象的地址,而String也并没有重写这个方法,这就可以说明,string并没有打算用这个clone方法

不知道1.5中的String是不是另外一个样子
晨星 2005-07-01
  • 打赏
  • 举报
回复
没有进行深拷贝,是你理解错了。再仔细想想,看自己卡在那里了。
String其实是个常量类型,他的任何一个对象都是不变的,它的所有的方法也是顶多返回一个新的串给你,而自身是永远不变的。
如果还想不明白,把你不明白的代码贴出来,让大家帮你解释解释。:)
zhudonhua 2005-07-01
  • 打赏
  • 举报
回复
但是它进行参数传递时候进行深拷贝阿!!
晨星 2005-07-01
  • 打赏
  • 举报
回复
偶那个是1.4的,呵呵。
晨星 2005-07-01
  • 打赏
  • 举报
回复
public final class String
extends Object
implements Serializable, Comparable, CharSequence
yuanwh121 2005-07-01
  • 打赏
  • 举报
回复
public final class String implements java.io.Serializable, Comparable<String>, CharSequence

你看看它实现了什么接口?

62,628

社区成员

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

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