-
入门经典教程:C#定制ToString方法
资源介绍
定制ToString()
默认返回.
定制ToString方法实例:
using System.Text;
using System;
public class Chair
{
private double myPrice;
private string myVendor, myID;
public Chair(double price, string vendor, string sku)
{
myPrice = price;
myVendor = vendor;
myID = sku;
}
public override string ToString()
{
StringBuilder chairSB = new StringBuilder();
chairSB.AppendFormat("ITEM = Chair");
chairSB.AppendFormat("\tVENDOR = {0}", myVendor);
chairSB.AppendFormat("\tPRICE = {0}", myPrice.ToString());
return chairSB.ToString();
}
static void Main()
{
Chair myChair = new Chair(120.0, "Broyhill", "60-1222");
Console.WriteLine(myChair.ToString());
}
}
- 上一篇: 显式实现接口-C#入门经典教程
- 下一篇: 克隆来创建对象副本-C#入门经典教程