如何为3个表员工,城市和部门使用界面? [英] How do I use interface for 3 tables employee, city and department?

查看:107
本文介绍了如何为3个表员工,城市和部门使用界面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Employee类中,我定义了以下属性:



 public class Employee 
{
private int m_emp_id;

public int emp_id
{
get {return m_emp_id; }
set {m_emp_id = value; }
}

private String m_emp_Name;

public String emp_Name
{
get {return m_emp_Name; }
set {m_emp_Name = value; }
}

private DateTime m_emp_joiningdate;

public DateTime emp_joiningdate
{
get {return m_emp_joiningdate; }
set {m_emp_joiningdate = value; }
}

private DateTime m_emp_DOB;

public DateTime emp_DOB
{
get {return m_emp_DOB; }
set {m_emp_DOB = value; }
}

private int m_emp_cityId;

public int emp_CityId
{
get {return m_emp_cityId; }
set {m_emp_cityId = value; }
}

private int m_emp_DeptId;

public int emp_DeptId
{
get {return m_emp_DeptId; }
set {m_emp_DeptId = value; }
}

private int m_emp_DesignationId;

public int emp_DesignationId
{
get {return m_emp_DesignationId; }
set {m_emp_DesignationId = value; }
}

private int m_emp_SalaryAmount;

public int emp_SalaryAmount
{
get {return m_emp_SalaryAmount; }
set {m_emp_SalaryAmount = value; }
}
}





在City类我有房产:



公共类城市
{
private int m_city_id;

public int city_id
{
get {return m_city_id; }
set {m_city_id = value; }
}

private String m_city_name;

public String city_name
{
get {return m_city_name; }
set {m_city_name = value; }
}

}





我所拥有的另一个课是部门:



公共类部门
{
private int m_dept_id;

public int dept_id
{
get {return m_dept_id; }
set {m_dept_id = value; }
}

private String m_dept_name;

public String dept_name
{
get {return m_dept_name; }
set {m_dept_name = value; }
}

}





我现在要做的是我想要使用多继承,其中Employee是派生类,department和city是基类,我不想通过使用接口在Employee类中定义emp_cityId,emp_departmentId属性。



我尝试了什么:



我是界面新手所以我不知道如何解决这个问题。

解决方案

您打算从中派生员工?它不是一个城市,它不是一个部门。

从逻辑上思考:一个员工在一个城市工作,他在一个部门工作。许多员工将在同一个城市工作,许多人将在同一个部门工作。但这并不意味着这里有任何继承,或者这里有任何适当的接口。

你将拥有Employee作为基类的接口或派生可能性也许是经理和ShopFloor工人类,但试图强制继承它真正不适用的地方只会使你的设计复杂化而不是简化它。



此外,即使你的强制员工实施ICity界面仍然不意味着你的代码不必填写他每次工作的实际城市 - 实际上你不能实际上是这样做的,因为你无法创建一个界面的实例!



我认为你需要回到你的阅读材料,并刷新什么界面是 - 它们是合同,而不是类!


除了#a href =https://www.codeproject.com/script/Membership/View的soluiton#1。 ASPX?中旬= 6122202\" > 0 riginalGriff [ ^ ],我建议将一些属性从 Employee 类移到另一个类中,例如: EmployementDetails 。有:薪水部门等,因为既没有薪水也不是部门描述员工



假设City类用于存储Employee地址,则必须添加更多详细信息,例如:Zip,State ...

注意,1 Employee可以有1个或更多地址。这对你意味着什么?一个 City 类应该包含 EmpId ,而不是反过来!



Finall note: String [ ^ ]与字符串 [ ^ ]。点击链接查找原因。


Quote:

string 是.NET Framework中 String 的别名。


此时接口不适用。



您正试图在这里建立关系。



您可以添加外键(例如Employee.DepartmentId);或使用显式关系类(例如EmployeeDepartment类......链接员工和部门)。



城市 - 部门关系也可以用类似的方式定义

In Employee class i have defined following properties :

public class Employee
    {
        private int m_emp_id;

        public int emp_id
        {
            get { return m_emp_id; }
            set { m_emp_id = value; }
        }

        private String m_emp_Name;

        public String emp_Name
        {
            get { return m_emp_Name; }
            set { m_emp_Name = value; }
        }

        private DateTime m_emp_joiningdate;

        public DateTime emp_joiningdate
        {
            get { return m_emp_joiningdate; }
            set { m_emp_joiningdate = value; }
        }

        private DateTime m_emp_DOB;

        public DateTime emp_DOB
        {
            get { return m_emp_DOB; }
            set { m_emp_DOB = value; }
        }

        private int m_emp_cityId;

        public int emp_CityId
        {
            get { return m_emp_cityId; }
            set { m_emp_cityId = value; }
        }

        private int m_emp_DeptId;

        public int emp_DeptId
        {
            get { return m_emp_DeptId; }
            set { m_emp_DeptId = value; }
        }

        private int m_emp_DesignationId;

        public int emp_DesignationId
        {
            get { return m_emp_DesignationId; }
            set { m_emp_DesignationId = value; }
        }

        private int m_emp_SalaryAmount;

        public int emp_SalaryAmount
        {
            get { return m_emp_SalaryAmount; }
            set { m_emp_SalaryAmount = value; }
        }
    }



In City class I have properties :

public class City
    {
        private int m_city_id;

        public int city_id
        {
            get { return m_city_id; }
            set { m_city_id = value; }
        }

        private String m_city_name;

        public String city_name
        {
            get { return m_city_name; }
            set { m_city_name = value; }
        }

    }



Another class I have is Department :

public class Department
   {
       private int m_dept_id;

       public int dept_id
       {
           get { return m_dept_id; }
           set { m_dept_id = value; }
       }

       private String m_dept_name;

       public String dept_name
       {
           get { return m_dept_name; }
           set { m_dept_name = value; }
       }

   }



What I want to do now is I want to use multiple inheritance where Employee is derived class and department and city are base classes and I dont want to define emp_cityId,emp_departmentId properties in Employee class by using interface.

What I have tried:

I am new in interface so I dont have much idea how to start with this problem.

解决方案

What are you going to derive Employee from? It isn't a City, it isn't a Department.
Think about it logically: an Employee works in a city, he works in a department. Many employees will work in the same city, many will work in the same department. But that doesn't mean that there is any inheritance here, or that there is any interface which would be appropriate here.
You would have interface or derivation possibilities for "Employee" being a base class for a "Manager" and a "ShopFloor" worker classes perhaps, but trying to force "inheritance" where it really isn't applicable will just complicate your design rather than simplifying it.

And besides, even if your "force" Employee to implement an "ICity" interface that still doesn't mean that your code doesn't have to fill in the actual City he works in each time - in fact you can't actually do that, because you can't create an instance of an interface!

I think you need to go back to your reading materials, and brush up on what Interfaces are - they are a contract, not a class!


In addition to soluiton #1 by OriginalGriff[^], i'd suggest to move some properties from Employee class into another class, for example: EmployementDetails. There is: Salary, Department, etc., because neither Salary nor the Department describe the Employee.

Assuming that City class is used to store Employee address, you have to add more details, such as: Zip, State...
Note, that 1 Employee can have 1 or more addresses. What does it mean to you? A City class should contains EmpId, not conversely!

Finall note: String[^] is not the same as string[^] in C#. Follow the links to find out why.

Quote:

string is an alias for String in the .NET Framework.


Interfaces do not apply at this point.

You're trying to establish "relationships" here.

You can add "foreign keys" (eg. Employee.DepartmentId); or use explicit relationship classes (e.g. an EmployeeDepartment class ... linking an employee and a department).

A city-dept relationship can also be defined in a similar way.


这篇关于如何为3个表员工,城市和部门使用界面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆