c#

1.

  对于字符串类型,string s=1;是错的,赋值左右必须同类型,这时可以string s=1+"";则可以输出s=1;  Debug.Log("9+2="+(9 + 2));则输出:9+2=11;好好揣摩

2.

  object是Gameobject的父类,理解该表达式:Gameobject a=new Gameobject;

3.erson.GetFive());
        {

  【1】数组Arraylist a=new Arraylist();其中a.count是该动态数组的长度;
   【2x,y,z,b],用该方法后输出【b,z,y,x],不可以        printarray(a.Reverse());

4.【1】函数】

        (1)插入API:a.Insert(x,y)->在x前插入y           若定义ArrayList b=new ArrayList() ,b.Add("q"),b.Add("z"),b.InsertRange(1,b)->则在1前插入qz

         (2)删除API:a.remove(x),则把该数组中的x删除      a.RemoveAt(1),则把数组第一个元素删除      a.RemoveRange(1,2),则把数组第一个开始的2个数组元素删除(后面保)

          (3)查找API:        print(a.IndexOf("dog"));->查找“dog”在数组中的位置

           (4)倒置API:  a.Reverse();printarray(a)->比如a=[的每个单词首写字母大写

   【2】在函数定义前加public,则该函数是公共函数,可在外部使用

5. 处理Rigidbody时要用FixUpdate,给物体加作用力时更是如此

6.声明常量的方法:const int a=3;此后a不能被赋值了

7.系统有很多定义好了的方法(函数),然后有很多功能,比如void OnGUI()是Unity自定义的,你若写成void onGUI则无此功能

8.character controller同时也是一个  collider(碰撞器)

9.Mesh Renderer(网格渲染)

10.C#的引用类型:分为object和string,Object o = new object();<实例化>,o.Tostring(转化为字符串)

       string at= @"\\\u0066";//有@则把“”里的东西全部当成字符输出
            Console.WriteLine(at);
            string noat = "\\\\u0066";//第一个\都是转译其后面的内容

       string s2 = "jike";                   
        s2 += "xue";相当于可以给字符串加字符串  ,但比较消耗内存,      StringBuilder builder=new StringBuilder();则方便许多,可以实现同等功能
                                                                                                                       builder.Append("jike");
                                                                                                                       builder.Append("xueyuan");

11.转译符 string at= @"\\\u0066";//有@则把“”里的东西全部当成字符输出
            Console.WriteLine(at);
            string noat = "\\\\u0066";//第一个\转译第二个\,第三个\转译、

12.静态的方法通过类来调用的而不是通过实例化的变量来调用

13.class Person{  int age              public    Person(int maAge){      age = maAge;      }                },Person person = new Person(6);6会传到maAge,使age=6;

         public int GetAge(){  return age;},Console.WriteLine(person.GetAge());会输出6

          public static int GetFive()和 Console.WriteLine(Person.GetFive())说明12的意思
            return 5;

13。接下来是属性的格式 :可直接给属性赋值

public int Age
        {
            get
            {
                return age + 10;
            }
            set
            {
                 age = value - 10;    //value是Age的值
            }
        }
 person.Age = 10;

  14.class与interface
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
          Person person = new Person(6);
            Console.WriteLine(person.GetAge());
            Console.WriteLine(Person.GetFive());
            Console.WriteLine(person.Age);
            person.Age = 10;
            Console.WriteLine(person.GetAge());
           
            //Console.WriteLine(person.Age);
            Console.ReadLine();
        }
        
        
    }
    class Person:Man,  Isuper//类只能继承一个类。但是可以继承多个接口(interface),且里面的方法都要实现
    {
        int age;
    
        public int Age
        {
            get
            {
                return age + 10;
            }
            set
            {
                 age = value - 10;    //value是Age的值
            }
        }
         public    Person(int maAge)
        {
            age = maAge;

        }
        public int GetAge()
        {
            //string s = "5";
            //return s;
            return age;
        }
        public static int GetFive()
        {
            return 5;
        }
        public int GetSuper()
        {
            return age + 100;
        }
         
        //Person.GetAbstract();
        //Man.GetAbstract();
        //public override int GetAbstract()
        //{
        //    return 50;
        //    //throw new NotImplementedException();
        //}
    }
    interface Isuper//只能有方法、属性、索引
    {
        //string s;该句有错误,接口不能包含字段(field),即不能定义任何变量
       
         int GetSuper();
        //{
        //    return ag
        //}
        
    }
    abstract class Man//抽象类除了不能被继承外可以包含抽象或非抽象的方法和变量,且里面的方法和变量可以被继承
    {
        public String name;
        public String GetName()
        {
            return name;
        }
        public abstract int GetAbstract();
    }
}

15.类的转化(1)
namespace C岔的类型转换
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 10;
            long l = i;
            double a = 10.05;
            int b = (int)a;//大——>小,显示转换
            C1 c1 = new C2();//类的隐式转换,子类——>父类
            C1 c2 = new C1();//(基类)父类——>实例化

            // C2 c3 = (C2)c2;//把基类实例化后才能转化为子类
            try
            {
                C2 c3 = (C2)c2;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            Console.WriteLine(c1 is C1);
            Console.WriteLine(c2 is C1);
            C2 c22 = c2 as C2;//消耗资源低,若不能则返回空值,as只能用于引用类型和可以为null的空类型
            Console.WriteLine(c22 == null);
            int iAs = "5" as int;
            Console.ReadLine();
        }
        class C1
        {
            //public static implicit operator C1(C2 v)
            //{
            //    throw new NotImplementedException();
            //}
        }
        class C2:C1
        { }
    }
}
(2)主要讲了字符串转化为int类的方法 奋斗
 string sFromI = i.ToString();
            //int iFromS = Convert.ToInt32("jik");//不能将英文字符转化
            int iFromS = Convert.ToInt32("100");
            int iFromS2 = Int32.Parse("102");//只能将数字字符串转化为等效的数字,而不能是其他字符串如字母
                
            Console.WriteLine(iFromS+iFromS2);
            int iFromS3;
            bool succeed = Int32.TryParse("ji", out iFromS3);//只能将数字字符串转化为等效的数字,而不能是其他字符串如字母
            Console.WriteLine(succeed );
            Console.WriteLine(iFromS3);
            Console.ReadLine();

16.逻辑语句
namespace 逻辑语句
{
    class Program
    {
        static void Main(string[] args)
        {
            #region condintion

            if (true|false)//||同C语言为或逻辑,若为|,则要运算全部,而非第一个对则不在再判断
                Console.WriteLine("false");
            int i, j = 10;
            for (i = 1, Console.WriteLine("start"); i < j; i++, j--,Console.WriteLine("i={0},j={1}",i,j))//让i=初始值,有点类似printf("%d",i)
                //但这里必须从零开始

            {
                Console.WriteLine("body of the loop");
            }
            //for (;;)
            //    Console.WriteLine("saf");
            List<int> listInt = new List<int>() { 1, 2, 3 };
            foreach(int intInlist in listInt)
            {
                Console.WriteLine(intInlist);
            }
            Console.ReadLine();
            #endregion

        }
    }
}

17.固定长度数组
namespace 集合类型
{
    class Program
    {
        static void Main(string[] args)
        {
            Int32[] number = new int[5];//实例化定义一维数组
            int[,] numbers = new int[2, 3];//实例化定义二维数组
            string[] name = new String[2] { "aas", "asf" };//每个单元都可存储字符串不像char类
            Console.WriteLine(name[1]);
            byte[][] scores = new byte[3][];
            for (int i = 0; i < scores.Length; i++)
            {
                scores[i] = new byte[i + 3];
            }
            for (int i = 0; i < scores.Length; i++)
                Console.WriteLine("length of row{0} is{1}", i, scores[i].Length);
            int[] a = { 1, 2, 3, 4 };//最简单的一维数组定义法
            int[,] b = { { 1, 2 }, { 3, 4 }, { 5, 6 } };//二维数组的定义法
            int[][] c = { new int[] { 1, 2 }, new int[] { 3, 4 } };//储存数组的数组
            foreach(int i in b)//把数组b的每个值赋给i然后输出,实现循环赋值 输出
            {
                Console.WriteLine(i);
            }
      
            Console.ReadLine();
        }

    } 
    
}


18.集合类型

namespace Array和list
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList al = new ArrayList();//该类数组可以存储任何的数据,大杂烩式的存储,这使用起来危险,降低了代码质量
            al.Add("sf");
            al.Remove("sf");
            al.Add("gsgsaf");
            al.Add(13);
                
            foreach(var i in al)
            {
                Console.WriteLine(i);
            }
            //List<ValueType> vList = new List<ValueType>();
            List<ValueType> vList = new List<ValueType>() { 1, 2 ,1.2f};
            Console.WriteLine(vList[2]);
            List<int> k = new List<int>();
            k.Add(500);
            k.AddRange(new int[] { 10, 20, 30 });
            k.IndexOf(20);
            k.Insert(1, 8);//在k[1]插入8,把本在k[1位置(包括其后)的元素整体后移
            k.InsertRange(2, new int[] { 1, 2, 3 });//在k[2]的位置插入该数组,把本在k[2]位置(包括其后)的元素整体后移
            k.Insert(1,10000);
            foreach(int e in k)
            {
                Console.WriteLine(e);
            }
            Hashtable ht = new Hashtable();//哈希表,索引由自己定义,可以数字也可以字符,而值也没有规定非得哪种类型,比较不安全,易出错
            ht.Add("1", "jike");
            ht.Add("seconds", 23);
            Console.WriteLine(ht["2"]);//无效的键值(未定义该键)返回null,什么也不输出
            Console.WriteLine(ht["seconds"]);
            Dictionary<string, string> d = new Dictionary<string, string>();
            d.Add("first", "jike");
            //Console.WriteLine(d["aa"]); //无效的键值(未定义该键(),会报错
            SortedList<int, int> s1 = new SortedList<int, int>();
            s1.Add(1, 100);
            s1.Add(30, 300);
            s1.Add(10, 1000);
            foreach(var sle in s1)
            {
                Console.WriteLine(sle.Key);//按顺序输出s1键值
                Console.WriteLine(sle.Value);//按键值顺序输出s1的值
            }
            //stack--先进后出 ,queue--先进先出
           // d.Add(10, 10);




            //vList ={ 1,2};

            Console.ReadLine();
        }
    }
}


19.virtual类型的方法可以被override(重写)

20.封装

using System;
using 封装Another;//必须先引用该命名空间,否则否则不能使用该命名空间下的类
using AnotherAssembly;
namespace 封装//一个程序集里可以有多个命名空间,一个命名空间也可以在多个程序集里,AnotherAssembly是一个程序集,一个项目对应一个程序集
    //internal作为修饰可使该对象在同一个项目的其他命名空间里使用
{
    class Program
    {
        static void Main(string[] args)
        {
            Person person = new Person();
            Console.WriteLine(person.GetAge());
            AnotherNamespace anc = new AnotherNamespace();
            Console.WriteLine(anc.internalString);
            Console.ReadLine();
        }
    }
    class Person
    {
        public int age;
        public int Age
        {
            get
            {
                return age;
            }
            set
            {
                age = value;
            }
        }
        public int GetAge()
        {
            if (CheckAge())
                return age;
            else
                return -1;
        }
        private bool CheckAge()//即使是private,在本类中的其他方法都可以调用,但非本类都不能调用
        {
            if(age<=0)
            {
                return false;
            }
            return true;
        }
    }
}


22.继承.

namespace 继承
{
    class Program
    {
        static void Main(string[] args)
        {
            Dog dog = new Dog();
            dog.Age = 106;
            Console.WriteLine(dog.Age);
            dog.Bite();//直接使用它即可
            Animal animal = new Animal();
            animal.Bite();
            //Console.WriteLine(dog.Bite());不能这样写,你若要输出bite方法中的东西
            dog.GetAge();
            dog.BiteMan();
            Console.ReadLine();
                
        }
    }
    /*sealed*/  class Animal//若是sealed的类则不能被继承
    {
        public int Age
        {
            get
            {
                return 2;
            }
            set
            {
                //return 2;
            }
        }
       public Animal()//构造函数,其不属于任何类型
        {
            Console.WriteLine("Animal constructor");
        }
        public virtual void Bite()
        {
            Console.WriteLine("animal bite");
        }
        public virtual void GetAge()//在父类里声明的virtual方法既可以在子类中重写也可以不重写
        {
            Console.WriteLine(Age);
        }
        public  void BiteMan()
        {
            Console.WriteLine("Bite man");
        }
    }
    class Dog:Animal
    {
        public override void Bite()//重写就是在不同类中可以使用同一种方法,但是不同类里该方法里的东西可以不同
        {
            //base.Bite();
            Console.WriteLine("dog bite");
        }
        public new void BiteMan() //没有标记为abstract或virtual的方法不能被重写,用new可以把父类中同名的方法隐藏
        { }
        public  Dog()
        {
            Console.WriteLine("Dog contructor");
        }
        //Dog dog = new Dog();Dog已经实例化了就不能在任何类里再实例化,即使没有实例化也不能用dog访问父类
        //Animal dogs = new Animal();同理
        
        
           
        

    }
}






   













  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值