標籤:core class ring min 執行 task 屬性 str protected
命名空間/程式集:
命名空間是:namespace 後面可以起名字。在上面的是引用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
用到那個功能就要引用那個using.
如果建立一個新檔案夾aaa,並在檔案夾中建立一個類,那麼要向using中引用才能實現。
存取修飾詞:
public - 公用的
private - 私人的
internal - 預設的
protected - 被保護的
類:
成員變數
private string _name;
屬性
public string name{
get {return _name;}
set{_name = value;}
}
public decimal scoresum():
帶著括弧的是方法
比如求和的時候
public decimal scoresum(){
return _cscore+_mscore+_escore;
}
建構函式是預設在每個類中都有一個建構函式,建構函式中沒有傳回型別,類名叫什麼,建構函式叫什麼:
預設隱藏。
public student(){
}
執行個體化就是執行建構函式的一個過程。
其中靜態表示為:
public static decimal jiafa (decimal a , decimal b){
return a+b
}
靜態類不需要執行個體化,直接通過類名點出來:
decimal aaa =100;
decimal bbb=60;
decimal ccc = mamath .jiafa(aaa,bbb);
console.writeLine(ccc);
三局兩勝猜拳遊戲:
namespace 三局兩勝猜拳遊戲
{
class Program
{
static void Main(string[] args)
{
Player user = new Player();
Console.Write("請輸入您的名稱:");
user.Name = Console.ReadLine();
string[] comnames = new string[] { "劫", "盲僧", "小魚人", "亞索" };
Player com = new Player();
Random r = new Random();
int comind = r.Next(0, comnames.Length);
com.Name = comnames[comind];
Console.WriteLine("==============比賽者入場================");
Console.WriteLine("一號選手:" + user.Name);
Console.WriteLine("二號選手:" + com.Name);
while (true)
{
Console.Write("請輸入您的手勢:");
user.Hand = Convert.ToInt32(Console.ReadLine());
com.Hand = r.Next(0, 3);
if (user.Hand - com.Hand == -1 || user.Hand - com.Hand == 2) {
Console.WriteLine(user.Name + "手勢為:"+user .HandStr );
Console.WriteLine(com.Name + "手勢為:"+com.HandStr );
Console.WriteLine("*******" + user.Name + "勝利一局********");
user.Vic += 1;
Console.WriteLine(user.Name + "共勝利"+user.Vic +"局");
Console.WriteLine(com.Name + "共勝利"+com .Vic +"局");
}
else if (user.Hand - com.Hand == -2 || user.Hand - com.Hand == 1)
{
Console.WriteLine(user.Name + "手勢為:" + user.HandStr);
Console.WriteLine(com.Name + "手勢為:" + com.HandStr);
Console.WriteLine("*******" + com.Name + "勝利一局********");
com.Vic += 1;
Console.WriteLine(user.Name + "共勝利" + user.Vic + "局");
Console.WriteLine(com.Name + "共勝利" + com.Vic + "局");
}
else {
Console.WriteLine(user.Name + "手勢為:" + user.HandStr);
Console.WriteLine(com.Name + "手勢為:" + com.HandStr);
Console.WriteLine("*******平 局********");
Console.WriteLine(user.Name + "共勝利" + user.Vic + "局");
Console.WriteLine(com.Name + "共勝利" + com.Vic + "局");
}
if (user.Vic == 2) {
Console .WriteLine ("**************************");
Console.WriteLine(user.Name + "獲得最終勝利");
Console.WriteLine("***************************");
}
if (com.Vic == 2)
{
Console.WriteLine("**************************");
Console.WriteLine(com.Name + "獲得最終勝利");
Console.WriteLine("***************************");
}
Console.ReadLine();
}
}
}
}
namespace 三局兩勝猜拳遊戲
{
public class Player
{
public Player() {
_Vic = 0;
}
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
private int _Hand;
public int Hand
{
get { return _Hand; }
set { _Hand = value; }
}
private int _Vic;
public int Vic
{
get { return _Vic; }
set { _Vic = value; }
}
public string HandStr{
get {
string s = "石頭";
if (_Hand == 1) {
s = "剪刀";
}
else if (_Hand == 2) {
s = "包袱";
}
return s;
}
}
}
}
C#物件導向基礎