从两个不同的类访问相同的接口哪个接口在另一个类中实现。

时间:2012-05-03 11:54:26

标签: c# interface

我有两个不同的班级姓名员工&雇员。我有一个接口名称IEmployeeService这里包含两个方法名称Get(With Parameter)&获取()。这个接口是另一个类名EmployeeService的工具。现在我需要从我的Employee类调用我的接口方法“Get()”和从Employees类调用“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
    }
}

1 个答案:

答案 0 :(得分: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();
    }
}

我希望这就是你要找的东西。