关于《关于一道C#上机题的一点想法》
题目:17个人围成一圈,从第一个人开始报数,报到3的退出,一直到剩下最后一个人,用面向对象的思想去做这道题。
前面两篇感觉上还是不够面向对象,至少要有面向对象三大特征:封装,继承,多态吧,
第一篇仅是构建了一个双向循环链表数据结构,思想还是面向过程的。
第二篇用到了泛型委托,但是思想和特定技术是不相关的,用到泛型委托未必就是面向对象啊。
所以我也斗胆写了下这道题,请大家指教了:
首先说明,这里用到了状态模式,单例模式,主要还是状态模式,实现了一个简单的状态机,
先上图:
再上代码:
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace Test
6 {
7 interface IPersonState
8 {
9 void Handle();
10
11 void Handle(Person person);
12 }
13 }
14
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace Test
6{
7 class First:IPersonState
8 {
9 IPersonState 成员#region IPersonState 成员
10
11 public void Handle()
12 {
13 throw new Exception("The method or operation is not implemented.");
14 }
15
16 #endregion
17
18 IPersonState 成员#region IPersonState 成员
19
20
21 public void Handle(Person person)
22 {
23 if (person.Next != person)
24 {
25 person.Next.State = new Second();
26 PersonLink.Instance.RootPerson = person.Next;
27 person.Next.Call();
28 }
29 else
30 {
31 person.Next.State = new Last();
32 person.Next.Call();
33 }
34 }
35
36 #endregion
37 }
38}
39
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace Test
6{
7 class Second:IPersonState
8 {
9 IPersonState 成员#region IPersonState 成员
10
11 public void Handle()
12 {
13 throw new Exception("The method or operation is not implemented.");
14 }
15
16 #endregion
17
18 IPersonState 成员#region IPersonState 成员
19
20
21 public void Handle(Person person)
22 {
23 if (person.Next != person)
24 {
25 person.Next.State = new Three();
26 PersonLink.Instance.RootPerson = person.Next;
27 person.Next.Call();
28 }
29 else
30 {
31 person.Next.State = new Last();
32 person.Next.Call();
33 }
34 }
35
36 #endregion
37 }
38}
39
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace Test
6{
7 class Three:IPersonState
8 {
9 IPersonState 成员#region IPersonState 成员
10
11 public void Handle()
12 {
13 throw new Exception("The method or operation is not implemented.");
14 }
15
16 #endregion
17
18 IPersonState 成员#region IPersonState 成员
19
20
21 public void Handle(Person person)
22 {
23
24 Console.WriteLine(person.Val);
25 person.Prev.Next = person.Next;
26 person.Next.Prev = person.Prev;
27 PersonLink.Instance.RootPerson = person.Next;
28 if (PersonLink.Instance.RootPerson.Next == PersonLink.Instance.RootPerson)
29 {
30 person.Next.State = new Last();
31 }
32 else
33 {
34 person.Next.State = new First();
35 }
36 person.Next.Call();
37 }
38 #endregion
39
40
41 }
42}
43
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading;
5
6namespace Test
7{
8 class Last:IPersonState
9 {
10 IPersonState 成员#region IPersonState 成员
11
12 public void Handle()
13 {
14 throw new Exception("The method or operation is not implemented.");
15 }
16
17 public void Handle(Person person)
18 {
19 Console.WriteLine("The last "+person.Val.ToString());
20 Thread.Sleep(20000);
21 }
22
23 #endregion
24 }
25}
26
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace Test
6{
7 class Person
8 {
9 private IPersonState _state = null;
10
11 public IPersonState State
12 {
13 get
14 {
15 return _state;
16 }
17 set
18 {
19 this._state = value;
20 }
21 }
22 private Person _pre=null;
23 private Person _next=null;
24 private int _val = 1;
25
26 public Person Prev
27 {
28 get{ return _pre; }
29 set{ _pre = value;}
30 }
31
32 public Person Next {
33 get { return _next; }
34 set { _next = value; }
35 }
36 public int Val {
37 get { return _val; }
38 set { _val = value; }
39 }
40
41 public Person() { }
42 public Person( Person pre,Person next, int val)
43 {
44 Next = next;
45 Val = val;
46 Prev = pre;
47 }
48 public void Call()
49 {
50 this.State.Handle(this);
51 }
52 }
53}
54
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace Test
6{
7 class PersonLink
8 {
9 private int count = 0;
10 private Person root = null;
11 public Person RootPerson
12 {
13 get
14 {
15 if (root == null)
16 {
17 root = new Person(null,null, 1);
18 }
19 return root;
20 }
21 set
22 {
23 root = value;
24 }
25 }
26
27 public static PersonLink Instance
28 {
29 get
30 {
31 if (_instance == null)
32 {
33 _instance = new PersonLink();
34 }
35 return _instance;
36 }
37 }
38 private static PersonLink _instance = null;
39
40 public PersonLink()
41 {
42
43 }
44
45 public void InitLink(int count)
46 {
47 this.count = count;
48 //初始化数据
49 Person temp = this.RootPerson;
50 for (int i = 2; i <= count; i++)
51 {
52 Person p = new Person(temp,null, i);
53 temp.Next = p;
54 temp = p;
55 }
56 temp.Next =this.RootPerson;
57 RootPerson.Prev = temp;
58 }
59
60
61 internal void Start()
62 {
63 this.RootPerson.State = new First();
64 this.RootPerson.Call();
65 }
66
67 }
68}
69
调用:
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace Test
6{
7 class Program
8 {
9 static void Main(string[] args)
10 {
11 PersonLink.Instance.InitLink(17);
12 PersonLink.Instance.Start();
13 }
14 }
15}
16
运行结果:
源代码:/Files/hongyin163/Test.rar
不知道大家感觉怎样?
转载于:https://www.cnblogs.com/hongyin163/archive/2009/08/27/1555012.html
关于《关于一道C#上机题的一点想法》相关推荐
- 学计算机是不是必须要买电脑,上大学了,你必须买一部电脑吗?
有一位今年的大一新生在网上问:上大学了,必须买电脑吗? 对于这个问题,大家的看法没有大的分歧,我们总结整理一下,大致观点如下: 一,根据学生学习的需要情况. 在大学里,有些专业学习时必须用到电脑,类似 ...
- 菜鸟学编程,用x86二手本学习嵌入式编程(硬件编程)
最近一直忙着上课,一直忙着写论文,一直忙着准备考试,但是脑子没有停止思考. 下学是结合个人的学习经历,给希望学习软件的菜鸟一点点帮助. 1:菜鸟也想学编程 大部分同学从大学开始接触软件编程,但其实有一 ...
- 小孩多大适合学编程机器人
小孩多大适合学编程机器人?小孩的学习一直以来都是家长们非常关心和重视的一件事情.很多的家长在培养孩子的学习的时候,在选择学习课程的时候,可以说是非常的用心的.就拿现在很多的家长想要孩子去学习机器人编程 ...
- 关于《学编程,还有必要上大学吗? 》
这两天登录CSDN,下面的广告弹出链接写着<学编程,还有必要上大学吗?>原来是篇针对<收回你的偏见,软件培训机构更能出人才>和<90后高中生学编程月薪过万,刺痛了谁?&g ...
- 电路板必须用c语言编程吗,上大学才知道绿油油的板子叫PCB,如何成为一名电子工程师...
原标题:上大学才知道绿油油的板子叫PCB,如何成为一名电子工程师 本文为十周年征文来稿,活动还在就行中欢迎烧友们拉到文章底部了解参加 对活动有疑问欢迎联系小助手(微信:XM1007721171) 我是 ...
- @大学生,送给步入大学生活的你,高速学编程。
大学生怎样学习编程,学哪个编程语言好? 上大学后学哪门编程语言好?大学生学哪个编程语言好就业?学哪个编程语言工资高?新手如何自学编程?哪个编程语言开发的软件多?大公司都招聘哪些编程语言人才? 相信很多 ...
- 大学java怎么算学得好_为什么现在大学里很多计算机系的大学生,学编程的时候总学不好...
小编发现如今在大学校园里,总是会出现一种现象,就是现在大学里很多计算机系的大学生,学编程的时候总学不好,这到底是为什么呢?因为看不懂?因为不认真?相信很多同学也会感到疑惑,那小编就一起来分析一下吧,同 ...
- 大学如何开始学编程?
大学如何开始学编程? 作为编程初学者每个人一开始都很迷茫不知如何学习,学习也没动力,其实大家都是一样的,但这并不意味着你会成为一个糟糕的 程序猿(媛).学习编程关键是要找到一种合适的语言,坚持学习,学 ...
- 3.4 学编程不拘于语言,学语言不限于平台——《逆袭大学》连载
返回到[全文目录] 目录 3.4 学编程不拘于语言,学语言不限于平台 编程语言 软件和硬件--计算机要作为一个整体看待 语言的江湖 语言不是回事 多平台上的精彩 3.4 学编程不拘于语言,学语言不限于 ...
- 学编程会拉低数学成绩,巴黎大学跟踪1500个小学生得出结论
"怎么培养数学逻辑思维?来点编程试试?" 大概这是很多人的既有认知,毕竟感觉程序员们的数学都挺好的. 巴特,反转来了. 来自巴黎大学的研究人员发现,小学生改用编程课学数学后,对成绩 ...
最新文章
- 什么样的显卡能支持 4K 分辨率输出?
- iOS 获取app进程被杀死事件applicationWillTerminate
- C语言再学习 -- linux 压缩与解压缩含义
- office软件的发展前景_2018年办公软件产业发展趋势
- [转载]创建数据库与完成数据添删改查--第一种写法
- 服务器USB启动故障一例
- 18-Gm-TransH:Group-Constrained Embedding of Multi-fold Relations in Knowledge Bases,嵌入,transH,n-ary
- utf8编码为什么这么普遍,优势在哪里?
- 【转】const int *p和int * const p的区别(常量指针与指向常量的指针)
- BZOJ 4810 [Ynoi2017]由乃的玉米田(莫队+bitset)
- 腾讯优图 ncnn AI 框架宣布加入 ONNX 开放生态系统
- 自制solidworks图框步骤_如何建立标准的solidworks图框模板
- Multisim14安装教程(下载链接在文末)
- ip地址和域名的关系是什么?
- 如何检测猥琐的私有SDWAN隧道协议
- openGauss 简介
- 关于华为校招面试的那些事儿
- 计算机电源 安装方向,怎么安装计算机电源
- 陈平原:《阅读大学的六种方式》[转]
- “无人驾驶汽车系统入门”博客专栏
热门文章
- Android记事本开发02
- 子矩阵(暴搜(全排列)+DP)
- GNU Radio的hello world(转)
- 【转】启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误的解决方法!...
- R语言警告:Cannot compute exact p-value with ties的处理方法
- bps计算机,bps指的是计算机的什么
- java futher多线程_Java多线程系列--“JUC集合”05之 ConcurrentSkipListMap
- python argparse库_python标准库之argparse
- MFC滑块的使用方式
- pyqt5 qscrollarea到达_在PYQT5中QscrollArea(滚动条)的使用方法