C#:Can';I don’我似乎对一个编译错误束手无策
我在“publicstaticvoidpromoteemployee(List-employeeList,IsPromotable-isligibletopromote)”中的“PromoteEmployee”中遇到了一个编译问题 如果有人能告诉我该怎么做,我将不胜感激 编辑: 错误是: 可访问性不一致:参数类型“Program.IsPromotable”的可访问性不如方法“Program.Employee.PromoteEmployee(列表,Program.IsPromotable)”的可访问性C#:Can';I don’我似乎对一个编译错误束手无策,c#,C#,我在“publicstaticvoidpromoteemployee(List-employeeList,IsPromotable-isligibletopromote)”中的“PromoteEmployee”中遇到了一个编译问题 如果有人能告诉我该怎么做,我将不胜感激 编辑: 错误是: 可访问性不一致:参数类型“Program.IsPromotable”的可访问性不如方法“Program.Employee.PromoteEmployee(列表,Program.IsPromotable)”的可访
类程序
{
静态void Main(字符串[]参数)
{
List empList=新列表();
empList.Add(新员工()
{
ID=101,
Name=“Test1”,
工资=5000,
经验=5
});
empList.Add(新员工()
{
ID=101,
Name=“Test2”,
薪金=2000,
经验=1
});
empList.Add(新员工()
{
ID=101,
Name=“Test3”,
工资=4000,
经验=4
});
IsPromotable IsPromotable=新IsPromotable(升级);
员工。提拔员工(员工,可提拔);
}
公共静态bool升级(员工emp)
{
如果(环境管理经验>=5)
{
返回true;
}
其他的
{
返回false;
}
}
代表bool可提议(员工员工);
公营雇员
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公共整数{get;set;}
公共int经验{get;set;}
public static void promoteeemployee(列出employeeList,IsPromotable isligibletopromote)
{
foreach(员工列表中的员工)
{
如果(IsEligibleToPromotion(员工))
{
Console.WriteLine(employee.Name+“promoted”);
}
}
}
}
}
编译您的程序时出现的错误是:
(67:28) Inconsistent accessibility: parameter type 'Program.IsPromotable' is less accessible than method 'Program.Employee.PromoteEmployee(System.Collections.Generic.List<Program.Employee>, Program.IsPromotable)'
(67:28)可访问性不一致:参数类型“Program.IsPromotable”的可访问性不如方法“Program.Employee.promoteeEmployee(System.Collections.Generic.List,Program.IsPromotable)”的可访问性
发生此错误的原因是PromoteEmployee
是public
,而IsPromotable
是private
如果班外程序的某个人想调用promoteeemployee
,他不能这样做,因为他不能创建程序的实例。IsPromotable
,因为它是程序的私有对象
--
要修复它,请使IsPromotable
internal
或public
,以便程序之外的其他人可以创建它
或者,您可以将promoteeemployee
设置为私有。这是一个工作的dotnetfiddle。
使用系统;
使用System.Collections.Generic;
公共课程
{
公共静态void Main(字符串[]args)
{
List empList=新列表();
empList.Add(新员工()
{
ID=101,
Name=“Test1”,
工资=5000,
经验=5
});
empList.Add(新员工()
{
ID=101,
Name=“Test2”,
薪金=2000,
经验=1
});
empList.Add(新员工()
{
ID=101,
Name=“Test3”,
工资=4000,
经验=4
});
IsPromotable IsPromotable=新IsPromotable(升级);
员工。提拔员工(员工,可提拔);
}
公共静态bool升级(员工emp)
{
如果(环境管理经验>=5)
{
返回true;
}
其他的
{
返回false;
}
}
公共代表bool可推荐(员工员工);
公营雇员
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公共整数{get;set;}
公共int经验{get;set;}
public static void promoteeemployee(列出employeeList,IsPromotable isligibletopromote)
{
foreach(员工列表中的员工)
{
如果(IsEligibleToPromotion(员工))
{
Console.WriteLine(employee.Name+“promoted”);
}
}
}
}
}
告诉我们编译器错误是什么,给我们一个提示。错误是什么?它发生在哪一行?请给我们一些提示。是这一行吗IsPromotable IsPromotable=new IsPromotable(promotable)
??代理是私有的。可能重复@DStanley您是对的,感谢您的提示,如果有来自外部类程序的人想调用PromoteEmployee
,他不能这样做,因为他不能创建程序的实例。IsPromotable
,因为它是程序的私有对象。因此编译器警告您不要这样做你在做一些毫无意义的事情。谢谢@EdPlunkett,回答中引用了很好的描述。@Josepharl谢谢你
(67:28) Inconsistent accessibility: parameter type 'Program.IsPromotable' is less accessible than method 'Program.Employee.PromoteEmployee(System.Collections.Generic.List<Program.Employee>, Program.IsPromotable)'
using System;
using System.Collections.Generic;
public class Program
{
public static void Main(string[] args)
{
List<Employee> empList = new List<Employee>();
empList.Add(new Employee()
{
ID = 101,
Name = "Test1",
Salary = 5000,
Experience = 5
});
empList.Add(new Employee()
{
ID = 101,
Name = "Test2",
Salary = 2000,
Experience = 1
});
empList.Add(new Employee()
{
ID = 101,
Name = "Test3",
Salary = 4000,
Experience = 4
});
IsPromotable isPromotable = new IsPromotable(Promote);
Employee.PromoteEmployee(empList, isPromotable);
}
public static bool Promote(Employee emp)
{
if (emp.Experience >= 5)
{
return true;
}
else
{
return false;
}
}
public delegate bool IsPromotable(Employee empl);
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public int Experience { get; set; }
public static void PromoteEmployee(List<Employee> employeeList, IsPromotable IsEligibleToPromote)
{
foreach (Employee employee in employeeList)
{
if (IsEligibleToPromote(employee))
{
Console.WriteLine(employee.Name + " promoted");
}
}
}
}
}