收藏 分销(赏)

云大软件学院C#-实验6-继承及多态.doc

上传人:仙人****88 文档编号:7226949 上传时间:2024-12-28 格式:DOC 页数:9 大小:304KB 下载积分:10 金币
下载 相关 举报
云大软件学院C#-实验6-继承及多态.doc_第1页
第1页 / 共9页
云大软件学院C#-实验6-继承及多态.doc_第2页
第2页 / 共9页


点击查看更多>>
资源描述
指标等级 A B C D 功能完整 程序质量 按时检查 提问回答 检查时间 总评成绩 实 验 报 告 序 号: 实验老师: 课程名称: C#与.NET实验 实验名称: 实验6:继承和多态 学 号: 姓 名: 课程名称   C#与.NET实验 实验项目 实验6:继承和多态 实验目的  熟悉并掌握,C#的类继承与多态的特性。 实验内容(算法、程序、步骤和方法) 实验内容(算法、程序、步骤和方法) 实验内容(算法、程序、步骤和方法) 实验内容(算法、程序、步骤和方法) 实验内容(算法、程序、步骤和方法) 实验内容(算法、程序、步骤和方法) 实验内容(算法、程序、步骤和方法) (1) 类型QueryTest的程序代码如下: using System; using System.IO; using System.Collections; namespace QueryTextLanguageTest { public class QueryTest { static void Main(string[] args) { Query query = null; Console.WriteLine("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")) //退出查询 { Console.WriteLine("OK,bye!\nTextQuery: end()\n"); sign = false; } else { if (query != null) { query = QueryTest.parseQuery(statement); query.eval(); query.eval_print(); } else { Console.WriteLine("你的输入有误,请重新输入!\n"); } } } } //对输入的语句进行处理,返回查询类型 public static Query parseQuery(string 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.Split(' '); if (word.Length == 1) //匹配各种查询语句 { query = new NameQuery(statement); } else if (word.Length == 2 && word[0].Equals("!")) { query = new NotQuery(word[1]); } else if (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("&&")) { 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 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("&&")) { 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, q3); } else { query = null; } return query; } } } (2) 类型Query的程序代码如下: abstract public class Query { protected int textLines; protected StreamReader sr; protected string text_line = null; protected ArrayList numList = null; protected ArrayList lineList = null; public ArrayList NumList //文本行号链表 { get { return numList; } set { numList = value; } } public ArrayList LineList //文本行内容链表 { get { return lineList; } set { lineList = value; } } public int TextLines { get { return textLines; } } public Query() //构造函数 { sr = new StreamReader(@"F:\query.txt"); } public virtual void eval() { while ((text_line = sr.ReadLine())!= null) { textLines++; } } public virtual void eval_print() { Console.WriteLine(); } } (3) 类型NameQuery的程序代码如下: public class NameQuery : Query { private string name; //声明两个变量 private string line; public NameQuery(String name) //构造函数 : base() { this.name = name; //初始化各变量 this.numList = new ArrayList(); this.lineList = new ArrayList(); } //寻找符合条件的文本行 public override void 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); 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 < 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]);} Console.WriteLine(); } public override string ToString() { return this.name; } } (4) 类型NotQuery的程序代码如下: public class NotQuery : Query { private string name; //声明各个变量 private string line; private Query query; public NotQuery(String name) //构造函数 : base() { //初始化各变量 this.query = new NameQuery(name); this.name = name; this.numList = new ArrayList(); this.lineList = new ArrayList(); } //寻找符合条件的文本行 public override void eval() { this.query.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); this.LineList.Add(this.text_line); } } } public override void eval_print()//输出查询结果 { Console.Write("{0}\n \t{1} line(s) match: [", this.query.ToString(), 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); 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]); } Console.WriteLine(); } public override string ToString() { return this.query.ToString(); } } (5) 类型OrQuery的程序代码如下: public class OrQuery : Query { private Query q1; //声明两个Query变量 private Query q2; public OrQuery(Query q1,Query q2) //构造函数 :base() { this.q1 = q1; //初始化各变量 this.q2 = q2; this.numList = new ArrayList(); this.lineList = new ArrayList(); } public override void eval() //寻找符合条件的文本行 { this.q1.eval(); this.q2.eval(); this.NumList = (ArrayList)this.q1.NumList.Clone(); this.LineList = (ArrayList)this.q1.LineList.Clone(); bool flag = false; //判断是否存在文本行的标记 //对查询二的文本行把不在查询一中文本加入一中 for (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])) { flag = true; break; } } if (flag == false) //查询一中不含的行加入查询一 { this.NumList.Add(this.q2.NumList[i]); this.LineList.Add(this.q2.LineList[i]); } 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++) { 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.Write(" {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(" {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 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() { this.q1 = q1; //初始化哥变量 this.q2 = q2; this.numList = new ArrayList(); this.lineList = new ArrayList(); } //寻找符合条件的文本行 public override void eval() { this.q1.eval(); this.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])) { this.NumList.Add(this.q1.NumList[i]); this.LineList.Add(this.q1.LineList[i]); break; } } } } 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++) { 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.Write(" {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(" {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 ToString() { return this.q1.ToString()+" && "+this.q2.ToString(); } } } 数据记录 和计算 数据记录 和计算 实验结果截图如下: 结 论 (结 果) 1.通过实验,对C#中的继承和多态有了深刻的理解和掌握; 2.面向对象程序设计的两个最主要特征是:继承和多态; 3.继承机制使我们得以将一群彼此相关的classes组织起来,并让我们得以分享其间的公共数据和公共操作行为; 4.多态机制让我们在这些classes身上进行编程时,可以如同操控单一class的方便,并赋予我们更多弹性来加入或移除任何特定的classes; 5.继承机制定义了父子关系。父类定义出所有子类公共的公开接口和私有实现,子类可以增加或覆写继承而来的东西,以便实现它自身独特的行为; 9
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 小学其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服