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 else 22 { 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 Robot 34 { 35 public string Name { set; get; } 36 public string Speak { set; get; } 37 private int fullLevel; 38 public int FullLevel 39 { 40 set 41 { 42 if (this.fullLevel > 180) 43 { 44 Console.WriteLine("撑死了"); 45 } 46 else if (this.fullLevel<=60) 47 { 48 Console.WriteLine("饿死了"); 49 } 50 else 51 { 52 Console.WriteLine("吃饱了"); 53 } 54 } 55 get 56 { 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 }
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 Robot 19 { 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 else 48 { 49 Console.WriteLine("听不懂"); 50 } 51 FullLevel--; 52 } 53 } 54 }
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 else 25 { 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 Robot 40 { 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 else 63 { 64 if (str.Contains("名字")) 65 { 66 SayHello(); 67 } 68 else 69 { 70 Console.WriteLine("听不懂。"); 71 } 72 FullLevel--; 73 } 74 } 75 } 76 }
所以r指向“r1指向的对象”并不是r1这个变量,而是【new 机器人】这个对象,r不会因为r1的改变而改变了。