2011-07-14 54 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