好好學習c#代碼,學了怎麼調用類
using System;
using System.Collections.Generic;
using
System.Linq;
using System.Text;
namespace robot
{
class Program
static void
Main(string[] args)
robot r1=new robot();
r1.name="cyychen";
r1.Eat(15);
robot r2 =
new robot();//r1指向的對象的變量
r2.name = "malone";
r2.Eat(14);
Console.WriteLine("請選擇機器人,1→cyychen,2→malone");
robot r;
string str = Console.ReadLine();
if(str=="1")
r=r1;//r指向r1指向的對象;不是指向r1;
}
else
r=r2;//程式中僅有兩個對象。
r.sayhello();
while (true)
string str1 = Console.ReadLine();
r.speak(str);
/* person p1=new
person();
p1.Age=30;
Console.WriteLine(p1.Age);*/
Console.ReadKey();
class person
public int Age
set;//編譯器自動幫我們生成私有字段和set,get代碼
get;
public string name
set;
class robot
public string name { get;
set; }
private int fulllevel { get; set; }
public void
sayhello()
Console.WriteLine("我叫{0}", name);
public void Eat(int foodcount)
if
(fulllevel > 100)
return;
fulllevel = fulllevel + foodcount;
public void speak(string str)
if(fulllevel<=10)
Console.WriteLine("餓死了,不說了");
if(str.Contains("姓名")||str.Contains("名字"))
this.sayhello();//調用同類的另外一個方法
else if(str.Contains("女朋友"))
Console.WriteLine("年齡太小");
Console.WriteLine("聽不懂");
fulllevel--;