Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
关于OOPS中的编码样式&;C#_C#_Oop - Fatal编程技术网

关于OOPS中的编码样式&;C#

关于OOPS中的编码样式&;C#,c#,oop,C#,Oop,我浏览了一个代码示例,其中我看到了创建实例的不同方法 所以在这里编码 public interface IEmployee { System.Int32? EmployeeID { get; set; } System.String FirstName { get; set; } System.String LastName { get; set; } System.DateTime DateOfBirth { get; set; } System.Int

我浏览了一个代码示例,其中我看到了创建实例的不同方法

所以在这里编码

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) };
公共接口IEEmployee
{
System.Int32?EmployeeID{get;set;}
System.String FirstName{get;set;}
System.String LastName{get;set;}
System.DateTime出生日期{get;set;}
System.Int32?部门ID{get;set;}
字符串FullName();
系统。单一工资();
}
公共类员工:IEEmployee
{
#区域属性
public System.Int32?EmployeeID{get;set;}
public System.String FirstName{get;set;}
public System.String LastName{get;set;}
public System.DateTime出生日期{get;set;}
public System.Int32?部门ID{get;set;}
#端区
公职人员(
System.Int32?员工ID
,System.String firstname
,System.String lastname
,System.DateTime bDay
,System.Int32?部门ID
)
{
this.EmployeeID=EmployeeID;
this.FirstName=FirstName;
this.LastName=LastName;
this.DateOfBirth=b天;
this.DepartmentID=部门ID;
}
公共雇员(){}
public System.String FullName()
{
System.String s=FirstName+“”+LastName;
返回s;
}
公共系统。单一工资()
{
系统。单个i=10000.12f;
返回i;
}
}
私有列表myList=新列表{新员工(1,“约翰”,“史密斯”,新日期时间(1990,4,1),1),
新员工(2名,“古斯塔沃”,“阿雄”,新日期时间(1980年8月1日),1名),
新员工(3名,“麦克斯韦”,“贝克尔”,新日期时间(1966年12月24日),2名),
新雇员(4,“凯瑟琳”,“约翰斯顿”,新日期时间(1977年4月12日),2),
新员工(5名,“佩顿”,“卡斯特卢西奥”,新日期时间(1959年4月21日),3名),
新雇员(6名,“帕梅拉”,“李”,新日期时间(1978年9月16日),4名);
所以我只想知道为什么代码应该用IEEmployee创建list的实例,为什么不是Employee

在这里,我们可以像

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) };
private List myList=new List{new Employee(1,“John”,“Smith”,new DateTime(1990,4,1),1),
新员工(2名,“古斯塔沃”,“阿雄”,新日期时间(1980年8月1日),1名),
新员工(3名,“麦克斯韦”,“贝克尔”,新日期时间(1966年12月24日),2名),
新雇员(4,“凯瑟琳”,“约翰斯顿”,新日期时间(1977年4月12日),2),
新员工(5名,“佩顿”,“卡斯特卢西奥”,新日期时间(1959年4月21日),3名),
新雇员(6名,“帕梅拉”,“李”,新日期时间(1978年9月16日),4名);
这也行……他们为什么要用IEmployee。 为什么编码器使用的是IEmployee而不是Employee,所以一定有特定的目标。所以我只需要知道使用IEmployee的目的。
我在寻找好的解释。请引导我。谢谢。

使用
列表
可以使代码保持灵活,如果您想要实现多个
IEEmployee
。例如,您可能需要
Employee:ieemployee
Manager:ieemployee
,并且使用
List
允许列表同时包含这两个使用
列表
允许代码保持灵活,如果您想要有多个
ieemployee
的实现。例如,您可能需要
Employee:ieemployee
Manager:ieemployee
,并且使用
List
可以让列表同时包含这两种代码您说需要一些代码。我会给你看一些(为了回答的缘故有些不同)

假设你有

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);
}
因此,在某种意义上,接口可以帮助您以同构的方式管理异构对象。这也是一种(许多)通用的、可重用的编程(不要看维基百科网站,它将通用编程等同于模板和泛型)

请注意,
列表
实现了
IList
-接口,因此您可以进一步概括:

IList<IPlanet> solarsystem = new List<IPlanet> {
    new Mars(),
    new Venus(),
    new Jupiter(),
    new Earth(),
};
IList solarsystem=新列表{
新火星(),
新维纳斯(),
新木星(),
新地球(),
};

有关是否应该,请参阅。

您说过需要一些代码。我会给你看一些(为了回答的缘故有些不同)

假设你有

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);
}
因此,在某种意义上,接口可以帮助您以同构的方式管理异构对象。这也是一种(许多)通用的、可重用的编程(不要看维基百科网站,它将通用编程等同于模板和泛型)

请注意,
列表
实现了
IList
-接口,因此您可以进一步概括:

IList<IPlanet> solarsystem = new List<IPlanet> {
    new Mars(),
    new Venus(),
    new Jupiter(),
    new Earth(),
};
IList solarsystem=新列表{
新火星(),
新维纳斯(),
新木星(),
新地球(),
};

至于您是否应该,请参见。

我甚至会使用IList。)我想说的是,这就是cargo cult编程或程序员面临的一些奇怪的技术问题。也可能是对遗产的无理恐惧。我甚至会使用IList我想说的是,这就是cargo cult编程或程序员面临的一些奇怪的技术问题。可能对继承的恐惧也是不合理的。谢谢,但如果有人用小样本代码解释以便更好地理解它,我会很高兴。谢谢,但如果有人用小样本代码解释以便更好地理解它,我会很高兴。