C#其他
来源:互联网 发布:邮币卡交易平台软件 编辑:程序博客网 时间:2024/06/05 09:36
摘自:C#入门经典第10章
C#属性块
//---------Student.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace First{ public class Student { public readonly string name; private double score; public Student(string name) { this.name = name; score = 0; } public override string ToString() { return "[名字:" + this.name + "--分数:" + this.score + "]"; } public double stuScore { get { return score; } set { if (value >= 0 && value <= 100) score = value; else throw new ArgumentOutOfRangeException("stuScore",value,"stuScore must be assigned a value between 0 and 100!"); } } }}
//----------Program.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace First{ class Program { static void Main(string[] args) { Console.WriteLine("新建学生..."); /* //ERROR String[] nameList = new String[20]; nameList={"a","b","c"}; */ string[] nameList={"张三","李四","王五"}; Student[] stu=new Student [3]; stu[0] = new Student(nameList[0]); stu[1] = new Student(nameList[1]); stu[2] = new Student(nameList[2]); Console.WriteLine("stu 新建完成..."); foreach(Student s in stu ) try { Console.WriteLine("--------------------------"); Console.WriteLine("请给下面学生输入分数:"+s.name); double inputScore = Convert.ToDouble(Console.ReadLine()); s.stuScore = inputScore; Console.WriteLine("设置分数成功!"); } catch (Exception e) { //Console.WriteLine("输入分数不符合要求!"); } Console.WriteLine("输出学生信息:"); foreach (Student s in stu) Console.WriteLine(s); Console.ReadKey(); } }}
摘自:C#入门经典第13章
C#委托和事件
//----------Connection.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Timers;namespace Second{ //在名字空间声明一个委托. public delegate void MessageHandler(string message); public class Connection { /* * 在类中声明一个事件,且用关键字event修饰 * public event MessageHandler MessageArrived; * 关键字 委托(事件的类型) 事件 */ public event MessageHandler MessageArrived; private Timer pollTimer; public Connection() { /* * 1.建立对象(这个对象可以引发事件Elapsed) * 2.订阅事件(采用显/隐式委托+=。这里的委托指ElapsedEventHandler,匹配方法是CheckMessage) * 3.开启线程(当引发事件后,就通知订阅器,转到相应的处理方法中去) * 注:在下面主线程中可以看到事件的嵌套 */ pollTimer = new Timer(100); pollTimer.Elapsed += new ElapsedEventHandler(CheckMessage); } public void Connect() { pollTimer.Start(); } public void Disconnect() { pollTimer.Stop(); } /* * 定义一个Random实例,生成0-9之间的随机数 * 定义一个匹配方法CheckMessage,该方法与委托名具有相同的返回类型和形参 * MessageArrived != null含义是:事件是否有订阅者?如果为null表示没有订阅,从而也就不会被引发 * MessageArrived("Hello C sharp!");表示委托,由相应的匹配事件处理 * 解释:见主函数 */ private static Random random = new Random(); private void CheckMessage(object source,ElapsedEventArgs e) { Console.WriteLine("Checking for new messages:"); if ((random.Next(9) == 0) && (MessageArrived != null)) MessageArrived("Hello C sharp!"); } }}
//----------Display.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Second{ public class Display { public void DisplayMessage(string message) { Console.WriteLine("Message arrived:"+message ); } }}
//-------------Program.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Second{ class Program { static void Main(string[] args) { Connection myConnection = new Connection(); Display myDisplay = new Display(); /* * 事件触发者:Connection,事件触发对象:myConnection * 事件:MessageArrived,事件委托:MessageHandler * * 事件订阅者:Display,事件订阅对象:myDisplay * 匹配方法:DisplayMessage,用来响应MessageArrived事件 */ myConnection.MessageArrived += new MessageHandler(myDisplay.DisplayMessage); myConnection.Connect(); Console.ReadKey(); Console.ReadKey(); } }}
阅读全文
0 0
- C# 其他
- C#其他
- c# 打开其他EXE
- C# 最小化其他应用程序
- c# 其他技术学习
- C#再学习以及其他
- c# 运行其他外部文件
- C# 3.0 其他XML技术
- 【其他】c# 正则匹配程序
- C#的其他运算符
- c# 打开其他应用程序方法
- C#一些其他的流
- C#字符串截取 和其他函数
- C#中使用Win32和其他库
- C#中使用Win32和其他库
- C#中使用Win32和其他库
- C# 网页代码转换及 其他
- C#中使用Win32和其他库
- 11.3dairy
- 4、进程同步:进程同步的概念和同步原则,临界资源和临界区的概念,信号量及其应用,经典进程同步问题
- 欢迎使用CSDN-markdown编辑器
- 计算两个矩阵的乘积
- 无限极分类mysql存储过程编写实现调用时给定一个id,返回其所有父级与子级并排序
- C#其他
- [JZOJ5446]【NOIP2017提高A组冲刺11.3】高考是不可能高考的(2-SAT问题)【Atcoder regular context 069F】
- CTF中常见的php函数绕过(保持更新)
- 图灵奖
- 编写一个tf listener
- C++之"endl"与"\n"的区别
- 2017.11.3
- Python中os模块
- 移植u-boot-2012.04----支持Nandflash启动