.NET学习(4) C#中类的设计
来源:互联网 发布:mysql该不该用join 编辑:程序博客网 时间:2024/06/06 01:30
看了Modern C#系列课程4--C#中类的设计的笔记,主讲 俞晖
*********************************************************
Property(属性):看着像方法,但是可以像字段一样直接使用。
Public class BankCustomer
{
Private decimal m_Balance; //私有field
Public decimal Balance //property
{
Get{ return this.m_Balance; }
Set{
If(value < 0.0m) //value是系统自定义的输入参数
;
Else
This.m_Balance = value;
}
}
调用:bc = new BankCustomer();
bc.Balance = bc.Balance – 10.0m;//其实是对m_Balance操作
****************************************************************
索引器 :其实也是属性的一种,是对类似于数组的操作。
Public class BankCustomer{
Private Person[] relatives;//定义一个索引器
Public BankCustomer(){
Relatives = new Person[100];//对100个Person对象索引
}
Public Person this[int index]{//这个方法是主要的,建立索引
Get{
Return relatives[index];
}
Set{
If(value != null)
Relatives[index] = value;
}
}
调用:bc = new BankCustomer();
Bc[10] = new Person(“hello”);
TextBox1.Text = bc[10].name;
//person是一个类
public class Person
{
private string name;
public Person()
{
}
public Person(string name)
{
this.name = name;
}
public string Name
{
get
{
return this.name;
}
}
}
***********************************************************
委托代理delegate相当于函数指针。指向参数表和返回类型一致的所有函数。这是一个类
MoneyCompute.cs
using System;
public delegate double DelegateCompute(double x);//定义代理,匹配参数和返回值都是double的方法
namespace WindowsApplication3
{
public class MoneyCompute
{
public static double Compute(double t, DelegateCompute dc)//代理的方法在这里被调用
{
return dc(t);
}
public MoneyCompute()//构造函数
{ }
}
}
Form1.cs
public static double Low(double T) //被代理的方法
{
return 0.02*T;
}
public static double High(double T) //被代理的方法
{
return 0.1*T;
}
private void button1_Click(object sender, System.EventArgs e)
{
double result;
DelegateCompute dc; //因为代理是一个类,所以要生成类的实例
dc= new DelegateCompute(Form1.Low);//代理Form1.Low方法。参数和返回值和代理函数一致
result = MoneyCompute.Compute(1000.0,dc);//调用代理函数
this.low.Text = result.ToString();
dc = new DelegateCompute(Form1.High);
result = MoneyCompute.Compute(2000.0,dc);
this.high.Text = result.ToString();
}
代理还可以同时顺序调用多个被代理方法,但是这些被代理方法的返回值必须为空。
Messages.cs
using System;
public delegate void Message();//定义代理,返回值为void
namespace WindowsApplication4
{
public class Messages
{
public Messages()
{ }
public static void Greeting() //被代理方法
{
Console.WriteLine("Welcome to Webcast and MSDN China");
}
public static void DateAndTime()//被代理方法
{
Console.WriteLine(DateTime.Now.ToLongDateString());
}
public static void Feedback()//被代理方法
{
Console.WriteLine("Your feedback are very important for us");
}
}
}
Form1.cs
Message msg;
private void View_Click(object sender, System.EventArgs e)
{
msg = new Message(Messages.Greeting); //因为是类,所以要生成实例
msg += new Message(Messages.DateAndTime); //增加一个事件
Message msg2 = new Message(Messages.Feedback);
msg += msg2;
msg();//运行代理方法
}
*********************************************************************
事件处理event,其实也用到了代理的知识
NameList.cs
using System;
using System.Collections;
namespace WindowsApplication5
{
public class NameList
{
ArrayList list;
public event NameListEventHandler nameListEvent;//生成事件实例,注意用的event关键字
public NameList()
{
list = new ArrayList();
}
public void Add(string name)
{
list.Add(name);
if(nameListEvent != null)//是否有事件处理
{
nameListEvent(this, new NameListEventArgs(name, list.Count));//调用事件处理
}
}
}
}
NameListEventArgs.cs
using System;
public delegate void NameListEventHandler(object source, //事件处理代理
WindowsApplication5.NameListEventArgs args);
namespace WindowsApplication5
{
public class NameListEventArgs : EventArgs
{
string name;
int count;
public NameListEventArgs()
{ }
public NameListEventArgs(string name, int count)
{
this.name = name;
this.count = count;
}
public string Name
{
get{ return name; }
}
public int Count
{
get{ return count;}
}
}
}
Form1.cs
private void button1_Click(object sender, System.EventArgs e)
{
NameList names =new NameList();
names.nameListEvent += new NameListEventHandler(NewName);//代理NewName方法为事件处理方法
names.nameListEvent += new NameListEventHandler(CurrentCount);//代理CurrentCount方法为事件处理方法
names.Add("MSDN");//每add一次,都要调用NewName,CurrentCount方法
names.Add("Webcast");
}
public static void NewName(object source, NameListEventArgs args)//被代理的事件方法
{
Console.WriteLine(args.Name + "was added to the list");
}
public static void CurrentCount(object source, NameListEventArgs args)//被代理的事件方法
{
Console.WriteLine("list currently has " + args.Count + " items.");
}
- .NET学习(4) C#中类的设计
- C#中设计模式的学习
- C#学习笔记-C#中Json.Net(newtonjs)的使用
- .NET学习(10)C# 中的一些设计技巧
- C#中类的学习
- .NET学习(5) C#中异常处理
- C#温故而知新学习系列之.NET框架高级特性—.NET框架中自身提供的属性(二)
- C#温故而知新学习系列之.NET框架高级特性—如何在.NET框架中自定义属性类?(三)
- c#中类的学习以及使用方法
- .NET深入学习笔记(2):C#中判断空字符串的4种方法性能比较与分析
- 《C#和NET学习的总结(一)--数据类型》
- 学习笔记(三)-----C#的设计模式
- ASP.NET中Excel操作类C#版的
- 【C#.NET】C#中抽象类和接口的区别
- C#或者说.NET的自动内存管理 学习中碰到的问题。
- C# winfrom 中自定义的翻页控件(自己设计)
- NET/C# 中 Main() 方法与它所在的类,如 (Program class) 的关系
- .Net中C#的DllImport的用法
- JavaScript页面刷新与弹出窗口问题解决方法
- 新起点
- DataSet导出到Excel比较完整的解决方案(二)--服务器端生成文件(downmoon)
- 用JavaScript刷新框架子页面的方法
- 开发板买来后如何练习写驱动、开发BSP?(作者:wogoyixikexie@gliet)
- .NET学习(4) C#中类的设计
- 英特尔主流移动CPU参数知多少?
- Js字符串操作函数大全
- Silverlight Map Viewer for ArcGIS Server
- 超级实用:让你的摄像头变成文字扫描器
- 在Windows Vista系统下安装Windows Xp并设置双重启动
- 多线程中使用stl容器
- 放假
- 页面间传递变量的方法及使用范围的讨论