我目前正在学习C#,并且我试图了解类继承,并编写了一些代码来向控制台显示一行,但我遇到了错误的问题。这个帖子主题。
它在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);
}
}
}
答案 0 :(得分:0)
将(
替换为{
( return String.Format("
行中的0} - {1} - {2} - {3} - {4}"...
。
所以
return String.Format("
( 0} - {1} - {2} - {3} - {4}"...
˙将是return String.Format("
{ { {1}}