thinking in java 中有这样的列子string a;
this could not create an object but a referencestring a = new string("asdf");
this creat an reference connect to the object.//我理解为,a是一个引用, 是string对象的一个引用,而对象是"asdf"而在method的charpter中
tpye a = new type();
int x = a.f();
this a is a object.//所以我现在模糊了,希望高手能帮我解决这个疑难,方便我深入了解多态
this could not create an object but a referencestring a = new string("asdf");
this creat an reference connect to the object.//我理解为,a是一个引用, 是string对象的一个引用,而对象是"asdf"而在method的charpter中
tpye a = new type();
int x = a.f();
this a is a object.//所以我现在模糊了,希望高手能帮我解决这个疑难,方便我深入了解多态
this could not create an object but a referencestring a = new string("asdf");
this creat an reference connect to the object.//我理解为,a是一个引用, 是string对象的一个引用,而对象是"asdf"而在method的charpter中
tpye a = new type();
int x = a.f();
this a is a object.//所以我现在模糊了,希望高手能帮我解决这个疑难,方便我深入了解多态
如果是String的话,String a = new String("asdf");是极苯的写法……
会被人笑话,String a = "asdf";就可以了。
不用重复构建对象,虽然是小对象。然后,e文不行,this a is a(an) object.最后才说,tpye a = new type();
int x = a.f();
这里,a就是一个object,没有错的。
之所以这里string a = new string("asdf");
有人会说是一个reference,是因为有些家伙对vm
比较了解,基本上所有的vm(就我所见以及听说),
对于同一个vm中运行的代码,,只要他们包含相同的字符串
字面常量,则该对象就会被重用(jls中有,不记得具体的地方)
简单的说就是不管你
String a = "asdf";
String b = "asdf";
......
String x = "asdf";
vm都会让他们指向同一个对象,避免重复创建。
------------------------------------------------------------
引用楼主原话
string a;
this could not create an object but a referencestring a = new string("asdf");
this creat an reference connect to the object.//我理解为,a是一个引用, 是string对象的一个引用,而对象是"asdf"
----------------------------------------------------------------
楼主的理解完全正确,这里a不过是一个申明而已,他被申明为一个指向String对象的引用
---------------------------------------------------------------------
再引用楼主的第二段话
而在method的charpter中
tpye a = new type();
int x = a.f();
this a is a object.//所以我现在模糊了,希望高手能帮我解决这个疑难,方便我深入了解多态
---------------------------------------------------------------------------这不是多态的问题,这不过是一个JAVA中以对象调用函数,籍以此传递消息
在这里 type a = new type();,变量a依然是一个reference
但是int x = a.f()中,实际是a所引用的对象调用其f() method,那么我们认为a就代表一个对象,这很难通过术语解释清楚(起码我想了半天也很难解释,呵呵),但这里a.f()所代表的就是发送消息给对象
@.@||~
3Q