【文件属性】:
文件名称:定制ToString-C#入门经典教程
文件大小:4.81MB
文件格式:PPT
更新时间:2024-05-13 13:36:46
c#
定制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());
}
}