学习目标
掌握c#语言委托的用法
学习内容
请使用委托实现信用卡用户定时还款功能
源代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 委托与事件
{
class user
{
private int cnm;
private string name;
private int demoney;
private int balance;
public int date;
private int ddl = 11;
public user(int cnm, string name, int demoney, int balance, int date)
{
this.cnm= cnm;
this.name = name;
this.demoney = demoney;
this.balance = balance;
this.date = date;
}
public int getddl()
{
return this.ddl;
}
public void Repay()
{
int Nowmoney;
Console.WriteLine("{0}用户:需要还款{1}元,当前余额为{2}", this.name, this.demoney, this.balance);
Nowmoney = this.balance - this.demoney;
if (Nowmoney < 0)
{
Console.WriteLine("用户{0}余额不足,无法还款", this.name);
}
else
{
Console.WriteLine("用户{0}已成功还款,还款后卡中余额为{1}", this.name, Nowmoney);
}
}
}
class dele
{
public delegate void card();
public event card dMoney;
public void test()
{
if (dMoney != null)
{
dMoney();
}
}
}
class Program
{
static void Main(string[] args)
{
dele DD = new dele();
user u1 = new user(001, "zzf", 100, 250, 11);
if (u1.date == u1.getddl())
{
DD.dMoney += new dele.card(u1.Repay);
}
else Console.WriteLine("没有到还款日");
DD.test();
}
}
}
实验结果
实验心得
在这次实验中,掌握了委托和事件的相关定义以及用法。通过定义一个用户类来定义出所有对象,之后在定义一个日期的还款函数来实现信用卡和显示输出函数来给予用户显示信息,再使用委托与事件的捆绑,主函数来输入用户信息,从而完成该用户的相关还款事项。一步一步来操作完成其功能。
gitee地址
gitee地址:https://gitee.com/zzflovemm/programming-windows-