-1

我有两个不同的班级名称员工和员工。我有一个接口名称 IEmployeeService 这里包括两个方法名称 Get(With Parameter) & Gets()。这个接口在另一个类名EmployeeService 中实现。现在我需要从我的员工类调用我的接口方法“Get()”,从我的员工类调用“Gets()”。有人帮我吗?

我的代码是:

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
   //My first Class 
    public class Employee
    {
        public Employee() { }
        public int EmployeeID { get; set; }
        public string EmployeeName { get; set; }
        public string Address { get; set; }
        public double Salary { get; set; }

        //This is a method I want to call My IEmployeeService Get Method from here 
        public Employee Get(int nEmployeeID)
        {
            //Try to call Interface Get Method
        }

    }

    //My Second Class
    public class Employees : CollectionBase
    {
        public void Add(Employee oItem)
        {
            List.Add(oItem);
        }
        public void Remove(Employee oItem)
        {
            List.Remove(oItem);
        }

        //This is my another method I want to call My IEmployeeService Gets Method from here 
        public static Employees Gets()
        {
            //Try to call Interface Gets Method
        }
    }



    //My Interface 
    public interface IEmployeeService
    {
        Employee Get(int nEmployeeID);
        Employees Gets();
    }



    // My Interface implementation Class 
    public class EmployeeService : IEmployeeService
    {

        public EmployeeService() { }

        #region IEmployeeService Members
        public Employee Get(int nEmployeeID)
        {
            Employee oEmployee = new Employee();
            oEmployee.EmployeeID = 1;
            oEmployee.EmployeeName = "Mohammed Faruk";
            oEmployee.Address = "Comilla, Bangladesh";
            oEmployee.Salary = 50000.00;
            return oEmployee;
        }

        public Employees Gets()
        {
            Employees oEmployees = new Employees();
            Employee oEmployee = new Employee();
            for (int i = 1; i <= 10; i++)
            {
                oEmployee = new Employee();
                oEmployee.EmployeeID = i;
                oEmployee.EmployeeName = i.ToString() + "th Mohammed Faruk";
                oEmployee.Address = i.ToString() + "Comilla, Bangladesh";
                oEmployee.Salary = 50000.00;
                oEmployees.Add(oEmployee);
            }
            return oEmployees;
        }
        #endregion
    }
}
4

1 回答 1

0

您可能需要有一个 EmployeeService 的单例实例,如下所示(由您决定使用不同的实现,为简单起见,我使用了这种方法)

public class EmployeeService : IEmployeeService
{
      private readonly static EmployeeService _instance = new EmployeeService();

      public static EmployeeService Instance
      {
          get { return _instance; }
      }
}

现在将其用于 Employee 和Employees 类,如下所示。

public class Employee
{
    //This is a method I want to call My IEmployeeService Get Method from here 
    public static Employee Get(int nEmployeeID)
    {
        return EmployeeService.Instance.Get(nEmployeeID);
    }

}

public class Employees
{
    //This is a method I want to call My IEmployeeService Get Method from here 
    public static Employees Gets()
    {
        return EmployeeService.Instance.Gets();
    }
}

我希望这就是你要找的。

于 2012-05-03T12:03:50.737 回答