博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
面向对象聊天机器人
阅读量:7102 次
发布时间:2019-06-28

本文共 5756 字,大约阅读时间需要 19 分钟。

1 namespace ConsoleApplication1 2 { 3     public class Program 4     { 5         static void Main(string[] args) 6         { 7             Robot r = new Robot(); 8             Console.WriteLine("请选择机器人:1/2"); 9             string s = Console.ReadLine();10             if (s == "1")11             {12                 Robot r1 = new Robot();13                 r1.Name = "机器人1号";14                 r1.Speak = "hello";15                 Console.WriteLine("喂饭");16                 int f=Convert.ToInt32(Console.ReadLine());17                 r1.Eat(f);//f作为参数传进方法中,f赋值给FullLevel属性有值,直接赋值给属性没有值。18                 r1.FullLevel = f;19                 r1.SayHello();20             }21             else22             {23                 Robot r2 = new Robot();24                 r2.Name = "机器人2号";25                 r2.Speak = "hello";26                 Console.WriteLine("喂饭");27                 r2.FullLevel = Convert.ToInt32(Console.ReadLine());28                 r2.SayHello();29             }30             Console.ReadKey();31         }32     }33     public class Robot34     {35         public string Name { set; get; }36         public string Speak { set; get; }37         private int fullLevel;38         public int FullLevel39         {40             set41             {42                 if (this.fullLevel > 180)43                 {44                     Console.WriteLine("撑死了");45                 }46                 else if (this.fullLevel<=60)47                 {48                     Console.WriteLine("饿死了");49                 }50                 else51                 {52                     Console.WriteLine("吃饱了");53                 }54             }55             get56             {57                 return this.fullLevel;58             }59         }60         public void SayHello()61         {62              Console.WriteLine("{0},我是{1},我吃的{2}",this.Speak,this.Name,FullLevel);63         }64         //喂食65         public void Eat(int foodCount)66         {67             this.fullLevel = foodCount;68         }69     }70 }
View Code
1 namespace ConsoleApplication1 2 { 3     public class Program 4     { 5         static void Main(string[] args) 6         { 7             Robot r = new Robot(); 8             r.Eat(5);//方法里面处理逻辑(饱的程度累加),别人调用只用给数据(参数) 9             r.Name = "机器人1号";10             while (true)11             {12                 string say = Console.ReadLine();13                 r.Speak(say);//方法里面处理逻辑(处理回复结果),别人调用只用给数据(参数)14             }15             Console.ReadKey();16         }17     }18     public class Robot19     {20         public string Name { set; get; }21         public int FullLevel { set; get; }//饱的程度22         public void SayHello()23         {24             Console.WriteLine("我叫{0}", this.Name);25             FullLevel--;26         }27         //喂食28         public void Eat(int foodCount)29         {30             FullLevel = FullLevel + foodCount;31         }32         public void Speak(string str)33         {34             if (FullLevel <= 0)35             {36                 Console.WriteLine("饿死了。");37                 return;38             }39             if (str.Contains("姓名") || str.Contains("名字"))40             {41                 SayHello();//同一个类中调用类中的方法。42             }43             else if (str.Contains("岁"))44             {45                 Console.WriteLine("20岁");46             }47             else48             {49                 Console.WriteLine("听不懂");50             }51             FullLevel--;52         }53     }54 }
View Code
1 namespace ConsoleApplication1 2 { 3     public class Program 4     { 5         static void Main(string[] args) 6         { 7             Robot r; 8             Robot r1 = new Robot(); 9             Robot r2 = new Robot();10             Console.WriteLine("请选择一个机器人:1→小I,2→小J");11             string selectR = Console.ReadLine();12             if (selectR == "1")13             {14                 r = r1;//r指向“r1指向的对象即new Robot()”15                 r1.Name = "小I";16                 r1.Eat(5);17                 r1.SayHello();18                 while (true)19                 {20                     string talk = Console.ReadLine();21                     r1.Speak(talk);22                 }23             }24             else25             {26                 r = r2;//r指向“r2指向的对象即new Robot()”27                 r2.Name = "小J";28                 r2.Eat(8);29                 r2.SayHello();30                 while (true)31                 {32                     string talk = Console.ReadLine();33                     r2.Speak(talk);34                 }35             }36             Console.ReadKey();37         }38     }39     public class Robot40     {41         public string Name { set; get; }42         public int FullLevel { set; get; }43         public void SayHello()44         {45             Console.WriteLine("我叫{0}", this.Name);46             FullLevel--;47         }48         public void Eat(int eatfood)49         {50             FullLevel = FullLevel + eatfood;51         }52         public void Speak(string str)53         {54             if (FullLevel >= 100)55             {56                 Console.WriteLine("撑死了。");57             }58             else if (FullLevel < 0)59             {60                 Console.WriteLine("饿死了。");61             }62             else63             {64                 if (str.Contains("名字"))65                 {66                     SayHello();67                 }68                 else69                 {70                     Console.WriteLine("听不懂。");71                 }72                 FullLevel--;73             }74         }75     }76 }
View Code

所以r指向“r1指向的对象”并不是r1这个变量,而是【new 机器人】这个对象,r不会因为r1的改变而改变了。

转载于:https://www.cnblogs.com/chuizhuizhigan/p/3210830.html

你可能感兴趣的文章
关于tableView的cell复用问题
查看>>
微博是个大金矿,使用VS2010编译QOAuth支持微博通用认证OAuth实现SINA微博“.NET研究”登陆...
查看>>
工信部通知要求加强域名系统安全保障工作
查看>>
How Digg is Built:讲述Digg背后的技术,互联网营销
查看>>
7款强大的Javascript网格插件推荐
查看>>
一起谈.NET技术,你是个软件架构师吗?
查看>>
JS小游戏
查看>>
ios 九宫格布局,块动画,字典转模型,Xib使用
查看>>
Lucene学习笔记
查看>>
微软Xbox One无线手柄控制机器人
查看>>
zepto 基础知识(2)
查看>>
汇编语言 实验12 编写0号中断的处理程序
查看>>
方法进阶,构造器
查看>>
redhat6.4 elasticsearch1.7.3安装配置
查看>>
ant深入浅出(一)ant+xdoclet 生成hibernate配置文件以及实体映射文件
查看>>
linux -- Ubuntu network-manager
查看>>
SharePoint 2013 状态机工作流之日常报销示例
查看>>
坐标旋转变换公式的推导
查看>>
集合已修改 ;可能无法执行枚举操作 Dictionary
查看>>
裴蜀定理(贝祖定理)及证明
查看>>