如何在C#中实现具有继承的类组合 [英] How to Implement class composition with inheritance in C#

查看:194
本文介绍了如何在C#中实现具有继承的类组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是面向对象开发的新手,并感谢您在这方面的帮助。在我的工资单系统中,我有四个类别叫做付款,薪水,收入和扣除。准备工资需要多种类型的收入(如OT,奖励,空间津贴,额外班次付款,预算津贴,预付金额等)和扣除(如保险,统一,贷款,恢复,福利,膳食,提前工资等)因此,我将其分为两类,即收入和扣除。最后,班级薪水应该做平衡部分,班级付款将处理付款部分(银行)。其他类型的付款是工资预付款,这只是每月支付的金额。建模的关系如下:收入和扣除类与薪水有组合关系,薪水是来自付款的派生类。我已经在我的C#代码中实现了这些关系,如下所示......我的问题是



1.我是否以正确的方式关联课程?



2.关系的实施是否正确?



付款



I am new to object oriented development and appreciate your help in this regard. In my payroll system, I have four classes called ‘Payment’, ‘Salary’, ‘Earning’ and ‘Deduction’. preparing salary requires many types of earnings (like OT, Incentives, Spacial Allowance, payments for extra shifts, budgetary allowances, Brought forward amounts etc.) and deductions (like Insurance, Uniform, Loans,Recovery, Welfare, meals, salary advance taken etc.) So I split this in to two classes namely 'Earning' and 'Deduction'. Finally class 'Salary' is supposed to do the balancing part and class 'payment' will handle payment part (banking). Other type of payment is salary advance which is just an amount paid monthly. the relationships modeled, as follows… ‘Earning’ and ‘Deduction’ classes has a composition relationship with ‘Salary’ and ‘Salary’ is a derived class from ‘Payment’. I have implemented these relationships in my C# code as follows… My questions are

1.Whether I ve related classes in a proper way?

2.Is implementation of the relationships correct?

PAYMENTS

class payment
    {
        public virtual void pay()
        {

        }
}





SALARY





SALARY

class salary:payment
{
//Composition of ‘Earning’ class
    private earning E1;

    public salary()
    {
        E1 = new earning(this);

    }
//Calling calEarning() of ‘Earning’ class through ‘Salary’ class
    public void calEarningForSal()
    {
        E1.calEarning();
    }

//Implementing pay method of base class 
    public override void pay()
    {

    }
}





收益





EARNING

public class earning
   {
   //Since earning has a composition relationship with salary…
       private salary S1;

       internal earning(salary sal)
       {
           this.S1 = sal;
       }
   //This method will calculate earnings of employees
       public void calEarning()
       {

       }
   }





主要方法





MAIN METHOD

Main()
        {
            salary newSal = new salary();
            newSal.calEarningForSal();
        }

推荐答案

我想在这里添加一件事。

你可以创建一个父母班级头部或工薪头。

班级薪酬头部{}

班级收入:SalaryHead {}

班级扣除:SalaryHead {}

Main {我们可以使用抽象工厂在运行时创建对象}



但是在工资单系统中这不是全部,我认为我们需要更多的课程和继承来设计工资单系统。就像一个重要的类是员工,它应该与薪资类相关。
Well one thing I want to add here.
You can create a parent class Head or salaryhead.
Class SalaryHead { }
Class Earning:SalaryHead{}
Class Deduction : SalaryHead {}
Main { we can use abstract factory to create objects at run time}

But in payroll system this is not all, I think we need more classes and inheritance to design a payroll system. Like one important class is employee and it should be related with payroll classes.


这篇关于如何在C#中实现具有继承的类组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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