public class School {
//属性
String sname;
int jshu;
int jfshu;
//方法
public String toString()
{
return "名称:"+this.sname+"教室数:"+this.jshu+"机房数:"+this.jfshu;
}
}public class Schooltest { /**
* @param args
*/
public static void main(String[] args){
School xiao2=new School();
xiao2.sname="changsha";
xiao2.jfshu=2;
xiao2.jshu=3;
System.out.println(xiao2.toString());
School xiao3=new School();
xiao3=xiao2;
xiao3.sname="wuhan";
System.out.println(xiao2.toString());
System.out.println(xiao3.toString());
}}
上面是一段简单的代码,但是鄙人心里有个疑问,在 xiao3=xiao2之后,xiao3的值改变了,xiao2的值也改变了,xiao3的值改为wuhan,xiao2的值也改为wuhan了,大为不解,请各位给解释下。
//属性
String sname;
int jshu;
int jfshu;
//方法
public String toString()
{
return "名称:"+this.sname+"教室数:"+this.jshu+"机房数:"+this.jfshu;
}
}public class Schooltest { /**
* @param args
*/
public static void main(String[] args){
School xiao2=new School();
xiao2.sname="changsha";
xiao2.jfshu=2;
xiao2.jshu=3;
System.out.println(xiao2.toString());
School xiao3=new School();
xiao3=xiao2;
xiao3.sname="wuhan";
System.out.println(xiao2.toString());
System.out.println(xiao3.toString());
}}
上面是一段简单的代码,但是鄙人心里有个疑问,在 xiao3=xiao2之后,xiao3的值改变了,xiao2的值也改变了,xiao3的值改为wuhan,xiao2的值也改为wuhan了,大为不解,请各位给解释下。
解决方案 »
- javabean中,把属性由int改为double提示出错!
- java是否有能替代JColorChooser的组件或其它替代方法,颜色板中的颜色条是阶梯状的
- 请问网址以html结尾的是怎么做的
- java-j2ee的历程?
- hibernate中如果使用uuid.hex主键生成策略,是不是主键类型一定要是string的
- 报sessionFactory错误(求救啊)
- 急救:如何在jsp中调用WebService? 一百分奉送
- weblogic81怎么注册,高分求助
- 大侠推荐一个做报表软件吧!!!
- struts报找错:Cannot find ActionMappings or ActionFormBeans collection
- jsp中这个错误怎么处理!
- 关于框架的升级 请赐教!!
xiao3.sname="wuhan";
xiao2的引用赋给xiao3后,两个引用指向相同的内存区。
class School {
//属性
String sname;
int jshu;
int jfshu;
//方法
public String toString()
{
return "名称:"+this.sname+"教室数:"+this.jshu+"机房数:"+this.jfshu;
}
} public class Schooltest { /**
* @param args
*/
public static void main(String[] args){
School xiao2=new School();
System.out.println(xiao2.hashCode());
xiao2.sname="changsha";
xiao2.jfshu=2;
xiao2.jshu=3;
School test=new School();
System.out.println(test.hashCode());
System.out.println(xiao2.toString());
School xiao3=new School();
xiao3=xiao2;
System.out.println(xiao3.hashCode());
xiao3.sname="wuhan";
System.out.println(xiao2.toString());
System.out.println(xiao3.toString());
} }把他们的hashCode打出来,就明白了.xiao2和xiao3是同一个
**************第一种情况************************************
#include <iostream>
#include <string>class school
{
public:
std::string name;
int jiaoshi,jifang;
public:
void tostring()
{
std::cout<<name<<jiaoshi<<jifang<<std::endl;
}
};
int main(int argc,char *argv[])
{
school *xiao2=new school;
xiao2->name="changsha";
xiao2->jiaoshi=2;
xiao2->jifang=3;
std::cout<<xiao2->name<<std::endl;
school *xiao3=new school;
xiao3=xiao2;
xiao3->name="wuhan";
std::cout<<xiao2->name<<xiao3->name<<std::endl;
}
用堆分配对象时,用的是指针指向所分配的内存区域,这样就相当于将xiao2的指针值赋给xiao3,也就是xiao3指向的内存区域与xiao2指向的相同,因此,一改俱改。
*************************************第2种情况***********************
int main(int argc,char *argv[])
{
school xiao2;
xiao2.name="changsha";
xiao2.jiaoshi=2;
xiao2.jifang=3;
std::cout<<xiao2.name<<std::endl;
school xiao3;
xiao3=xiao2;
xiao3.name="wuhan";
std::cout<<xiao2.name<<xiao3.name<<std::endl;
}
用栈给对象分配内存,就不存在上述问题
System.out.println(xiao2.toString());
System.out.println(xiao3.toString());
xiao3.sname="wuhan";
System.out.println(xiao2.toString());
System.out.println(xiao3.toString()); 这样看就更容易理解了;两个引用总是指向相同的内存区。
把他们的hashCode打出来,就明白了.xiao2和xiao3是同一个hashCode是怎么调出来的?小弟新手。
实例化一个对象:xiao2,在堆上分配一块空间
再实例化第二个对象:xiao3,同样在堆上分配一块空间
然后将xiao2赋给xiao3,因为类是引用类型,引用类型的赋值是地址的赋值,所以xiao3也会指向xiao2的地址,xiao2和xiao3引用同一块内存空间,so,修改任何一个对象的属性值,另外一个也都会改变
不可能吧
只看这段代码的话肯定不会变
public class School {
//属性 -----还属性? 你这个哪个是属性呀?
String sname;
int jshu;
int jfshu;
//方法
public String toString()
{
return "名称:"+this.sname+"教室数:"+this.jshu+"机房数:"+this.jfshu;
}
}
..................................
例如:void fillWithInteger(Object &a);
最后说一下指针,同意二楼说法,害了一代人,功能上和引用很像,不过老是出一些诸如越界之类的问题,使用起来也老要在变量前面加一个“*”,实在用起来不爽(当然不排除某些高手仍旧把它用的出神入化)