2012年3月1日c#基础练习
namespace 对象的引用
{
class Program
{
static void Main(string[] args)
{
int i = 10;
int j = i;
i++;
Console.WriteLine(j);//输出10值类型是传递拷贝,不能引用指向的值
Person a = new Person();
Person b = a;
a.Shengao++;
Console.WriteLine(b.Shengao);//对象是引用类型,是传递的引用。a变b变
Console.ReadKey();
}
}
class Person
{
private int shengao;
public int Shengao
{
set{this.shengao = value; }
get { return shengao;}
}
}
}
namespace 练习33以后
{
enum pp { sasa=-100,dsf=200,fg}
class Program
{
static void Main(string[] args)
{
//练习33
/*
Persong p = new Persong();
p.age=30;
p.height=20;
p.name="sadsaf";
p.sayhello();
*/
Robot a = new Robot();
a.fullleven = 1;
a.Sayhello();
a.Eatfood(4);
while (true)
{
string str = Console.ReadLine();
a.Speak(str);
}
Console.ReadKey();
}
}
-----------------------------------------------------
//练习33用的类
/*
class Persong
{
public int height;
public int age;
public string name;
public void sayhello()
{
Console.WriteLine("大家好我是{0}我的年龄是{1}我的身高是{2}",this.name,this.age,this.height);
}
}
*/
class Robot
{ public int fullleven;
public string Name { get; set; }
public void Sayhello()
{
Console.WriteLine("大家好我叫xiaoai");
}
public void Eatfood( int food)
{
if(fullleven>100)
{
Console.WriteLine("不吃了撑死了");
}
fullleven = fullleven + food;
}
public void Speak( string str)
{
if(fullleven<=0)
{
Console.WriteLine("饿死了,不说了,你可以喂我点食物");
return;
}
if(str.Contains("名字"))
{
this.Sayhello();
}
else if (str.Contains("老婆"))
{
Console.WriteLine("我还小");
}
else
{
Console.WriteLine("听不懂");
}
fullleven--;
}
}
}
--------------------------
namespace 构造函数
{
class Program
{
static void Main(string[] args)
{
Person s = new Person();
Person s1 = new Person("小狗");
Person s2 = new Person("小猫",23);
Console.WriteLine("{0}",s.Name);
Console.WriteLine("{0}",s1.Name);
Console.WriteLine("{0}{1}",s2.Name,s2.Age);
Console.ReadKey();
}
}
class Person
{
public string Name { get; set; }//属性,可以控制写与读提高安全性,字段不可控制
public int Age { get; set; }
public Person()
{
this.Name = "未命名";
}
public Person(string name)
{
this.Name = name;
}
public Person(string name,int age)//构造函数的作用是创建对象的同时可以对对象进行初始化
{
this.Name = name;
this.Age = age;
}
}
}
-----------------------
namespace 继承
{
class Program
{
static void Main(string[] args)
{
Person 人 = new Person();
中国人 zgr = new 中国人();
zgr.Name = "中国人";//子类可以用父类中的东西
Console.WriteLine(zgr.Name);
韩国人 hgr = new 韩国人();
hgr.Name = "韩国人";
Console.WriteLine(hgr.Name);
Person ren = zgr;
ren.Speak();
zgr =(中国人)ren;
Console.ReadKey();
}
}
class Person
{
public string Name { get; set; }
public void Speak()
{
Console.WriteLine("{0}",this.Name);
}
}
class 中国人 : Person
{
public int Age { get; set; }
public void Childage()
{
Console.WriteLine("中国人的年龄是{0}",this.Age);
}
}
class 韩国人 : Person
{
public int Fl { get; set; }
public void Hanguofl()
{
Console.WriteLine("韩国人的饭量是{0}",this.Fl);
}
}
}
----------------------------------
namespace 索引器
{
class Program
{
static void Main(string[] args)
{
Person a = new Person();
a[1,1] = "哈哈";
Console.WriteLine(a[1,1]);
Console.ReadKey();
}
}
class Person
{
public string name = "恭喜你中奖了";
public string this[int x,int y]
{
set
{
if (x==y)
{
name = value;
}
else
{
Console.WriteLine("dsf");
}
}
get
{
return name;
}
}
}
}