Unity 学习,C#基础学习

包含,C#字符串,枚举,集合,数组以及类,属性的设置

Unity 学习,C#基础学习_第1张图片
Snip20170719_1.png

C#基本数据类型代码
一:字符串!
using UnityEngine;
using System.Collections;
public class hehe:NewBehaviourScript8{

}

public class person{
public int age{
set{
age = value;
}
get{
return age;
}

}
string name;
public int c;
public int metp;
//这个具体有啥作用还是不知道
public void Swap (ref int a, ref int b ){
    metp = 9;
    metp = a;
    a = b;
    b = metp;

}
public void Sum (int i ,int j ,out  int sum){

    sum = 0;
    sum = i + j;


}

public person(){
    Debug.Log("初始化方法");
}

}

public class NewBehaviourScript8 : MonoBehaviour {

 public string weidu{
    set{ 
        weidu = value;
    }
    get{ 
        return weidu;
    }

}
public NewBehaviourScript8(){



}
public string haha;
 //这个一开始就会走啊


void Start () {

// name = "hehe";
//如果没有get方法 就打印不出来的
// Debug.Log ("name = " + name);
// stringCreatMacth();
// macthCreat ();
}
//方法的应用
public void macthCreat(){

    person p = new person ();
    int i = 5;
    int j = 12;
    p.Swap (ref i,ref j);
    Debug.Log (i);
    Debug.Log (j);
    Debug.Log ("jiegou dengyu " + p.metp);
    int r;
    p.Sum (5, 13, out r);
    //结果是18 ,自动加一个返回值
    Debug.Log (r);


}
//字符串增删查改,string 是一个引用类型本质是一个chain类型的数组
public void stringCreatMacth(){
    string s1 = "123";
    string s2 = "123";
    if (s1 == s2) {
        Debug.Log ("文本一样");
    
    } else {
    
        Debug.Log ("buyiyang");

    }
    string name = "lanou";
    for(int i = 0;i

//等价上面的那个字符串
char[] n = new char[]{ 'l', 'a', 'n', 'o', 'u' };
string o = "wudiyeshiyizhongjimo";
string wu = "zhong";
if (o.Contains (wu)) {
Debug.Log ("baohan");

    } else {
        Debug.Log ("bu baohan");
    
    }
    //获取下标
    int c = wu.IndexOf ("g");
    Debug.Log ("xiabiao = "+ c);
    wu.Remove (2);
    Debug.Log ("shanchu = "+ wu);
    //分割子串
    char[] en = {'w','u'};
    string[] sub = o.Split (en,1);
    Debug.Log ("sub = " + sub);
    for(int i = 0;i hehe = new List();

    //      Dictionary mydictionary = new Dictionary();
    //      mydictionary.Add(0,"0");
    //
    //      bool i = mydictionary.ContainsKey (0);
    //      int value;
    List Mylist = new List ();
    //创建一个数组
    string[] temArr = { "ha", "heh", "gaga", "asdas" };
    //已集合为参数初始化list;
    List testlist = new List (temArr);
    Mylist.Add ("wudi");
    Mylist.AddRange (temArr);
    Mylist.Insert (0, "tl");
    for(int i= 0 ;i < Mylist.Count;i++  ){

        Debug.Log (Mylist [i]);

    }
    //删除
    Mylist.Remove ("hunter");
    //      Mylist.RemoveAll (0);
    //清空
    Mylist.Clear();
    //      Mylist.RemoveAll();
    //查找相应的元素
    if (Mylist.Contains ("hehe")) {


    } else {


    }
    // 排序
    Mylist.Sort ();


}

void dictionarycreat(){
    //字典初始化。

    Dictionary film = new Dictionary();
    film.Add ("weixiaobao", "lutingji");
    film.Add ("zhangxueji", "qitiandulongji");
    film.Add ("yangguo", "shendiaoxialv");
    film.Add ("linghuchong", "xiaoaojianghu");
    Debug.Log ("xianzaijihezhongyoujige" + film.Count);
    //通过KEYVALUEPAIR遍历字典;
    foreach(KeyValuePairkvp in film){
        Debug.Log ("dictionary ="+ kvp);
    }
    // 通过内容KEY 来获取对应的Key
    if(!film.ContainsKey("tl")){
        film.Add ("tl", "wudi");
    }
    // 取Key值
    Dictionary.KeyCollection keys = film.Keys;
    foreach(string s in keys){
        Debug.Log (s);
        if( s =="tl"){
            Debug.Log ("tlzuishai");

        }
    }
    //取Value值
    Dictionary.ValueCollection values = film.Values;

    foreach(string v in values){
        Debug.Log ("painliValuse");

    }
    //直接找values 
    foreach(string strname in film.Values){
        Debug.Log (strname);
    }
    string myfilm = film ["tl"];
    string objfilm = string.Empty;
    //获取键对应值的TryGetValue方法    
    if (film.TryGetValue ("tl", out objfilm)) {

        Debug.Log("主角为段誉的电影是{0}"+ objfilm);
    } else {

        Debug.Log ("没有这个名字");

    }


}

}
三:抽象类虚方法
using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {

//抽象类 abstract,关键字。。
public abstract class bloo {
    public  abstract void Play ();
    public void run(){
        Debug.Log ("球开始跑了");
    
    }

}

public class basketball : bloo{
    public override void Play ()
    {
        Debug.Log ("打篮球");
    }

}
public class foolball:bloo{
    public override void Play ()
    {
        Debug.Log ("踢足球");
    }


}

public class volleyball :bloo{
    public override void Play ()
    {
        Debug.Log ("打排球");
        
    }


}
// Use this for initialization
void Start () {
    //
    bloo b1 = new basketball ();
    bloo b2 = new foolball ();
    bloo b3 = new volleyball ();
    b1.Play ();
    b2.Play ();
    b3.Play ();
    b1.run ();
    //直接使用类名 调用,static.
    A.i = 10;


}
public   class A{
    public static int i;
    //静态区,程序运行过程中,是一直存在的
    //在静态类中,只能保护静态的成员,字段,属性,方法 都要是静态的,
    //静态构造方法 前是不允许加修饰符的。静态构造方法可以存在于静态类中,也可以在普通类中
    public  static int c ;
    //构造方法
      static  A(){


    }
    public static void hehe (){


    }

}
public class b{
    //可以存在普通的类中,静态构造方法,会在访问A的静态
    static b(){


    }
}
//单利类,保证在程序运行期间,一个了类存在一个对象。。
public class player{
    //单利三步1,构造私有化方法,不让对象随意的创造对象。
    private player () {

    }
    //2,我们需要在类的内部提供一个静态实例。
    private static  player _instance;

    //3.提供获取实例的接口
    public static  player  GetInstance(){
        if(_instance == null){
            _instance = new player ();

        }
        return _instance;
    }
    // 成员变量
    public string name;
    public int level;
    public int hp;
    public int maxHp;

}
public class Bag{
    public void UseHp( ){
        player p = player.GetInstance ();
        p.hp += 10;
        p.hp = p.hp > p.maxHp ? p.maxHp : p.hp;
    
    }

}
// Update is called once per frame
void Update () {

// player p = new player ();
// p.name = "老王";
// p.level = 1;
// p.hp = 2;
// p.maxHp = 100;
//
// Bag b = new Bag ();
// b.UseHp (p);
//单利
player p = player.GetInstance();
p.name = "laowang";
//使用父类调用父类对象
//使用父类调用父类对象
Super super = new Super ();
super.A ();
super.B ();

    //使用子类类型调用子类对象
    //使用子类类型调用子类对象

    Sub sub = new Sub ();
    sub.A ();
    sub.B ();

    //使用父类类型 引用子类对象
    Super c = new Sub();
    c.A ();//调用父类中定义的方法
    c.B();//调用了子类的方法
    //多态 - 使用父类类型,调用子类中实现的方法;
    Apple a = new Apple();
    a.Eat ();
    Orangr o = new Orangr ();
    o.Eat ();
}

//虚方法,在子类中重新实现父类的方法。。
//抽闲方法和虚方法,1,抽象方法必须在抽闲类中,2,抽象方法不能再父类中现实。3.抽象方法在非抽象子类中必须实现。
public class Super{
    //virtual 使用虚方法 关键字
    public virtual string Name{
        get{ 
            return "laowang";
        }

    }
    public void A(){
    
        Debug.Log ("super");
    }
    public virtual void B (){
        Debug.Log ("super vitrual");
    
    }

}
public class Sub:Super{

    public new void A (){
    
        Debug.Log ("sub");
    }
    //重写父类方法,override  要使用多态的话 必须使用override 这个关键字
    public override void B (){

        Debug.Log ("sub override");

    }

}
//抽象方法只能出现在抽象类中
public abstract class Food{
    //方法不能够实现。。
    public abstract void Eat ();
}
public class Apple:Food{
    public override void Eat(){
    
        Debug.Log ("hp+10");

    }

}
public class Orangr :Food {
    public override void Eat(){

        Debug.Log ("HH+10");

    }

}

}
四:接口 堆栈
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class mode : MonoBehaviour {
public interface Area {
void Eat ();

}
public  interface IVolume {
    void Run ();


}
public  class Ball : Area,IVolume {
    //一旦某个类实现了接口,就必须实现接口的定义的全部方法。
    public void Eat(){
        
    }

    public void Run(){


    }
}
// Use this for initialization
void Start () {
    //接口
    Area a = new Ball ();
    IVolume i = new Ball ();
    a.Eat ();
    i.Run ();
    //栈 先进先出
    Stack  s = new Stack  ();
    int count = s.Count;
    s.Clear ();
    bool b = s.Contains ("老王");
    s.Push ("老王");
    s.Push ("老张");
    s.Push ("小明");

    string s1 = s.Pop ();
    string s2 = s.Pop ();
    string s3 = s.Pop ();
    //后进先出的原则
    Queue  q = new Queue ();
    q.Clear ();
    int coun = q.Count;
    bool p = q.Contains("老王");
    q.Enqueue ("老王");
    string s4 = q.Dequeue();

}

// Update is called once per frame
void Update () {
    Array a = new Array ();
    int c;
    a.Log ();

}
//封装数组
public class Array{
    //索引
    public int this [int  index] {
        set {
            _arr [index] = value;
        }
    }

    public int Count{
        get{ 
            return _count;
        }

    }
    //添加元素,
    public void Add(int value){
        //赋值_arr;
        _arr [_count] = value;

        _count++;
    
    }
    //初始化数组
    public Array(){
        
        _arr = new int[100];
    }
    //打印方法
    public void Log(){
        string str = "当前数组中包含" + Count + "个元素 (";
    
        for (int i = 0; i < Count; i++) {
            str += _arr [i];
            if(i

// string str = "当前数组中有" + arr.

}

}

using UnityEngine;
using System.Collections;
public class person{
string _name;
public string name
{
get;
set;
}

// public string UserName { set; get; }

public  void  eat(){
    Debug.Log ("ear fool");

}
public void eat(int i){
    Debug.Log ("吃了" + i);

}
public void eat (float f){
    Debug.Log("chil "+ f);

}

}
public class reftest : MonoBehaviour {

// Use this for initialization
void Start () {
    person p = new person ();
    p.eat (1);
    p.eat ();
    p.eat (10f);
    p.name = "ahha";


}
public void chongzai(){

    print ("chongzai");

}
private void chongzai(int i){

    Debug.Log ("int = "+ i);
}
private void  mathC(){
    int i = 5;
    int j = 13;
    Swap (ref i,ref j);

// Debug.Log ("i = "+ i);
// Debug.Log ("i = "+ j);
int r;
Sum (i,j, out r);
Debug.Log (r);

}
//ref 值类型 ,引用类型 ,然并卵
public void Swap (ref int a,ref int b ){
    int temp = a;
    a = b;
    b = temp;
}
//让一个方法返回多个值,可以用来输出参数来出来,在前面在Out
//官方定义! 自己觉得就是 多了一个参数可以不用写方法返回值。
public void Sum(int i ,int j ,out int sum){

    sum = 0;
    sum = i + j;

}
public  void ArrayListsample(){
    
    ArrayList arrsample =  new ArrayList();
    arrsample.Add (100);
    arrsample.Add (200);
    arrsample.Add (300);
    //获取下标!
    int a =  arrsample.IndexOf (200);

// arrsample.InsertRange(1)
//从1 开始找
a = arrsample.IndexOf(200,1);
//排序
a = arrsample.BinarySearch(200);
arrsample.Sort();// 排序
a = arrsample.BinarySearch (200);
arrsample.Reverse ();//进行翻转!排序
//循环
foreach(int val in arrsample){

        Debug.Log ("数组函数 " + val);

    }


}
public void hashtablesmaple(){
    print("****Queue : 先进先出 ");
    Queue queue = new Queue();
    queue.Enqueue (200);
    queue.Enqueue (100);
    queue.Enqueue (100);
    //偷看
    Debug.Log ("peek = "+ queue.Peek ());

    foreach(int Val in queue){
        Debug.Log ("val = "+ Val);
    }
    while (queue.Count > 0) {
    
        Debug.Log ("queue = "+queue.Dequeue());
    
    }

}
//字典
public void SortedListsmaole(){
    SortedList sortedList = new SortedList ();
    sortedList.Add ("one", 100);
    sortedList.Add ("tow",200);
    sortedList.Add ("therr",300);
    sortedList.Add ("fore",400);
    //他 有一个结构体是来读取的
    foreach(DictionaryEntry  key in sortedList){

// Debug.Log ("zidian"+key.Key,key.Value);
}
//获取全部的keys
ICollection keys = sortedList.Keys;

// ICollection key = sortedList.Keys;
foreach( string key in keys){
Debug.Log(keys);

    }



}
//堆栈
public void Stacksmaole(){
    Stack stack = new Stack ();
    stack.Push (100);
    stack.Push (200);
    stack.Push (300);

    Debug.Log ("***数据个数="+stack.Count);

    foreach(int key in stack){
        Debug.Log ("获取里面的东西"+ key);

    }
    //弹出!
    while(stack.Count>0){

// Debug.Log (stack.Pop);
}
}
// Update is called once per frame
void Update () {

}

}

你可能感兴趣的:(Unity 学习,C#基础学习)