您的位置:首页 > 编程语言 > C#

c#学习笔记

2011-11-25 21:03 375 查看
*属性: 属性开头字母大写

属性可以判断输入的非法值

属性本身不存储值->依靠字段

*索引器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace b
{
class Program
{
static void Main(string[] args)
{
//Robot r1 = new Robot();
//Robot r2 = new Robot();

//r1.Name = "1";
//r1.Eat(5);

//Console.WriteLine("hi");

//while(true)
//{
//    Console.WriteLine("please input the string");
//    string str = Console.ReadLine();
//    r1.Speak(str);
//}

//try
//{
//    int i = Convert.ToInt32("abc");
//}
//catch (Exception e)
//{
//    Console.WriteLine(e.Message + "@" + e.StackTrace);
//}

//Console.ReadKey();

person p = new person();
p[1] = 2;
p[2] = 3;
Console.WriteLine(p[1]);
}
}

class Robot
{
private string name;
private int food;
public string Name
{
get
{
return name;
}
set
{
this.name = value;
}
}

private int FullLevel
{
get
{
return food;
}
set
{
this.food = value;
//避免如此写法
//this.FullLevel = value;
}
}

public void SayHello()
{
Console.WriteLine("My name is {0}",Name);
}

public void Eat(int foodCount)
{
if(foodCount > 100)
{
return;
}
FullLevel += foodCount;
}

public void Speak(string str)
{
if(FullLevel <= 0)
{
Console.WriteLine("stop");
return;
}
if(str.Contains("name") || str.Contains("Name"))
{
this.SayHello();
}
else if (str.Contains("gril"))
{
Console.WriteLine("suck");
}
else
{
Console.WriteLine("unkonwn");
}
FullLevel--;
}
}

class person
{
private int a;
private int b;

public int this[int index]
{
set
{
if (index == 1)
{
a = value;
}
else if(index ==2)
{
b = value;
}
else
{
throw new Exception ("error");
}
}

get
{
if (index == 1)
{
return this.a;
}
else if(index ==2)
{
return this.b;
}
else
{
throw new Exception("error1");
}
}
}
}
}


*有中文用nvarchar

*SQL语句中字符串用单引号
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息