0
我目前正在學習C#,並且試圖讓我的頭繞着類繼承,並且已經編寫了一些代碼來向控制檯顯示一行代碼,但是我遇到了此帖子主題中的錯誤。未處理的類型爲「System.FormatException」的異常輸入字符串格式不正確
它在string.format行中的子類PC中拋出錯誤。我假設它是與我傳遞的類型有關,但我迄今爲止嘗試過的所有內容都無法正常工作。任何意見都非常感謝,因爲我希望在進入下一個培訓階段之前確保充分理解這一點。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassInheritance
{
class Program
{
static void Main(string[] args)
{
PC myPC = new PC();
myPC.casetype = "Black SuperCool";
myPC.contract = true;
myPC.dateadded = DateTime.Now;
myPC.id = 1;
myPC.type = "Small form factor";
printEquipmentDetails(myPC);
Console.ReadLine();
}
private static void printEquipmentDetails(Equipment Equipment)
{
Console.WriteLine("Details: {0}", Equipment.DisplayEquipment());
}
}
abstract class Equipment
{
public int id { get; set; }
public string type { get; set; }
public bool contract { get; set; }
public DateTime dateadded { get; set; }
public abstract string DisplayEquipment();
}
class PC : Equipment
{
public string casetype { get; set; }
public override string DisplayEquipment()
{
return String.Format("(0} - {1} - {2} - {3} - {4}", this.id, this.type, this.contract.ToString(), this.dateadded.ToShortDateString(), this.casetype);
}
}
class Laptop : Equipment
{
public string batterylife { get; set; }
public override string DisplayEquipment()
{
return String.Format("(0} - {1} - {2} - {3} - {4}", this.id, this.type, this.contract, this.dateadded, this.batterylife);
}
}
}
這就是它!謝謝智能感知通常會阻止我做出這樣的noob錯誤。 – Trinitrotoluene