运用C# 创建一个职工类
发布网友
发布时间:2022-06-13 06:22
我来回答
共3个回答
热心网友
时间:2023-10-12 11:44
你职工数据存储在那呀?数据库,文本,XML?
类可以这样写
public class worker
{
public worker():this(sting.Empty,0,0){};
public worder(string name,int age,Decimal pay)
{
this.Name = name;
this.Age = age;
this.Pay = pay;
}
public Decimal showpay()
{
Decimal allpay = this.pay;
Decimal returnpay = 0;
if(allpay<=2000)returnpay=allpay;
if(2000<allpay<=5000)returnpay=allpay-allpay*0.05;
if(allpay>5000)returnpay=allpay-allpay*0.1;
if(returnpay>0)return returnpay;
else throw error;//这里处理下异常
}
public string Name{get; private set;};
public int Age{get; private set;};
public Decimal Pay{get; set;}
}
如果你是实际做项目,建议把那个所得税也设置成属性,那样就复杂了,因为国家所得税的税率和征税工资标准是可以变化的,这样考虑就要复杂些。这个你可以自己考虑下,原理就是这样
热心网友
时间:2023-10-12 11:45
public class Worker
{
private double _salary;
private string _name;
public string Name
{
set
{
this._name = value;
}
get
{
return this._name;
}
}
public double Salary
{
set
{
this._salary = value;
}
get
{
return this._salary;
}
}
public Worker(double sl, string WName)
{
this.Name = WName;
this.Salary = sl;
}
public string showpay()
{
double WorkSalary;
if (Salary >= 2000 && Salary <= 5000)
{
WorkSalary = Salary * 0.05;//5%的所得税
}
else if (Salary > 5000)
{
WorkSalary = Salary * 0.1;//10%的所得税
}
else
{
WorkSalary = Salary; //低于2000不扣税
}
return "员工:" + this.Name + " 的工资扣税后为:" + Convert.ToString(WorkSalary);
}
}
热心网友
时间:2023-10-12 11:45
简单点的就这样写吧:
年龄要求有*的写成属性,姓名直接写成公共字段
public class worker
{
/// <summary>
/// 姓名
/// </summary>
public string name;
/// <summary>
/// 年龄
/// </summary>
private int _age;
public int Age
{
set
{
if (value > 0 && value < 150)//判断输入的年龄不能太离谱
_age = value;
}
get { return _age; }
}
/// <summary>
/// 工资
/// </summary>
private float _pay;
public float Pay
{
set
{
if(value>0) Pay = value;//工资不能输入负数
}
get { return _pay; }
}
/// <summary>
/// 计算税后工资
/// </summary>
public float showpay()
{
float shgz = _pay;
if (_pay <= 2000) shgz= _pay;
if (2000 < _pay&&_pay <= 5000) shgz= _pay - (float)(_pay * 0.05);
if (_pay > 5000) shgz= _pay -(float)(_pay * 0.1);
return shgz;
}
}