第一步,创建一个类CreditCardDelegate,定义事件,并判断事件是否启用
第二步,创建一个user类,进行初始化,并为每个函数定义一个get和set的方法
第三步,创建一个bank类,进行还款
第四步,在main里面将事件与委托关联起来
using System;
namespace Text
{
class CreditCardDelegate
{
public delegate void card(User user);
public event card DeMoney;//事件绑定委托
//判断事件是否启用
public void DeMoneyTest(User user)
{
if (DeMoney != null)
{
DeMoney(user);
}
else
{
Console.WriteLine("还款日期未到");
}
}
}
class User
{
private int balance;
private int debt;
private string name;
private int day;
public User(int balance, int debt, string name, int day)
{
this.balance = balance;
this.debt = debt;
this.name = name;
this.day = day;
}
public int Balance
{
get => balance;
set => balance = value;
}
public string Name
{
get => name;
set => name = value;
}
public int Debt
{
get => debt;
set => debt = value;
}
public int Day
{
get => day;
set => day = value;
}
}
class Bank
{
public void DeMoneying(User user)
{
Console.WriteLine("{0},可用余额{1},本次还款{2},目前余额{3}",
user.Name, user.Balance, user.Debt, (user.Balance - user.Debt));
Console.WriteLine();
}
}
class program
{
static void Main(string[] agrs)
{
CreditCardDelegate d = new CreditCardDelegate();
User user = new User ( 401, 400, "张三", 1);
Bank bank = new Bank();
d.DeMoney += bank.DeMoneying;
for (int i=0;i<=10000;i++)
{
if (i == user.Day)
d.DeMoneyTest(user);
}
}
}
}
https://gitee.com/yu-mingjie-20333/