m_<:這里記錄C#學(xué)習(xí)的筆記,基礎(chǔ)的語法略去,重點(diǎn)在類、方法、繼承三項(xiàng)。
1 一個(gè)簡(jiǎn)單的類
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
person P1 = new person(); //類的實(shí)例
P1.Name = "Tom"; //字段賦值
P1.Age = 30;
P1.Height = 180;
P1.SayHello(); //調(diào)用方法
Console.ReadKey(); //讀到鍵盤輸入時(shí)結(jié)束
}
}
class person //新建一個(gè)類
{
public int Height; //類的字段(類似變量)
public int Age;
public string Name;
public void SayHello() //類的方法(類似函數(shù))
{
Console.WriteLine("大家好,我叫{0},我的身高是{1}",this.Name,this.Height); //輸出一行字符串
}
}
}
this的用法:
1
2 在類的方法中作為值類型,表示對(duì)調(diào)用該方法的對(duì)象的引用
3
4
2 類成員
字段、方法、屬性都是類的成員,需要定義訪問級(jí)別,字段盡量不用public。
public:公有成員,任何地方都可以訪問
private:私有成員,只能由類成員訪問(默認(rèn))
protected:保護(hù)成員
internal:內(nèi)部成員
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
person P2 = new person();
//P2.Name = "Tom"; 無法直接設(shè)置Name字段
P2.GiveName("Tom"); //通過調(diào)用public的GiveName方法來設(shè)置Name字段
P2.Age = 30;
P2.Height = 180;
P2.SayHello();
Console.ReadKey();
}
}
class person
{
public int Height;
private string Name;
public void GiveName(string name) //類中成員可訪問private型成員
{
if (name=="Jerry")
{
return;
}
this.Name = name;
}
public void SayHello()
{
Console.WriteLine("大家好,我叫{0},我的身高是{1}",this.Name,this.Height);
}
}
3 屬性
為私有的字段配一個(gè)公有的屬性,方便取值賦值。字段首字母小寫,屬性大寫。
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
person P3 = new person();
P3.Age = 2; //賦值
P3.SayHello();
Console.ReadKey();
}
}
class person
{
public int height;
private int age; //字段age
public int Age //屬性Age,屬性沒有存儲(chǔ)功能,數(shù)據(jù)在age中
{
set
{
if (value<0) //屬性可以控制非法值
{
return;
}
this.age = value;
}
get {return this.age;}
}
public void SayHello()
{
Console.WriteLine("大家好,我的年齡是{0}",this.age);
}
}
4 一個(gè)面向?qū)ο蟪绦?/h2>
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
robot r1 = new robot();
r1.Name = "小I";
r1.Eat(5);
r1.SayHello();
while (true)
{
string str = Console.ReadLine();
r1.Speak(str);
}
Console.ReadKey();
}
}
class robot
{
public string Name { get; set; } //簡(jiǎn)化,機(jī)器人名字
private int FullLevel { get; set;} //等級(jí)
public void SayHello()
{
Console.WriteLine("我叫{0}!",Name); //this.Name亦可
}
public void Eat(int foodCount) //喂食
{
if (FullLevel > 100)
{
return;
}
FullLevel += foodCount;
}
public void Speak(string str) //問答
{
if (FullLevel<=0)
{
Console.WriteLine("說不了,餓死了!");
return;
}
if (str.Contains("姓名")||str.Contains("名字"))
{
this.SayHello(); //類的方法調(diào)用另一個(gè)方法
}
else if (str.Contains("女朋友"))
{
Console.WriteLine("年齡小,不考慮。");
}
else
{
Console.WriteLine("聽不懂...");
}
FullLevel--;
}
}
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
robot r1 = new robot();
r1.Name = "小I";
r1.Eat(5);
r1.SayHello();
while (true)
{
string str = Console.ReadLine();
r1.Speak(str);
}
Console.ReadKey();
}
}
class robot
{
public string Name { get; set; } //簡(jiǎn)化,機(jī)器人名字
private int FullLevel { get; set;} //等級(jí)
public void SayHello()
{
Console.WriteLine("我叫{0}!",Name); //this.Name亦可
}
public void Eat(int foodCount) //喂食
{
if (FullLevel > 100)
{
return;
}
FullLevel += foodCount;
}
public void Speak(string str) //問答
{
if (FullLevel<=0)
{
Console.WriteLine("說不了,餓死了!");
return;
}
if (str.Contains("姓名")||str.Contains("名字"))
{
this.SayHello(); //類的方法調(diào)用另一個(gè)方法
}
else if (str.Contains("女朋友"))
{
Console.WriteLine("年齡小,不考慮。");
}
else
{
Console.WriteLine("聽不懂...");
}
FullLevel--;
}
}