m_<:這里記錄C#學(xué)習(xí)的筆記认烁,基礎(chǔ)的語法略去总滩,重點在類、方法柳刮、繼承三項挖垛。
1 一個簡單的類
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
person P1 = new person(); //類的實例
P1.Name = "Tom"; //字段賦值
P1.Age = 30;
P1.Height = 180;
P1.SayHello(); //調(diào)用方法
Console.ReadKey(); //讀到鍵盤輸入時結(jié)束
}
}
class person //新建一個類
{
public int Height; //類的字段(類似變量)
public int Age;
public string Name;
public void SayHello() //類的方法(類似函數(shù))
{
Console.WriteLine("大家好,我叫{0}痒钝,我的身高是{1}",this.Name,this.Height); //輸出一行字符串
}
}
}
this的用法:
1
2 在類的方法中作為值類型,表示對調(diào)用該方法的對象的引用
3
4
2 類成員
字段痢毒、方法送矩、屬性都是類的成員,需要定義訪問級別哪替,字段盡量不用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 屬性
為私有的字段配一個公有的屬性,方便取值賦值帅霜。字段首字母小寫匆背,屬性大寫。
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身冀,屬性沒有存儲功能钝尸,數(shù)據(jù)在age中
{
set
{
if (value<0) //屬性可以控制非法值
{
return;
}
this.age = value;
}
get {return this.age;}
}
public void SayHello()
{
Console.WriteLine("大家好,我的年齡是{0}",this.age);
}
}
4 一個面向?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; } //簡化,機(jī)器人名字
private int FullLevel { get; set;} //等級
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)用另一個方法
}
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; } //簡化,機(jī)器人名字
private int FullLevel { get; set;} //等級
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)用另一個方法
}
else if (str.Contains("女朋友"))
{
Console.WriteLine("年齡小,不考慮剩愧。");
}
else
{
Console.WriteLine("聽不懂...");
}
FullLevel--;
}
}