zoukankan
html css js c++ java
委托事件之买烟
由于家住在五楼,晚上我的精品白沙烟抽完了.然而自己不想下楼去商店买,于是
委托
我的朋友去买.打算买10包.
using
System;
using
System.Collections.Generic;
using
System.Text;
namespace
BuyCigarette
{
class
Program
{
static
void
Main(
string
[] args)
{
Console.Write(
"
请输入买烟的数量:
"
);
//
声明BuyCigarettesEvent类的一个实例
BuyCigarettesEvent m
=
new
BuyCigarettesEvent();
m.BuyCigarette
+=
new
BuyCigarettesEvent.EventDelegate(Result);
//
BuyCigarette事件交给EventDelegate委托去做
//
转换用户输入,并调用Buy方法
m.Buy(
int
.Parse(Console.ReadLine()));
//
输出总价格
Console.WriteLine(
"
总价格为:
"
+
m.Price
+
"
元人民币
"
);
Console.ReadKey(
false
);
}
//
事件处理函数。
static
void
Result(
object
sender, EventArgs e)
{
Console.WriteLine(
"
朋友去买烟了
"
);
}
}
public
class
BuyCigarettesEvent
{
//
首先声明一个委托
public
delegate
void
EventDelegate(
object
sender, EventArgs e);
//
声明一个事件
public
event
EventDelegate BuyCigarette;
//
价格
public
int
Price;
public
void
Buy(
int
number)
{
Price
=
number
*
10
;
//
假如是买的是精品白沙烟10元一包
if
(BuyCigarette
!=
null
)
{
BuyCigarette(
this
,
new
EventArgs());
//
响应事件
}
}
}
}
结果显示:
查看全文
相关阅读:
Simple DirectMedia Layer常用API总结
[游戏复刻] Super Mario Brothers(1985. Famicom)
[游戏复刻] 2048(2014. Android)
图的结构以及寻路算法的c实现
散列查找的C实现
【游戏编程从0开始】一、基本结构
C++中const关键字用法总结
C标准库常用函数概要
字符串表达式计算器的设计
初探数据结构
原文地址:https://www.cnblogs.com/xiaobaigang/p/1031554.html
最新文章
风格指南--C++
对象开发原则
无法安装或者卸载网卡驱动
如何激活win10
SAP OLE中常用的一些方法和属性
一千行ABAP代码实现Windows传统游戏扫雷
自开发理财思想
ABAP 程序/接口调用其他程序的数据
ALV 动态行列
java 生成随机校验码
热门文章
【ElementUI】日期选择器时间选择范围限制
四元表达式
for循环动态改变样式状态
vue + element table数据过多实现懒加载
获取当前日期,格式如:2021-07-02
element table表格中的数据修改弹出框,点击确定更新后table修改的数据高度位置不变
在vue中使用tinymce富文本编辑器,解决tinymce在dialog对话框中层级太低的问题
如何修改Element-UI的input样式
webpack之proxyTable设置跨域
Vue router报错:NavigationDuplicated {_name: "NavigationDuplicated", name: "NavigationDuplicated"}的解决方法
Copyright © 2011-2022 走看看