您的位置:首页 > 编程语言 > C#

复习:C#3.0面向对象测试开发包

2008-08-04 12:26 549 查看
复习:C#3.0面向对象测试开发包



----------------Form1.cs文件

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using jc;

using abstract_test;

using arraylist_test;

using hashtable_test;

using thief_Test;

namespace OO_Test

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{ /*

//MessageBox.Show("你好!");

TestAdd t = new TestAdd();

ATM a = new ATM();

a.Type = 110;

Girls g = new Girls();

g[0] = "mm";

g[2] = "mm2";

Girls.boys b = new Girls.boys();

b[0] = "gg";

String s = String.Format(" 调用TestAdd类中Add法后值为:{0} \n ATM机帐户类型:{1}\n ATM机在用帐号:{2}",t.add(2,3).ToString (),a.Type.ToString (),a.GetAccountNumber.ToString ());

string s2 = string.Format("\n 索引器1值为:{0} \n 索引器3值为:{1}\n 索引器3值的索引1值为{2}", g[0], g[2],b[0]);

MessageBox.Show(s+s2);

*/

boys b = new boys();

//b[0]="帅哥1";

//b[1]="帅哥2";

Girls g = new Girls();

g.set_name(0, "靓女1");

g[0] = b;

g[0][0] = "帅哥1";

g[0][1] = "帅哥2";

g[0][2] = "帅哥3";

g.set_name(1, "欲女1");

g[1] = b;

g[1][3] = "衰哥1";

g[1][4] = "衰哥2";

string s = string.Format("\n {3}的男朋友1:{0}\n {3}的男朋友2:{1}\n {3}的男朋友3:{2}", g[0][0], g[0][1], g[0][2],g.get_name(0));

string s2 = string.Format("\n {3}的男朋友1:{0}\n {3}的男朋友2:{1}\n {3}的男朋友3:{2}", g[1][3], g[1][4], "精尽人亡兄", g.get_name(1));

MessageBox.Show(s+s2 );

}

private void button2_Click(object sender, EventArgs e) //继承测试

{

jc.Person p0 = new jc.police();

jc.police p = (jc.police)p0;

p.name = "成龙";

p.NO = "pc110";

p.sex = true;

p.job = "警官";

string s = string.Format("名为{0},警号{1},职务{2},性别{3}的警察在{4},过后要{5},然后再{6}", p.name, p.NO, p.job, p.sex ? "男" : "女", p.claw_thief(),p.sleep (),p.study ());

MessageBox.Show(s);

}

private void button3_Click(object sender, EventArgs e) //抽象类测试

{

abstract_test.Person p0 = new abstract_test.police();

abstract_test.police p = (abstract_test.police)p0;

p.name = "成龙";

p[1] = "李连杰";

string s = string.Format("{0}与{1}在{2}中,{3},然后各自回去{4}", p.name,p[1],p.walk(),p.catch_thief (),p.sleep ());

MessageBox.Show(s);

p.show();

MessageBox.Show("超人"+p.fly ());

}

private void button4_Click(object sender, EventArgs e) //动态数组测试

{

arraylist_class a = new arraylist_class();

a.show();

arraylist_test.student[] stus = new arraylist_test.student[] { new arraylist_test.student("cyz", "s001", true, "一班", 85),

new arraylist_test.student("zym", "s002", true, "二班", 95),

new arraylist_test.student("ly", "s003", false , "三班", 75)};

dataGridView1.DataSource = stus;

//dataGridView1.DataBindingComplete();

}

private void button5_Click(object sender, EventArgs e) //哈希表调用

{

hashtable_class ht = new hashtable_class();

ht.show();

}

private void button6_Click(object sender, EventArgs e) //事件测试

{

bank y = new bank();

thief x = new thief();

thief_Test.police j1 = new thief_Test.police();

thief_Test.police j2 = new thief_Test.police();

y.bell_Event += x.run;

y.bell_Event += j1.wnn;

y.bell_Event += j2.wnn;

y.bell();

cat c = new cat();

mouse m1 = new mouse();

mouse m2 = new mouse();

master ms = new master();

c.cry_event += m1.run;

c.cry_event += m2.run;

c.cry_event += ms.walk_up;

c.cry();

}

private void button7_Click(object sender, EventArgs e) //泛型测试

{

List<string> oscar = new List<string>();

oscar.Add("《伤城》");

oscar.Add("《十面埋伏》");

oscar.Add("《***》");

oscar.Remove("《***》");

if (!oscar.Contains("《红楼梦》")) oscar.Add("《红楼梦》");

ltb.Items.Add(string.Format("容量:{0}",oscar.Capacity.ToString() ));

ltb.Items.Add(string.Format("元素个数:{0}", oscar.Count.ToString ()));

ltb.Items.Add("");

ltb.Items.Add(string.Format("最佳影片:"));

foreach (string t in oscar)

{

ltb.Items.Add(t);

}

ltb.Items.Add(string.Format("我最喜欢的电影:{0}", oscar[2].ToString ()));

oscar.Clear();

Dictionary<string, string> stu = new Dictionary<string, string>();

stu.Add("s001", "cyz");

stu.Add("s002", "lm");

stu.Add("s003", "lgt");

stu.Add("s004", "ly");

stu.Remove("s002");

if (!stu.ContainsKey("s005")) stu.Add("s005", "lxj");

stu["s001"] = "yzc";

foreach (string t in stu.Keys )

{

ltb.Items.Add(t+" "+stu[t].ToString ());

}

}

}

class TestAdd //类的测试

{

public int add(int a, int b)

{

return a + b;

}

}

class ATM //属性的测试

{

private int type; //帐户类型

private double balance; //帐户余额

private int accountNumber;//帐户号码

public ATM()

{

accountNumber = 101101;

}

public int Type

{

get { return type; }

set { type = value; }

}

public double Balance

{

get { return balance; }

set { balance = value; }

}

public int GetAccountNumber

{

get { return accountNumber; }

}

}

class Girls //测试索引器,类中类,一个女孩多个男朋友

{

private string[] names = new string[5];

boys[] b=new boys [5];

public boys this[int n]

{

get { return b
; }

set { b
= value; }

}

public void set_name(int n,string name)

{

names
= name;

}

public string get_name(int n)

{

return names
;

}

}

class boys

{

private string[] names = new string[5];

public string this[int n]

{

get { return names
; }

set { names
= value; }

}

}

}

--------------------------------------------Event.cs文件

using System;

using System.Collections.Generic;

using System.Text;

using System.Windows.Forms;

namespace thief_Test

{

class thief

{

public void run() { MessageBox.Show ( "我跑!"); }

}

class police

{

public void wnn() { MessageBox.Show ( "给我追"); }

}

class bank

{

public delegate void ActDelegate();//定义了一个委托,相当于定义了一个类

public event ActDelegate bell_Event;//定义了一个事件,相当于实例化了一个多播委托

public void bell()

{

MessageBox.Show("警铃响!");

bell_Event();

}

}

//----以下部份为测试"猫叫,老鼠跑,主人醒"功能

class mouse

{

public void run() { MessageBox.Show("老鼠跑!"); }

}

class master

{

public void walk_up()

{

MessageBox.Show("主人醒!");

}

}

class cat

{

public delegate void dele_cry();

public event dele_cry cry_event;

public void cry()

{

MessageBox.Show("猫叫!");

cry_event();

}

}

}

------------------------------抽象类测试.cs

using System;

using System.Collections.Generic;

using System.Text;

using System.Windows.Forms;

namespace abstract_test

{

interface Ipreterhuman

{

string fly();

}

abstract class Person

{

public abstract void show();

public abstract string name { get;set;}

public abstract string this[int n] { get;set;}

public string walk()

{

return "走路!";

}

public string sleep()

{

return "睡觉!";

}

}

class preterhuman

{

public string fly()

{

return "飞行!";

}

}

class police : Person, Ipreterhuman

{

private string [] _name=new string [5];

public string fly()

{

return "飞行中!";

}

public override void show()

{

MessageBox.Show("抽象类显示方方法测试");

}

public override string name

{

get

{

return _name[0];

}

set

{

_name[0] = value;

}

}

public override string this[int n]

{

get

{

return _name
;

}

set

{

_name
= value;

}

}

public string walk()

{

return "警察巡逻!";

}

public string catch_thief()

{

return "抓小偷!";

}

}

}

------------------------------------------动态数组测试.cs

using System;

using System.Collections.Generic;

using System.Text;

using System.Collections;

using System.Windows.Forms;

namespace arraylist_test

{

class arraylist_class

{

ArrayList ls = new ArrayList();

student s1 = new student("cyz", "s001", true, "一班", 85);

student s2 = new student("zym", "s002", true, "二班", 95);

student s3 = new student("ly", "s003", false , "三班", 75);

public arraylist_class()

{

ls.Add(s1);

ls.Add(s2);

ls.Add(s3);

}

public void show()

{

student s = (student )ls[2];

s.name = "李艳";

s.show();

}

}

class student

{

private string _name;

private string _out;//学号

private string _class;//班级

private bool _sex;

private float _grade;//成绩

public string name

{

get { return _name; }

set { _name = value; }

}

public string Out

{

get { return _out; }

set { _out = value; }

}

public string Class

{

get { return _class; }

set { _class = value; }

}

public bool Sex

{

get { return _sex; }

set { _sex = value; }

}

public float grade

{

get { return _grade; }

set { _grade = value; }

}

public student(string __name,string __out,bool __sex,string __class,float __grade)

{

_name = __name;

_out = __out;

_sex = __sex;

_class = __class;

_grade = __grade;

}

public void show()

{

string s = string.Format("学号为{0}性别为{1}在{2}的学生{3}的成绩为{4}",_out,_sex?"男":"女",_class ,_name ,_grade );

MessageBox.Show(s);

}

}

}

---------------------------------------------------哈希表测试.cs

using System;

using System.Collections.Generic;

using System.Text;

using System.Windows.Forms;

using System.Collections;

namespace hashtable_test

{

class Class

{

private string _class_name; //班级名称

private string _class_director;//班主任

private int _number;//班上人数

public string class_name

{

get { return _class_name; }

set { _class_name = value; }

}

public string class_director

{

get { return _class_director; }

set { _class_director = value; }

}

public int number

{

get { return _number; }

set { _number = value; }

}

public Class(string class_name, string class_director, int number)

{

_class_name = class_name;

_class_director = class_director;

_number = number;

}

public void show()

{

string s = string.Format("{0}所管的{1}班级的人数为{2}", _class_director, _class_name, _number);

MessageBox.Show(s);

}

}

class student

{

private string _name;

private string _out;//学号

private string _class;//班级

private bool _sex;

private float _grade;//成绩

public string name

{

get { return _name; }

set { _name = value; }

}

public string Out

{

get { return _out; }

set { _out = value; }

}

public string Class

{

get { return _class; }

set { _class = value; }

}

public bool Sex

{

get { return _sex; }

set { _sex = value; }

}

public float grade

{

get { return _grade; }

set { _grade = value; }

}

public student(string __name, string __out, bool __sex, string __class, float __grade)

{

_name = __name;

_out = __out;

_sex = __sex;

_class = __class;

_grade = __grade;

}

public void show()

{

string s = string.Format("学号为{0}性别为{1}在{2}的学生{3}的成绩为{4}", _out, _sex ? "男" : "女", _class, _name, _grade);

MessageBox.Show(s);

}

}

class hashtable_class

{

Hashtable ht = new Hashtable(); //学生哈希表

Class c1 = new Class("立志", "陈老师", 12);

Class c2 = new Class("成才", "杨老师", 8);

student s1 = new student("cyz", "s001", true, "立志2", 85);

student s2 = new student("zym", "s002", true, "立志2", 95);

student s3 = new student("ly", "s003", false, "成才2", 73);

student s4 = new student("wl", "s004", false, "成才2", 77);

student s5 = new student("qh", "s005", true , "立志2", 93);

public hashtable_class()

{

ht.Add(s1, c1);

ht.Add(s2, c1);

ht.Add(s5, c1);

ht.Add(s3, c2);

ht.Add(s4, c2);

}

public void show()

{

string ss;

ss = "";

foreach (DictionaryEntry dc in ht)

{

student s = (student)dc.Key;

Class c = (Class)dc.Value;

ss = ss + "\n 学生:" + s.name + ",所在的班级为:" + c.class_name + "班";

}

MessageBox.Show(ss);

}

}

}

-----------------------------------------------------------继承测试.cs

using System;

using System.Collections.Generic;

using System.Text;

namespace jc //继承测试

{

public interface Iperson

{

string sleep();

string study();

}

class Person //:Iperson //建立人的基类

{

private string _name;

private bool _sex;

private int _age;

private float _height;

private float _weight;

public string name

{

get { return _name; }

set { _name = value; }

}

public bool sex

{

get { return _sex; }

set { _sex = value; }

}

public int age

{

get { return _age; }

set { _age = value; }

}

public float height

{

get { return _height; }

set { _height = value; }

}

public float weight

{

get { return _weight; }

set { _weight = value; }

}

public string walk()

{

return "走路!";

}

public string eat()

{

return "吃饭!";

}

/*

public string sleep()

{

return "睡觉!";

}

public string study()

{

return "学习!";

} */

}

class police : Person,Iperson

{

private string _NO;//警号

private string _job;//职务

public string NO

{

get { return _NO; }

set { _NO = value; }

}

public string job

{

get { return _job; }

set { _job = value; }

}

public string claw_thief()

{

return "抓小偷!";

}

public string sleep()

{

return "睡觉!";

}

public string study()

{

return "警务理论学习!";

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: