在函数调用中包含类名

J P*_*sen -1 c#

我只是在使用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在该行?

D S*_*ley 5

因为printVehicleDetails上的Vehicle静态方法.当您调用静态方法(在您所在的类之外的类上)时,您需要包含类名,以便编译器知道要绑定到哪个方法.