前言:
笔者是跟着哔站课程(Trigger)学习unity才去学习的C#,并且C语言功底尚存,所以只是简单地跟着课程将unity所用的C#语言的关键部分进行了了解,然后在后期unity学习过程中加以深度学习。如需完善的C#知识,推荐CSDN博主:呆呆敲代码的小Y - 链接: link
具体学习部分如下,建议将后面的源代码复制到vs打开后按顺序查看,其中EP标注的是笔者的课程集数
需要用到哪个部分取消该部分定义及Main语句里的注释即可,部分内容有所串通,不使用时重新注释,防止调试时出现问题
所有内容只需要简单搜索就可以找到解释
其中比较重要的部分是public,static等的理解,以及父子集的运用,推荐还是跟着课程学习为好
#region 和 #endregion 是用来分区的,便于找到所需部分,不用该部分时,可以点击 #region 左边的箭头进行缩略
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Serialization;
//快捷键
//Tab 一键补齐
//cw+Tab Console.WriteLine的快速输入
namespace c__Project_3_28
{
internal class Program
{
#region EP6 函数
static void RecoverPlayerHP()
{
Console.WriteLine("玩家血量回复了");
}
static int Add()
{
int a = 1 + 1;
return a;
}
#endregion
#region EP7 结构体
public struct Role
{
public string name;
public int level;
public int HP;
}
#endregion
#region EP7 枚举
public enum OCCUPATION
{
WARRIOR,//战士
MASTER,//法师
SHOOTER,//射手
ASSASSIN,//刺客
ASSIST//辅助
}
#endregion
#region EP8 面向对象、类
public class Bussinessman
{
private string name;
public int money;
public int goods;
public Bussinessman()
{
}
public Bussinessman(string bName, int bMoney, int bGoods)
{
name = bName;
money = bMoney;
goods = bGoods;
Console.WriteLine("当前商人的名字是:" + name);
}
public void SetName(string bName,int bMoney,int bGoods)
{
name = bName;
money = bMoney;
goods = bGoods;
Console.WriteLine("当前商人的名字是:" + name);
}
public void Buygoods(string otherName)
{
Console.WriteLine("当前" + name + "买了" + otherName + "的东西");
money--;
Console.WriteLine("当前" + name + "还剩" + money+"枚金币");
}
public void Sellgoods()
{
Console.WriteLine();
}
}
#endregion
#region EP10 继承
public class Monster
{
public string name;
public int hp;
public virtual void Attack()
{
//this.hp--;
Console.WriteLine("普通攻击");
}
}
public class Boss : Monster
{
public override void Attack()
{
base.Attack();
Console.WriteLine("放技能");
}
}
#endregion
#region EP11 属性
public class Trigger
{
private int money;
//ctrl+r -> ctrl+e - 自动生成
//public int Money { get => money; private set => money = value; }
public int Money
{
get { return money; }//可访问但不可修改
set { money = value; }//更改值,但加上 private 就仅在内部
//value 引用客户端代码尝试分配给属性的值
}
private void SendMoney()
{
Money--;
}
}
#endregion
#region EP11 接口
public class Drink //:IAddSuger
{
//public void AddSuger()
//{
//}
}
public class Milk : Drink, IAddSuger//一旦使用接口,必须调用
{
public int cost;//用在数组
public void AddSuger()
{
}
}
public class Coffee : Drink, IAddSuger
{
public void AddSuger()
{
}
}
public class soup : Drink, IAddSuger, IAddSalt
{
public void AddSuger()
{
}
public void AddSalt()
{
}
}
public interface IAddSuger//接口一般是public类型
{
void AddSuger();//接口内部默认public类型,不需要额外添加
}
public interface IAddSalt
{
void AddSalt();
}
#endregion
#region EP12 数据类型
#region EP12 数组
static int[] nums;
static string[] strs;
#endregion
#region EP12 列表
static List<int> numlist;
private static string name;
#endregion
#region EP12 栈
#endregion
#region EP12 队列
#endregion
#region EP12 字典
#endregion
#endregion
#region EP13 静态与非静态
public class Tool1
{
public int toolNum;
public void StartGame()//非静态化
{
}
}
public class Tool2
{
public static int toolNum;
public static void StartGame()//静态化
{
}
}
public class Person
{
}
#endregion
#region EP13 设计模式
public class GameManager
{
public bool gameOver;
//布尔值默认 false
public static GameManager instance{get;set;}//instance是变量名
}
public class GameMusic
{
public GameManager gameManager;
public void PlayMusic()
{
//if(!gameManager.gameOver)
//{
// Console.WriteLine("正常播放游戏音乐");
//}
//else
//{
// Console.WriteLine("退出游戏");
//}
if(!GameManager.instance.gameOver)
{
Console.WriteLine("正常播放游戏音乐");
}
}
}
public class GameController
{
public GameManager gameManager;
public void PerformGameLogic()
{
//if(!gameManager.gameOver)
//{
// Console.WriteLine("正常执行游戏逻辑");
//}
//else
//{
// Console.WriteLine("退出游戏");
//}
if (!GameManager.instance.gameOver)
{
Console.WriteLine("正常执行游戏逻辑");
}
}
}
#endregion
#region Main
static void Main(string[] args)
{
#region EP7 结构体
//Role role1;
//role1.name = "xiaoyan";
//role1.level = 1;
//role1.HP = 10;
//Role role2;
//role2.name = "wanglong";
//role2.level = 2;
//role2.HP = 20;
#endregion
#region EP7 枚举
//OCCUPATION hero1 = OCCUPATION.WARRIOR;
#endregion
#region EP8 面向对象、类
//Bussinessman xiaoming = new Bussinessman();
//xiaoming.Buygoods();
//xiaoming.goods = 10;
//Bussinessman bussinessman1 = new Bussinessman();
//bussinessman1.SetName("小明", 100, 10);
//Bussinessman bussinessman2 = new Bussinessman();
//bussinessman2.SetName("小红", 1000, 100);
//bussinessman1.Buygoods("小红");
#endregion
#region EP10 继承
//Monster monster = new Monster();
//monster.hp = 100;
//Boss boss = new Boss();
//boss.hp = 100;
//monster.Attack();
//boss.Attack();
//Monster monster = new Boss();//父类声明子类实例化 - 行
//monster.Attack();
//Boss boss = new Monster();//子类声明父类实例化 - 不行
//Monster monster = null;//未将对象引用到设置实例 - 错误
//monster.Attack();
#endregion
#region EP11 属性
//Trigger tri = new Trigger();
//Console.WriteLine(tri.Money);
#endregion
#region EP11 接口
//Milk milk = new Milk();
//IAddSuger drink = new Drink();//当父类对应接口时可实现
//drink.AddSuger();
#endregion
#region EP12 数据类型
#region EP12 数组
//nums = new int[] { 1, 3, 5, 7, 9 };//数组长度在初始化时已经固定
//Console.WriteLine(nums[0]);
//Console.WriteLine(nums[3]);
//Console.WriteLine(nums.Length);//Length - 计算数组长度
//nums = new int[2];//会产生覆盖
//nums[0] = 1;
//nums[1] = 2;
//Console.WriteLine(nums[1]);
遍历
//for (int i = 0; i < nums.Length; i++)
//{
// Console.WriteLine(nums[i]);
//}
//strs = new string[] { "s", "JohnKi" };
//Console.WriteLine(strs[1]);
//Milk[] milks = new Milk[]
//{
// new Milk(){cost = 10},
// new Milk()
//};
#endregion
#region EP12 列表
//numlist = new List<int>();
//numlist.Add(3);//Add - 加入到列表的方法名
//numlist.Add(9);
//numlist.Add(7);
//Console.WriteLine(numlist[1]);//3
//Console.WriteLine(numlist.Count);//3
//numlist.Remove(9);//Remove - 移除哪一个元素
//Console.WriteLine(numlist.Count);//2
//numlist.RemoveAt(0);//RemoveAt - 移除哪一个下标的元素
//Console.WriteLine(numlist.Count);//1
//Console.WriteLine(numlist[0]);//7
//numlist.Clear();//Clear - 全屏清空
//Console.WriteLine(numlist.Count);//0
//List<Monster> monstersList = new List<Monster>()//Monster 自定义类型(继承)
//{
// new Monster() { },
// new Monster() { }
//};
//Console.WriteLine(monstersList.Count);//2
#endregion
#region EP12 栈
//新进后出
//Stack<Trigger> triggerStack = new Stack<Trigger>();//Trigger - 来自属性
//triggerStack.Push(new Trigger() { Money = 10});//Push - 压栈
//triggerStack.Push(new Trigger() { Money = 1});
//Console.WriteLine(triggerStack.Count);//2
//Trigger t = triggerStack.Pop();//Pop - 弹出
//Console.WriteLine(t.Money);//1
//Console.WriteLine(triggerStack.Count);//1
#endregion
#region EP12 队列
//Queue<int> numsQueue = new Queue<int>();
//numsQueue.Enqueue(1);//入队
//numsQueue.Enqueue(2);
//Console.WriteLine(numsQueue.Count);//队列长度
//int n = numsQueue.Dequeue();//出队
//Console.WriteLine(n);
#endregion
#region EP12 字典
键、值 - 键值对
键和值可以为任意类型,只需一一对应
//Dictionary<int, Monster> monsterDict1 = new Dictionary<int, Monster>();
键为整形,值为自定义类型Monster
//monsterDict1.Add(1, new Monster() { name = "李白" });//添加字典键和值
//monsterDict1.Add(2, new Monster() { name = "貂蝉" });
//Console.WriteLine(monsterDict1[1].name);
//Dictionary<string, Monster> monsterDict2 = new Dictionary<string, Monster>();
键为字符型,值为自定义类型Monster
//monsterDict2.Add("李白", new Monster() { hp = 100 });
//monsterDict2.Add("貂蝉", new Monster() { hp = 80 });
//Console.WriteLine(monsterDict2["貂蝉"].hp);
遍历
//foreach (var item in monsterDict2)
//{
// Console.WriteLine(item.Key);//键
//}
//foreach (var item in monsterDict2)
//{
// Console.WriteLine(item.Value);//值
//}
//foreach (var item in monsterDict2)
//{
// Console.WriteLine(item.Value.hp);//值
//}
#endregion
#endregion
#region EP13 静态与非静态
当StartGame没有设置为全局变量时,通过创建的变量名调用
//Tool1 tool1 = new Tool1();
//tool1.StartGame();
//Console.WriteLine(tool1.toolNum);
当StartGame设置为全局变量时,直接通过类型名调用
//Tool2.StartGame();
//Console.WriteLine(Tool2.toolNum);
#endregion
#region EP13 设计模式
GameManager gameManager = new GameManager();
GameMusic gameMusic = new GameMusic();
gameMusic.gameManager = gameManager;
GameController gameController = new GameController();
gameController.gameManager = gameManager;
//gameManager.gameOver = false;
GameManager.instance = new GameManager();
GameManager.instance.gameOver = false;
gameMusic.PlayMusic();
gameController.PerformGameLogic();
#endregion
Console.ReadKey();
}
#endregion
}
}