好的,我在这里尝试做的是创建一个类数组。我的类TEAMLEADER派生自一个抽象类Employee。但是当我尝试在Main中创建一个TEAMLEADER实例时,我收到一条错误消息,指出无法找到TEAMLEADER。
namespace Lab3
{
public abstract class Employee
{
protected string EmployeeName;
protected int EmployeeNumber;
protected double WeeklySalary;
public Employee (string EmployeeName, int EmployeeNumber, double WeeklySalary)
{
this.EmployeeName = EmployeeName;
this.EmployeeNumber = EmployeeNumber;
this.WeeklySalary = WeeklySalary;
}
public Employee(string EmployeeName)
{
assignID(EmployeeNumber);
}
public override string ToString()
{
return EmployeeName + " " + EmployeeNumber + " " + WeeklySalary;
}
protected virtual double CalcSalary()
{
return CalcSalary();
}//"Virtual" is a keyword that says, "This can be overriden in the derived class."
private static int assignID(int EmployeeNumber)
{
EmployeeNumber.ToString();
EmployeeNumber++;
return EmployeeNumber;
}
}
class Program
{
static void Main(string[] args)
{
Employee[] workerarray = new Employee[4];
workerarray[0] = new TeamLeader("Rachel", 18, 1000000.00, 52000000.00, true);
}
}
}
在单独的类/选项卡中是TEAMLEADER类。
public class TeamLeader:Employee
{
protected double AnnualSalary;
protected bool WeeklyGoal;
public override void CalcSalary()
{
if (WeeklyGoal == true)
{ CalcSalary = (AnnualSalary / 52) * 1.10; }
else (CalcSalary = AnnualSalary / 52);
}
public TeamLeader(string EmployeeName, int EmployeeNumber, double WeeklySalary, double AnnualSalary, bool WeeklyGoal):base(EmployeeName, EmployeeNumber, WeeklySalary)
{
this.WeeklyGoal = WeeklyGoal;
this.AnnualSalary = AnnualSalary;
}
}
问题在于我的主要方法。我无法理解为什么我无法创建TeamLeader的实例。它不是一个抽象类,所以不应该主要能够识别它并创建一个实例吗?
答案 0 :(得分:0)
您是否尝试过添加:
clicked
到Main类文件的顶部?
答案 1 :(得分:0)
试试这个
确保将:sections
添加到TeamLeader类所在文件的顶部。然后将此行代码namespace Lab3
添加到主文件的顶部。
如果这不起作用,请尝试此操作。
Maby您将TeamLeader类添加到其他程序集中 要解决这个问题。
单击主要所在的程序集中的引用
点击添加参考
点击Projects>解决方案并添加您的程序集
如果有效,请告诉我
答案 2 :(得分:0)
你班上有公共默认构造函数吗?
public Employ()
{
}
当命令
Employee[] workerarray = new Employee[4];
执行后,需要创建4个Employ
类型的新对象,但由于没有无参数构造函数,因此无法创建。