我是编程新手,正在尝试使用用于 BBEdit 的“TextWrangler”程序(类似于 NotePad++,但适用于 Mac)的 Mono 编译器来学习 C#。但是,我的问题非常简单:我正在尝试用 C# 编写一个非常简单的程序,该程序使用来自不同 .cs 文件的类。
我正在为此苦苦挣扎。如果这些类在同一个 .cs 文件中,我没有问题;否则,bash 终端不断给我错误。这是我的代码:
程序.cs:
// Client class, where Main method calls the Car method
using System;
using Auto;
namespace Auto
{
class Program
{
public static void Main(string[] args)
{
//Instantiate car class
Car car = new Car("Toyota", "Corolla", "Sedan", 2014, 25000);
Console.WriteLine(car.Maintenance());
Console.Read();
}
}
}
汽车.cs:
// "Car" class (does NOT include the Main method)
using System;
namespace Auto
{
public class Car
{
//Create various attributes for Car Class with getters and setters
public string Make { get; set; }
public int Year { get; set; }
public string Model { get; set; }
public string Type { get; set; }
public int Millage { get; set; }
public DateTime PurchaseDate { get; set; }
//Constructor to instantiate Car object that pass all the necessary attribute values
public Car(string make, string model, string _type, int year, int millage)
{
Make = make;
Model = model;
Type = _type;
Millage = millage;
}
//Maintenance method will check all the inspection that the mechanic has to do based on the millage of the car
public string Maintenance()
{
int index = 1;
string maintenanceList = string.Empty;
if (Millage > 5000)
maintenanceList += (index++).ToString() + ". Oil Change " +Environment.NewLine;
if (Millage > 10000)
{
maintenanceList += (index++).ToString() + ". Check Brakes " + Environment.NewLine;
maintenanceList += (index++).ToString() + ". Rotate Tires " + Environment.NewLine;
maintenanceList += (index++).ToString() + ". Lube Hinges, Lock, and Latches" + Environment.NewLine;
}
if (Millage > 15000)
maintenanceList += (index++).ToString() + ". Replace air Cleaner element" + Environment.NewLine;
if (Millage > 30000)
maintenanceList += (index++).ToString() + ". Replace Dust and Pollen Filter" + Environment.NewLine;
return maintenanceList;
}
}
}
所以你去。代码比看起来更简单 - 但每当我尝试编译这些文件时,它们都保存在我的 Finder 中的同一文件夹下,我得到以下结果:
程序.cs:
Compilation failed: 1 error(s), 0 warnings
Program.cs(14,13): error CS0246: The type or namespace name `Car' could not be found. Are you missing an assembly reference?
Program.cs(15,31): error CS0841: A local variable `car' cannot be used before it is declared
汽车.cs:
error CS5001: Program `Car.exe' does not contain a static `Main' method suitable for an entry point
所以,是的,我被困住了。我已经倾注了许多关于 C# 入门类构建的资源,但我的运气一无所获。任何帮助将不胜感激。