摘自:C#入门经典第10章
C#属性块
//---------Student.cs
using 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.cs
using 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.cs
using 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.cs
using 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.cs
using 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();
}
}
}