好好学习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;
get;
}
}
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("饿死了,不说了");
return;
}
if(str.Contains("姓名")||str.Contains("名字"))
{
this.sayhello();//调用同类的另外一个方法
}
else if(str.Contains("女朋友"))
{
Console.WriteLine("年龄太小");
}
else
{
Console.WriteLine("听不懂");
}
fulllevel--;
}
}
}