1、指标等级 A B C D 功能完整 程序质量 按时检查 提问回答 检查时间 总评成绩 实 验 报 告 序 号: 实验老师: 课程名称: C#与.NET实验 实验名称: 实验6:继承和多态 学 号: 姓 名: 课程名称 C#与.NET实验 实验项目 实验6:继承和多态 实验目
2、的 熟悉并掌握,C#的类继承与多态的特性。 实验内容(算法、程序、步骤和方法) 实验内容(算法、程序、步骤和方法) 实验内容(算法、程序、步骤和方法)
3、 实验内容(算法、程序、步骤和方法) 实验内容(算法、程序、步骤和方法) 实验内容(算法、程序、步骤和方法)
4、 实验内容(算法、程序、步骤和方法) (1) 类型QueryTest的程序代码如下: using System; using System.IO; using System.Collections; namespace QueryTextLanguageTest { public class QueryTest { static void Main(string[] args) { Query query = null; Console.Wr
5、iteLine("TextQuery: begin()"); Boolean sign = true; //退出标记 while (sign) { Console.Write("Please enter a query or 'q' to Quit: "); string statement = Console.ReadLine(); //读取查询语句 if (statement.Equals("q")) /
6、/退出查询 { Console.WriteLine("OK,bye!\nTextQuery: end()\n"); sign = false; } else { if (query != null) { query = QueryTest.par
7、seQuery(statement); query.eval(); query.eval_print(); } else { Console.WriteLine("你的输入有误,请重新输入!\n"); } } } } //对输入的语句进行处理,返回查询类型 public static Query parseQuery(strin
8、g statement) { Query query = null; Query q1 = null; Query q2 = null; Query q3 = null; Query q4 = null; ArrayList word1 = new ArrayList(); string[] word = new string[5]; statement = statement.ToLower(); word = statement.Spl
9、it(' '); if (word.Length == 1) //匹配各种查询语句 { query = new NameQuery(statement); } else if (word.Length == 2 && word[0].Equals("!")) { query = new NotQuery(word[1]); } else i
10、f (word.Length == 3 && word[1].Equals("||")) { q1 = new NameQuery(word[0]); q2 = new NameQuery(word[2]); query = new OrQuery(q1,q2); } else if (word.Length == 3 && word[1].Equals("&&")) {
11、 q1 = new NameQuery(word[0]); q2 = new NameQuery(word[2]); query = new AndQuery(q1, q2); } else if (word[1].Equals("&&") && word[3].Equals("||")) { q1 = new NameQuery(word[0]); q2 = new
12、NameQuery(word[2]); q3 = new NameQuery(word[4]); q4 = new AndQuery(q1,q2); q4.eval(); q4.eval_print(); query = new OrQuery(q4, q3); } else if (word[1].Equals("||") && word[3].Equals("&&"))
13、 { q1 = new NameQuery(word[0]); q2 = new NameQuery(word[2]); q3 = new NameQuery(word[4]); q4 = new OrQuery(q1, q2); q4.eval(); q4.eval_print(); query = new AndQuery(q4,
14、q3); } else { query = null; } return query; } } } (2) 类型Query的程序代码如下: abstract public class Query { protected int textLines; protected StreamReader sr; protected string text_line = null;
15、 protected ArrayList numList = null; protected ArrayList lineList = null; public ArrayList NumList //文本行号链表 { get { return numList; } set { numList = value; } } public ArrayList LineList //文本行内容链表 { get
16、{ return lineList; } set { lineList = value; } } public int TextLines { get { return textLines; } } public Query() //构造函数 { sr = new StreamReader(@"F:\query.txt"); } public virtual void eval() { while (
17、text_line = sr.ReadLine())!= null) { textLines++; } } public virtual void eval_print() { Console.WriteLine(); } } (3) 类型NameQuery的程序代码如下: public class NameQuery : Query { private string name; //声明两个变量
18、private string line; public NameQuery(String name) //构造函数 : base() { this.name = name; //初始化各变量 this.numList = new ArrayList(); this.lineList = new ArrayList(); } //寻找符合条件的文本行 public override void
19、eval() { while ((this.text_line = this.sr.ReadLine()) != null) { this.textLines++; line = text_line.ToLower(); if (this.line.Contains(name)) { this.NumList.Add(this.TextLines);
20、 this.LineList.Add(this.text_line); } } } public override void eval_print()//输出查询结果 { Console.Write("{0}\n \t{1} line(s) match: [", this.name, this.NumList.Count); for (int i = 0; i < t
21、his.NumList.Count; i++) { Console.Write(" {0}", this.NumList[i]); } Console.WriteLine(" ]\n"); for (int i = 0; i < this.NumList.Count; i++) { Console.WriteLine("[{0}]: {1}",this.NumList[i],this.LineList[i]);} Console.WriteLin
22、e(); } public override string ToString() { return this.name; } } (4) 类型NotQuery的程序代码如下: public class NotQuery : Query { private string name; //声明各个变量 private string line; private Query query; public NotQuery(S
23、tring name) //构造函数 : base() { //初始化各变量 this.query = new NameQuery(name); this.name = name; this.numList = new ArrayList(); this.lineList = new ArrayList(); } //寻找符合条件的文本行 public overr
24、ide void eval() { this.query.eval(); while ((this.text_line = this.sr.ReadLine()) != null) { this.textLines++; line = text_line.ToLower(); if (!this.line.Contains(name)) {
25、 this.NumList.Add(this.TextLines); this.LineList.Add(this.text_line); } } } public override void eval_print()//输出查询结果 { Console.Write("{0}\n \t{1} line(s) match: [", this.query.ToString(),
26、 this.query.NumList.Count); for (int i = 0; i < this.query.NumList.Count; i++) { Console.Write(" {0}", this.query.NumList[i]); } Console.WriteLine(" ]\n"); Console.Write("! {0}\n \t{1} line(s) match: [",this.name,this.NumList.Count);
27、 for (int i = 0; i < this.NumList.Count; i++) { Console.Write(" {0}",this.NumList[i]); } Console.WriteLine(" ]\n"); for (int i = 0; i < this.NumList.Count; i++) { Console.WriteLine("[{0}]: {1}",this.NumList[i],this.LineList[i]);
28、} Console.WriteLine(); } public override string ToString() { return this.query.ToString(); } } (5) 类型OrQuery的程序代码如下: public class OrQuery : Query { private Query q1; //声明两个Query变量 private Query q2; publ
29、ic OrQuery(Query q1,Query q2) //构造函数 :base() { this.q1 = q1; //初始化各变量 this.q2 = q2; this.numList = new ArrayList(); this.lineList = new ArrayList(); } public override void eval() //寻找符合条件的文本行 {
30、 this.q1.eval(); this.q2.eval(); this.NumList = (ArrayList)this.q1.NumList.Clone(); this.LineList = (ArrayList)this.q1.LineList.Clone(); bool flag = false; //判断是否存在文本行的标记 //对查询二的文本行把不在查询一中文本加入一中 for
31、int i = 0; i < this.q2.NumList.Count; i++) { //如果查询一中含有此行,则不加入该行 for (int j = 0; j < this.NumList.Count; j++) { if (this.q2.NumList[i].Equals(this.NumList[j])) {
32、 flag = true; break; } } if (flag == false) //查询一中不含的行加入查询一 { this.NumList.Add(this.q2.NumList[i]); this.LineList.Add(this.q2.LineList[i]);
33、 } flag = false; } } public override void eval_print() //输出查询结果 { Console.Write("{0}\n \t{1} line(s) match: [", this.q1.ToString(), this.q1.NumList.Count); for (int i = 0; i < this.q1.NumList.Count; i++)
34、 { Console.Write(" {0}", this.q1.NumList[i]); } Console.WriteLine(" ]\n"); Console.Write("{0}\n \t{1} line(s) match: [", this.q2.ToString(), this.q2.NumList.Count); for (int i = 0; i < this.q2.NumList.Count; i++) { Console.Wr
35、ite(" {0}", this.q2.NumList[i]); } Console.WriteLine(" ]\n"); Console.Write("{0} || {1} \n \t{2} line(s) match: [",this.q1.ToString(),this.q2.ToString(),this.NumList.Count); for (int i = 0; i < this.NumList.Count; i++) { Console.Write
36、" {0}", this.NumList[i]); } Console.WriteLine(" ]\n"); for (int i = 0; i < this.NumList.Count; i++) { Console.WriteLine("[{0}]: {1}", this.NumList[i], this.LineList[i]);} Console.WriteLine(); } public override string
37、ToString() { return this.q1.ToString() + " || " + this.q2.ToString(); } } (6) 类型AndQuery的程序代码如下: public class AndQuery :Query { private Query q1; //声明两个Query变量 private Query q2; public AndQuery(Query q1, Query q2) //构造函数 : base
38、) { this.q1 = q1; //初始化哥变量 this.q2 = q2; this.numList = new ArrayList(); this.lineList = new ArrayList(); } //寻找符合条件的文本行 public override void eval() { this.q1.eval(); this
39、q2.eval(); //查找同时存在于两个查询中的文本行 for (int i = 0; i < this.q1.NumList.Count; i++) { for (int j = 0; j < this.q2.NumList.Count; j++) { if (this.q1.NumList[i].Equals(this.q2.NumList[j]))
40、{ this.NumList.Add(this.q1.NumList[i]); this.LineList.Add(this.q1.LineList[i]); break; } } } } public override void eval_print() //输出查询结果 {
41、 Console.Write("{0}\n \t{1} line(s) match: [", this.q1.ToString(), this.q1.NumList.Count); for (int i = 0; i < this.q1.NumList.Count; i++) { Console.Write(" {0}", this.q1.NumList[i]);} Console.WriteLine(" ]\n"); Console.Write("{0}\n \t{1}
42、line(s) match: [", this.q2.ToString(), this.q2.NumList.Count); for (int i = 0; i < this.q2.NumList.Count; i++) { Console.Write(" {0}", this.q2.NumList[i]); } Console.WriteLine(" ]\n"); Console.Write("{0} && {1} \n \t{2} line(s) match:
43、[", this.q1.ToString(), this.q2.ToString(), this.NumList.Count); for (int i = 0; i < this.NumList.Count; i++) { Console.Write(" {0}", this.NumList[i]); } Console.WriteLine(" ]\n"); for (int i = 0; i < this.NumList.Count; i++)
44、 { Console.WriteLine("[{0}]: {1}", this.NumList[i], this.LineList[i]); } Console.WriteLine(); } public override string ToString() { return this.q1.ToString()+" && "+this.q2.ToString(); } } } 数据记录 和计
45、算 数据记录 和计算 实验结果截图如下: 结 论 (结 果) 1.通过实验,对C#中的继承和多态有了深刻的理解和掌握; 2.面向对象程序设计的两个最主要特征是:继承和多态; 3.继承机制使我们得以将一群彼此相关的classes组织起来,并让我们得以分享其间的公共数据和公共操作行为; 4.多态机制让我们在这些classes身上进行编程时,可以如同操控单一class的方便,并赋予我们更多弹性来加入或移除任何特定的classes; 5.继承机制定义了父子关系。父类定义出所有子类公共的公开接口和私有实现,子类可以增加或覆写继承而来的东西,以便实现它自身独特的行为; 9






