using System; using System.Collections; public struct book { public string name; public string title; public book(string name1,string title1) { name=name1; title=title1; } } //定义一个委托: public delegate void RunProc(book Book); //定义一个类: public class Bookdb { ArrayList T_book=new ArrayList(); //数据加入; private void addbook(string name1,string title1) { T_book.Add(new book(name1,title1)); } //调用委托 public void rundeletgate(RunProc Probook) { foreach(book b in T_book) { Probook(b); } }
using System; using System.Collections;public struct book { public string name; public string title; public book(string name1,string title1) { name=name1; title=title1; } } //¶¨ÒåÒ»¸öίÍУº public delegate void RunProc(book Book); //¶¨ÒåÒ»¸öÀࣺ public class Bookdb { ArrayList T_book=new ArrayList(); //Êý¾Ý¼ÓÈ룻 public void addbook(string name1,string title1) { T_book.Add(new book(name1,title1)); } //µ÷ÓÃίÍÐ public void rundeletgate(RunProc Probook) { foreach(book b in T_book) { Probook(b); } }
{
addbook("test1","test2");
addbook("test3","test4");
addbook("test5","test6");
}
调用的时候
p_test1.addbooks();
addboxs();//
这里能够编译通过吗?不行吧,应该用
p_test1.addboxs();注意addBoxs不能使static的。
using System.Collections;
public struct book
{
public string name;
public string title;
public book(string name1,string title1)
{
name=name1;
title=title1;
}
}
//定义一个委托:
public delegate void RunProc(book Book);
//定义一个类:
public class Bookdb
{
ArrayList T_book=new ArrayList();
//数据加入;
private void addbook(string name1,string title1)
{
T_book.Add(new book(name1,title1));
}
//调用委托
public void rundeletgate(RunProc Probook)
{
foreach(book b in T_book)
{
Probook(b);
}
}
//定义输出:
static void Proc(book b)
{
Console.WriteLine("qeqweqweqweqw"+b.name.ToString());
}
//定义数据内容
private void addboxs()
{
addbook("test1","test2");
addbook("test3","test4");
addbook("test5","test6");
}
//主输出:
static void Main()
{
//Application.Run(new Form1());
Bookdb p_test1=new Bookdb();
p_test1.addboxs();
Console.WriteLine("It is :");
p_test1.rundeletgate(new RunProc(Proc));
}
}
修改后完整的源程序
using System.Collections;public struct book
{
public string name;
public string title;
public book(string name1,string title1)
{
name=name1;
title=title1;
}
}
//¶¨ÒåÒ»¸öίÍУº
public delegate void RunProc(book Book);
//¶¨ÒåÒ»¸öÀࣺ
public class Bookdb
{
ArrayList T_book=new ArrayList();
//Êý¾Ý¼ÓÈ룻
public void addbook(string name1,string title1)
{
T_book.Add(new book(name1,title1));
}
//µ÷ÓÃίÍÐ
public void rundeletgate(RunProc Probook)
{
foreach(book b in T_book)
{
Probook(b);
}
}
//¶¨ÒåÊä³ö£º
static void Proc(book b)
{
Console.WriteLine("qeqweqweqweqw=="+b.name.ToString());
}
//¶¨ÒåÊý¾ÝÄÚÈÝ
private void addboxs()
{
// Bookdb p_test=new Bookdb();
addbook("test1","test2");
addbook("test3","test4");
addbook("test5","test6");
}
//Ö÷Êä³ö£º
static void Main()
{
//Application.Run(new Form1());
Bookdb p_test1=new Bookdb();
p_test1.addboxs();
Console.WriteLine("It is :");
p_test1.rundeletgate(new RunProc(Proc));
}
}