复习: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[n]; }
set { b[n] = value; }
}
public void set_name(int n,string name)
{
names[n] = name;
}
public string get_name(int n)
{
return names[n];
}
}
class boys
{
private string[] names = new string[5];
public string this[int n]
{
get { return names[n]; }
set { names[n] = 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[n];
}
set
{
_name[n] = 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 "警务理论学习!";
}
}
}
分享到:
相关推荐
C#是一种现代化的、面向对象的编程语言,应用广泛于Windows和Web应用程序开发。以下是对C#复习题的详细解释: 1. 简单值数据类型:C#中的简单值数据类型包括int、char、bool、float、double、decimal等。因此,选项...
C#是一种由微软公司开发的面向对象的编程语言,它被广泛用于构建Windows桌面应用程序、Web应用程序以及游戏开发。在"C# Windows程序设计 课件+代码"的学习资源中,你可以深入理解C#语言的基础与高级特性,并通过实际...
C#的核心特性包括面向对象编程、类型安全、垃圾回收机制以及丰富的类库支持。它提供了多种编程范式,如面向过程、面向对象以及最近引入的面向切面编程(AOP)。C#的关键概念包括: 1. **变量与数据类型**:C#支持...
1. **C#基础语法**:C#是一种面向对象的编程语言,其语法结构清晰,易于理解。包括变量声明、数据类型(如整型、浮点型、字符串、布尔型等)、运算符(算术、比较、逻辑、位操作等)、控制流(条件语句、循环语句、...
C#是一种广泛应用于软件开发的面向对象的编程语言,由微软公司于2000年推出,主要用于构建Windows操作系统上的应用程序。"20090526pptC#"这个标题可能指的是一个2009年5月26日创建或更新的C#学习资源,形式为PPT演示...
它的设计目标是提供一种类型安全、面向对象、可移植性强的语言,使得开发者能够高效地构建各种应用程序,包括桌面应用、Web应用以及游戏开发等。C#的语法结构与C++和Java相似,但又在其基础上引入了许多现代化的特性...
C#是微软开发的一种面向对象的编程语言,它在.NET框架下运行,广泛应用于Windows桌面应用、Web应用、游戏开发以及移动应用等领域。 C# 2008是C#语言的一个版本,发布于2008年,主要特性包括: 1. **Linq(Language...
C#(发音为"C Sharp")是微软公司推出的一种面向对象的、现代的编程语言,主要用于开发Windows平台的应用程序,尤其是.NET框架下的应用。C#语法简洁且强类型,支持多种编程范式,包括面向对象、面向组件和函数式编程...
C#(读作"C Sharp")是微软公司开发的一种面向对象的、现代化的编程语言,广泛应用于Windows桌面应用、Web应用以及游戏开发等领域。本考试旨在检验学习者对C#语法、类与对象、控制结构、异常处理、数据类型等基础...
1. C#是一种面向对象的编程语言,由微软公司开发,用于.NET框架。在C#中,简单值数据类型包括整型(如int)、字符型(char)以及枚举类型等。例如,题目中的选择题1指出,`int`和`char`类型属于C#的简单值数据类型,...
《Microsoft.Press....《Microsoft.Press.Programming.Microsoft.Visual.C.Sharp.2005.The.Language.Jan.2006.INTERNAL.chm》这个文件很可能是该书的电子版,包含了全部章节和实例,是学习和复习C# 2005的宝贵资源。
C#是一种面向对象的编程语言,由微软公司推出,特别适合开发Windows应用程序和服务。在.NET框架的支持下,C#具有优秀的性能、类型安全性和跨平台能力,使得在线考试系统的开发更加高效和便捷。 在线练习是系统的...
C#作为一种功能强大且面向对象的编程语言,自微软在.NET平台上推出以来,因其简洁高效、支持模板和标准化的特性,迅速赢得了众多开发者的青睐。《C#的数据结构,需要的来拿》这一著作不仅填补了国内以C#语言讲述数据...
C#是一种现代、面向对象的编程语言,由微软在.NET平台上推出。它以其简洁、高效、模板化和标准化的特性而著称,被视为程序设计领域的一件艺术品。随着C#和.NET框架的不断更新,如从2.0版本发展到3.0版本,C#成为了...
C#是Microsoft开发的一种面向对象的编程语言,广泛应用于Windows桌面应用、游戏开发以及.NET框架的软件开发。项目中可能包含了C#3.0的特性,如Lambda表达式、匿名类型和自动属性等,这些都是C#从2.0版本升级至3.0时...
- C#(C Sharp):由微软开发的一种面向对象的编程语言。 8. 计算机内部资源: - CPU(中央处理器):计算机的控制中心,负责解释和执行程序指令。 - ROM(只读存储器):通常用于存储固件,它是一种非易失性...