';员工';是一个';命名空间';而是像';键入';
本文关键字:命名空间 键入 一个 员工 | 更新日期: 2023-09-27 18:21:18
"Employee"是一个"namespace",但使用起来像"type"。我似乎无法纠正这4个错误。帮助任何人?第12行和第27行。我把解释放在两条错误线上。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Employee
{
class Program
{
static void Main(string[] args)
{
Employee firstEmployee = new Employee(); **(2 errors on line for Employee)**
ApplicationUtilities.DisplayApplicationInformation(); //display the header for the application
ApplicationUtilities.DisplayDivider("Start Program"); //Show heading that the program has started
ApplicationUtilities.DisplayDivider("Prompt for employee information and create first employee"); //Heading that shows we are ready to input first employee information
firstEmployee.firstName = InputUtilities.getStringInputValue("First Name"); //Get first name input from the user
firstEmployee.lastName = InputUtilities.getStringInputValue("Last Name");//Get last name input from the user
firstEmployee.gender = InputUtilities.getCharInputValue("Gender");//Get gender input from the user
firstEmployee.dependents = InputUtilities.getIntegerInputValue("# Dependents");//Get dependent input from the user
firstEmployee.annualSalary = InputUtilities.getDoubleInputValue("Annual Salary");//Get annual salary input from the user
Console.WriteLine("");
Console.Write(firstEmployee.ToString());
ApplicationUtilities.PauseExecution();
Employee secondEmployee = new Employee("First Name", "Last Name", 'F', 3, 52000); ////declare an instance of second employee object with overloaded constructor called **(2 errors on this line for - Employee)**
Console.Write(secondEmployee.ToString());
ApplicationUtilities.TerminateApplication();
}
}
}
正如错误所示,名称空间和类共享相同的名称。编译器对您试图实例化的具体内容感到困惑。
快速修复:在创建Employee
的新实例时指定命名空间,这样编译器就知道您想在同名命名空间中创建类的新实例。
Employee.Employee firstEmployee = new Employee.Employee();
正确的修复方法:重命名名称空间,以便更容易地实例化类。
你可能还想从一个对编译器非常了解的人那里读到这篇文章:不要将类的名称与其命名空间相同
FWIW,这种大小的小型应用程序在技术上不需要命名空间。但是(正如Neolisk所指出的)如果你确实使用了一个,那么就使用一个好的。随着程序的发展,您可以在名为Entity
的命名空间中对表示人员的类(如Employee
)进行分组。
重命名Employee命名空间或重命名Employme类。