2011-07-14 33 views
1

我经历了一个代码示例,其中我看到了创建实例的不同方式。关于OOPS&C中的编码风格#

使代码在这里

public interface IEmployee 
{ 
    System.Int32? EmployeeID { get; set; } 
    System.String FirstName { get; set; } 
    System.String LastName { get; set; } 
    System.DateTime DateOfBirth { get; set; } 
    System.Int32? DepartmentID { get; set; } 
    System.String FullName(); 
    System.Single Salary(); 
} 

public class Employee : IEmployee 
{ 
    #region Properties 

    public System.Int32? EmployeeID { get; set; } 
    public System.String FirstName { get; set; } 
    public System.String LastName { get; set; } 
    public System.DateTime DateOfBirth { get; set; } 
    public System.Int32? DepartmentID { get; set; } 

    #endregion 

    public Employee(
     System.Int32? employeeid 
     , System.String firstname 
     , System.String lastname 
     , System.DateTime bDay 
     , System.Int32? departmentID 
    ) 
    { 
     this.EmployeeID = employeeid; 
     this.FirstName = firstname; 
     this.LastName = lastname; 
     this.DateOfBirth = bDay; 
     this.DepartmentID = departmentID; 
    } 

    public Employee() { } 

    public System.String FullName() 
    { 
     System.String s = FirstName + " " + LastName; 
     return s; 
    } 

    public System.Single Salary() 
    { 
     System.Single i = 10000.12f; 
     return i; 
    } 
} 

    private List<IEmployee> myList = new List<IEmployee> { new Employee(1, "John", "Smith", new DateTime(1990, 4, 1), 1), 
      new Employee(2, "Gustavo", "Achong", new DateTime(1980, 8, 1), 1), 
      new Employee(3, "Maxwell", "Becker", new DateTime(1966, 12, 24), 2), 
      new Employee(4, "Catherine", "Johnston", new DateTime(1977, 4, 12), 2), 
      new Employee(5, "Payton", "Castellucio", new DateTime(1959, 4, 21), 3), 
      new Employee(6, "Pamela", "Lee", new DateTime(1978, 9, 16), 4) }; 

,所以我只是想知道为什么代码应与IEmployee创建名单的情况下,为什么不雇员。

这里我们可以如下代码

private List<Employee> myList = new List<Employee> { new Employee(1, "John", "Smith", new DateTime(1990, 4, 1), 1), 
     new Employee(2, "Gustavo", "Achong", new DateTime(1980, 8, 1), 1), 
     new Employee(3, "Maxwell", "Becker", new DateTime(1966, 12, 24), 2), 
     new Employee(4, "Catherine", "Johnston", new DateTime(1977, 4, 12), 2), 
     new Employee(5, "Payton", "Castellucio", new DateTime(1959, 4, 21), 3), 
     new Employee(6, "Pamela", "Lee", new DateTime(1978, 9, 16), 4) }; 

它也可以....他们为何要使用IEmployee。 为什么编码器使用IEmployee而不是Employee,所以必须有一些特定的目标。所以我只需要知道使用IEmployee的目标。 我在寻找很好的解释。请指导我。谢谢。

+1

我甚至会用IList 。 ;) – tafa

+0

我会说 - 这是货物崇拜节目或一些程序员正面临的奇怪的技术问题。对继承也可能是不合理的恐惧。 –

回答

1

您说你想要一些代码。我会告诉你一些(为了答案略有不同)。

假设你有

public interface IPlanet { 
    string Name {get;} 
} 

然后专注该接口多次:

public class Mars : IPlanet { public string Name { get { return "Mars"; } } } 
public class Venus : IPlanet { public string Name { get { return "Venus"; } } } 
public class Jupiter : IPlanet { public string Name { get { return "Jupiter";} } } 
public class Earth : IPlanet { public string Name { get { return "Earth"; } } } 

然后,你可以这样做:

List<IPlanet> solarsystem = new List<IPlanet> { 
    new Mars(), 
    new Venus(), 
    new Jupiter(), 
    new Earth(), 
}; 

而且,因为它们共享相同的接口,你可以这样做:

foreach (var planet in solarsystem) { 
    Console.WriteWrite (planet.Name); 
} 

因此,从某种意义上说,接口可以帮助您以均匀的方式管理异构对象。这也是一种(多种)通用的,可重复使用的编程(不要看维基百科网站,它将泛型编程与模板和泛型等同起来)。

注意,一个List实现IList - 接口,这样你就可以更加概括:

IList<IPlanet> solarsystem = new List<IPlanet> { 
    new Mars(), 
    new Venus(), 
    new Jupiter(), 
    new Earth(), 
}; 

对于你是否应该或不看List<T> or IList<T>

8

使用List<IEmployee>允许代码保持灵活性,如果您想要多个实现IEmployee。例如,您可能希望有Employee : IEmployeeManager : IEmployee,并且使用List<IEmployee>允许列表中包含两个

+0

谢谢,但如果有人解释小样本代码以更好地理解它,我会很高兴。 – Mou