我只是在使用Microsoft Virtual Academy学习C#.这是我正在使用的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Variables
{
class Program
{
static void Main(string[] args)
{
Car myCar = new Car("BWM", "745li", "Blue", 2006);
printVehicleDetails(myCar);
Console.ReadLine();
}
}
abstract class Vehicle
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public string Color { get; set; }
public abstract string FormatMe();
public static void printVehicleDetails(Vehicle vehicle)
{
Console.Writeline("Here are the vehicle's details: {0}", vehicle.FormatMe());
}
}
class Car : Vehicle
{
public Car(string make, string model, string color, int year)
{
Make = make;
Model = model
Color = color;
Year = year;
}
public override string FormatMe()
{
return string.Format("{0} - {1} - {2} - {3}",
this.Make,
this.Model,
this.Color,
this.year);
}
}
Run Code Online (Sandbox Code Playgroud)
无论如何,我遇到的问题源于这条线printVehicleDetails(myCar).当我尝试构建项目时,我得到错误"当前上下文中不存在名称'printVehicleDetails'.
我可以通过将行更改为来修复错误Vehicle.printVechicleDetails(myCar).
有谁知道为什么我必须包括Vehicle在该行?
| 归档时间: |
|
| 查看次数: |
105 次 |
| 最近记录: |