ToString的疑惑
public class my:CollectionBase{
public override string ToString()
{
return "hello";
}
}
Console.WriteLine(new my());
为什么输出为"collection",而不是"HELLO"呢?重写好像没起作用!!! --------------------编程问答-------------------- 顶起 --------------------编程问答-------------------- Console.WriteLine(new my()); 你输出的是个对象 好像! --------------------编程问答-------------------- 我想输出对象进,会调用该对象的ToString()方法吧? --------------------编程问答-------------------- 你输出了一个my对象实例而你的my是基于CollectionBase的。和toString没有关系。。
要想测试你的代码应该是:
my ToString=new my();
Console.WriteLine(ToString.toString());
--------------------编程问答-------------------- 怎么没有构造方法?这样你的类无法实列化呀。 --------------------编程问答-------------------- 。。。不可能,一定是hello --------------------编程问答-------------------- 因该是调用类的构造方法,不会调用这个ToString()吧!
要是把return hello写构造函数里,应该可以输出吧! --------------------编程问答-------------------- 经测试是hello
lz逗我们玩呢。 --------------------编程问答-------------------- public class my:CollectionBase
{
public override string ToString()
{
return "hello";
}
}
Console.WriteLine(new my());
-----------------------------------------------
Console.WriteLine((new my()).ToString()); --------------------编程问答--------------------
Console.WriteLine(new my());
输出对象本身时,应该会调用该对象的ToString方法,但现在好像调用的是CollectionBase(父类)的ToString方法,因为结果为:COLLECTION.,还望高手指点!!
--------------------编程问答-------------------- 经过测试确实输出的是hello啊,楼主拿我们寻开心吧. --------------------编程问答-------------------- //项目:多列combobox
...
...
//定义my集合类,用以存放多列Combobox中的一行
public class my:CollectionBase
{
....
public my():base()
{ this.clear(); }
public int add(string v)//添加进集合
{
return this.innerList.add(v);
}
public override string ToString()
{
return this.innerList[0].toString();
}
.......
}
//sub
my a1=new my();
a1.add("001");
a1.add("zhangsan");
a1.add("20");
my a2=new my();
a2.add("002");
a2.add("wangwu");
a2.add("22");
combobox1.items.add(a1);
combobox1.items.add(a2);
......
///sub end
怎么在Combobox1中选择第一项时,combobox1.text为001
怎么在Combobox1中选择第二项时,combobox1.text为002
现在,无论选择那项,combobox1.text都是:"collection"
--------------------编程问答-------------------- ????? --------------------编程问答-------------------- combobox1.items.add(a1);
a1是当object添加进去了吧?你没ToString()? --------------------编程问答-------------------- 懂你什么意思,重写object的ToString来代替ToString
现在,无论选择那项,combobox1.text都是:"collection" 这句是怎么理解?COMBOBOX里面已经是001 002了没? --------------------编程问答-------------------- 我测试了下,你这份代码是没问题的, comboBox里面是001 002,ToString也起了正确的作用,现在你问题在哪请明确指出 --------------------编程问答-------------------- a o --------------------编程问答-------------------- Console.WriteLine((new my()).ToString());
是"hello" --------------------编程问答-------------------- 我测试了下,你这份代码是没问题的, comboBox里面是001 002,ToString也起了正确的作用,现在你问题在哪请明确指出
我现在出现在不是001 002 而是"collection" --------------------编程问答--------------------
Console.WriteLine((new my()).ToString()); --------------------编程问答-------------------- collection是哪出来?你该查查这个,你所给的代码里面没见到~ --------------------编程问答-------------------- public override string ToString()
{
return this.innerList[0].toString();
}
===========
innerList 具体定义??
--------------------编程问答-------------------- collection是哪出来?你该查查这个,你所给的代码里面没见到~
====================================================
我想可能是my类继承自collectionbase 中的问题,但是不知道怎么来查找"collection"的来源!!!
--------------------编程问答-------------------- up --------------------编程问答-------------------- piao --------------------编程问答--------------------
public class my:CollectionBase
{
public override string ToString()
{
return "hello";
}
}
---------------------
CollectionBase s = new my();
Console.WriteLine(s); --------------------编程问答--------------------
public new override string ToString()
{
return "hello";
} --------------------编程问答-------------------- public new override string ToString()
{
return "hello";
} --------------------编程问答-------------------- Are you exaggerating yourself? if yes, then close it please --------------------编程问答--------------------
补充:.NET技术 , C#