在不同的 class 中创建 Abstract Class 的实例
Creating an instance of Abstract Class in a different class
我正在尝试创建抽象实例 class(员工)
public abstract class Employee
{
public Employee() { }
public static SchoolEmployeesContext ctx = new SchoolEmployeesContext();
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
public string EmployeeSurname { get; set; }
public string Address { get; set; }
public int Grade { get; set; }
public double Salary { get; set; }
public DateTime DateOfCommencement { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public Department DepartmentOfEmployee { get; set; }
}
现在我正在尝试从上下文中获取员工,以实现多种功能,例如编辑和删除员工。在方法中声明 Employee 为 null 允许删除 "Unassigned" 错误。但是虽然 运行 这显然给了我一个 "null reference exception"
查找员工的方法如下:
public Employee Search(int EmpId)
{
var Emp = from emp in ctx.Employees
where emp.EmployeeId == EmpId
select emp;
Employee Found = null;
foreach (Employee e in Emp)
{
Found.EmployeeName = e.EmployeeName;
Found.EmployeeSurname = e.EmployeeSurname;
Found.Address = e.Address;
Found.Grade = e.Grade;
Found.Salary = e.Salary;
Found.DateOfCommencement = e.DateOfCommencement;
Found.Username = e.Username;
Found.Password = e.Password;
Found.DepartmentOfEmployee = e.DepartmentOfEmployee;
}
return Found;
}
抽象 classes 无法实例化。
它们可以被继承,被继承的类型可以被实例化,但不能直接实例化。
看看你的代码,这就是(我认为)你想要做的:
从您的 class 定义中删除 abstract
此外,您的 Employee
class -
中有一个完全未使用的静态
public static SchoolEmployeesContext ctx = new SchoolEmployeesContext();
所以我建议删除它。
您的 Employee
class 则变为:
public class Employee
{
public Employee() { }
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
public string EmployeeSurname { get; set; }
public string Address { get; set; }
public int Grade { get; set; }
public double Salary { get; set; }
public DateTime DateOfCommencement { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public Department DepartmentOfEmployee { get; set; }
}
此外,您的 Search
方法看起来有点可疑。
您正在从数据库中选择许多 名员工,其中EmployeeId
等于提供的EmpId
我觉得不对。
您可以使用 LINQ SingleOrDefault
通过 ID 检索单个项目(如果不存在,则为 null)
所以你的 Search
方法现在看起来像
public Employee Search(int EmpId)
{
//this will return the single employee
var Emp = ctx.Employees.SingleOrDefault(x=> x.EmployeeId == EmpId);
return Emp;
}
您不能实例化抽象 类。您可以在此处找到有关它们的更多信息:https://msdn.microsoft.com/en-us/library/k535acbf(v=vs.71).aspx
Abstract classes are closely related to interfaces. They are classes that cannot be instantiated, and are frequently either partially implemented, or not at all implemented.
不,您不能创建抽象实例 class,因为它没有完整的实现。摘要 class 的目的是作为子 class 的基础。它就像一个模板,或者一个空的或部分空的结构,您应该在使用
之前扩展并构建它
抽象 classes 无法实例化。在您提供的代码中,Employee 不需要是抽象的,因此您应该从 Employee class 定义中删除抽象修饰符。
正如其他人指出的那样,您的搜索方法似乎有误,应更改为 select 与员工 ID 匹配的员工实体,如果找到,则将该实体映射到员工。像这样:
public Employee Search(int EmpId)
{
// Return the employee with the correct Id (or null if not found).
var employeeEntity = ctx.Employees.SingleOrDefault(x=> x.EmployeeId == EmpId);
return employeeEntity == null
? null
: new Employee
{
EmployeeName = employeeEntity.EmployeeName,
EmployeeSurname = employeeEntity.EmployeeSurname,
...
};
}
}
应该会给你一个可行的解决方案。
我正在尝试创建抽象实例 class(员工)
public abstract class Employee
{
public Employee() { }
public static SchoolEmployeesContext ctx = new SchoolEmployeesContext();
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
public string EmployeeSurname { get; set; }
public string Address { get; set; }
public int Grade { get; set; }
public double Salary { get; set; }
public DateTime DateOfCommencement { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public Department DepartmentOfEmployee { get; set; }
}
现在我正在尝试从上下文中获取员工,以实现多种功能,例如编辑和删除员工。在方法中声明 Employee 为 null 允许删除 "Unassigned" 错误。但是虽然 运行 这显然给了我一个 "null reference exception"
查找员工的方法如下:
public Employee Search(int EmpId)
{
var Emp = from emp in ctx.Employees
where emp.EmployeeId == EmpId
select emp;
Employee Found = null;
foreach (Employee e in Emp)
{
Found.EmployeeName = e.EmployeeName;
Found.EmployeeSurname = e.EmployeeSurname;
Found.Address = e.Address;
Found.Grade = e.Grade;
Found.Salary = e.Salary;
Found.DateOfCommencement = e.DateOfCommencement;
Found.Username = e.Username;
Found.Password = e.Password;
Found.DepartmentOfEmployee = e.DepartmentOfEmployee;
}
return Found;
}
抽象 classes 无法实例化。
它们可以被继承,被继承的类型可以被实例化,但不能直接实例化。
看看你的代码,这就是(我认为)你想要做的:
从您的 class 定义中删除 abstract
此外,您的 Employee
class -
public static SchoolEmployeesContext ctx = new SchoolEmployeesContext();
所以我建议删除它。
您的 Employee
class 则变为:
public class Employee
{
public Employee() { }
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
public string EmployeeSurname { get; set; }
public string Address { get; set; }
public int Grade { get; set; }
public double Salary { get; set; }
public DateTime DateOfCommencement { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public Department DepartmentOfEmployee { get; set; }
}
此外,您的 Search
方法看起来有点可疑。
您正在从数据库中选择许多 名员工,其中EmployeeId
等于提供的EmpId
我觉得不对。
您可以使用 LINQ SingleOrDefault
通过 ID 检索单个项目(如果不存在,则为 null)
所以你的 Search
方法现在看起来像
public Employee Search(int EmpId)
{
//this will return the single employee
var Emp = ctx.Employees.SingleOrDefault(x=> x.EmployeeId == EmpId);
return Emp;
}
您不能实例化抽象 类。您可以在此处找到有关它们的更多信息:https://msdn.microsoft.com/en-us/library/k535acbf(v=vs.71).aspx
Abstract classes are closely related to interfaces. They are classes that cannot be instantiated, and are frequently either partially implemented, or not at all implemented.
不,您不能创建抽象实例 class,因为它没有完整的实现。摘要 class 的目的是作为子 class 的基础。它就像一个模板,或者一个空的或部分空的结构,您应该在使用
之前扩展并构建它抽象 classes 无法实例化。在您提供的代码中,Employee 不需要是抽象的,因此您应该从 Employee class 定义中删除抽象修饰符。
正如其他人指出的那样,您的搜索方法似乎有误,应更改为 select 与员工 ID 匹配的员工实体,如果找到,则将该实体映射到员工。像这样:
public Employee Search(int EmpId)
{
// Return the employee with the correct Id (or null if not found).
var employeeEntity = ctx.Employees.SingleOrDefault(x=> x.EmployeeId == EmpId);
return employeeEntity == null
? null
: new Employee
{
EmployeeName = employeeEntity.EmployeeName,
EmployeeSurname = employeeEntity.EmployeeSurname,
...
};
}
}
应该会给你一个可行的解决方案。