C#进阶篇---异常、类、接口、数组、泛型

异常处理

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _004_异常处理_案例2 {
    class Program {
        static void Main(string[] args)
        {
            int num1 = 0, num2 = 0;
            Console.WriteLine("请输入第一个数字");
            while (true)
            {
                try {
                    num1 = Convert.ToInt32(Console.ReadLine());//在try块中,如果有一行代码发生了异常,那么try块中剩余的代码就不会执行了
                    break;
                } catch {
                    Console.WriteLine("您输入的不是一个整数,请重新输入");
                }
                //break;//把break放在这里的话,不管发布发生异常都会执行,因为try对异常进行了处理
            }
            Console.WriteLine("请输入第二个数字");
            while (true)
            {
                try {
                    num2 = Convert.ToInt32(Console.ReadLine());//在try块中,如果有一行代码发生了异常,那么try块中剩余的代码就不会执行了
                    break;
                } catch {
                    Console.WriteLine("您输入的不是一个整数,请重新输入");
                }
                //break;//把break放在这里的话,不管发布发生异常都会执行,因为try对异常进行了处理
            }
            int sum = num1 + num2;
            Console.WriteLine(sum);
            Console.ReadKey();

        }
    }
}


面向对象

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _005_面向对象编程_类 {
    //在这里我们定义了一个新的类叫做Customer,也可以说定义了一个新的类型叫做Customer
    class Customer
    {
        //数据成员:里面包含了4个字段
        public string name;
        public string address;
        public int age;
        public string buyTime;
        //函数成员:定义了一个方法
        public void Show()
        {
            Console.WriteLine("名字:"+name);
            Console.WriteLine("年龄:"+age);
            Console.WriteLine("地址:"+address);
            Console.WriteLine("购买时间:"+buyTime);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _005_面向对象编程_类 {
    class Program {
        static void Main(string[] args) {
            //1,如果要使用一个类的话 要先引入它所在的命名空间,因为customer位于当前的命名空间下,所以不需要引入,我们可以直接使用Customer
            
            Customer customer1;//在这里我们使用Customer模板,声明了一个变量(对象)
            customer1 = new Customer();//对对象初始化 需要使用new加上类名
            customer1.name = "siki";//我们自己定义的类声明的对象,需要先进行初始化,才能使用 
            Console.WriteLine(customer1.name);
            customer1.Show();//使用对象中的方法
            Console.ReadKey();
        }
    }
}



类的定义和声明

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _006_类的定义和声明 {
    public class Vector3//设置为public 这样才别 的项目中才可以访问
    {
        //我们定义了一个构造函数,那么编译器不会为我们提供构造函数了
        public Vector3()
        {
            Console.WriteLine("Vector3的构造函数被调用了");
        }

        //含参数的构造函数
        public Vector3(float x, float y, float z)
        {
            this.x = x;
            this.y = y;
            this.z = z;
            length = Length();
        }
        public float Length()
        {
            return (float)Math.Sqrt(x * x + y * y + z * z);
        }

        //编程规范上 习惯把所有的字段 设置为private ,只可以在类内部访问,不可以通过对象访问
        private float x, y, z, length;
        private int age;
  
        //age的get\set方法
        public int Age
        {
            set
            {
                //通过set方法 在设置值之前做一些校验的工作  属性的更多好处,需要在写代码过程中体会
                这里value代表在外部对这个属性赋的值
                if (value >= 0)
                {
                    age = value;
                }
            }
        }

        public float getX() {
            return x;
        }

        public float X// 也可以叫做get set方法
        {
            get { return x; }
            set { x = value; }//如果在get 或者 set前面加上 private,表示这个块只能在类内部调用
        }
        //为字段提供set方法,来设置字段的值
        public void SetX(float x)
        {
            //如果我们直接在方法内部访问同名的变量的时候,优先访问最近 (形参)
            //我们可以通过this.表示访问的是类的字段或者方法
            this.x = x;
        }

        public void SetY(float y)
        {
            this.y = y;
        }

        public void SetZ(float z)
        {
            this.z = z;
        }

     
        //定义属性
        public int MyIntProperty {
            set {
                Console.WriteLine("属性中set块被调用");
                Console.WriteLine("在set块中访问value的值是:"+value);
            }
            //如果没有get块,就不能通过属性取值了
            get {
                Console.WriteLine("属性中的get块被调用 ");
                return 100;
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _006_类的定义和声明 {
    class Program {
        static void Main(string[] args) {
            //构造无参对象
            Vector3 v1 = new Vector3();
            //为对象设置值
            v1.SetX(1);
            v1.SetY(1);
            v1.SetZ(1);
            //调用方法Length()获取值,输出
            Console.WriteLine(v1.Length());
            Console.WriteLine();

            //使用属性
            Vector3 v2 = new Vector3(1, 1, 1);
            //对属性设置值,调用set方法
            v2.MyIntProperty = 600;
            //对属性取值
            int temp = v2.MyIntProperty;
            Console.WriteLine(temp);
            Console.WriteLine();

            //对属性设置值,调用set方法
            v1.X = 100;
            //取值并且输出
            Console.WriteLine(v1.X);
            //获取通过这样的方式取值、并且输出
            Console.WriteLine(v1.getX());
            Console.ReadKey();
        }
    }
}


引用赋值

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _008_值类型和引用类型___程序运行时候内存的占用 {
    class Vector3
    {
        public float x, y, z;
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _008_值类型和引用类型___程序运行时候内存的占用 {
    class Program {
        static void Main(string[] args)
        {
            
            Test4();
            Test5 ();
            
            Console.ReadKey();
        }



        static void Test4()
        {
            Vector3 v = new Vector3();
            v.x = 100;
            v.y = 100;
            v.z = 100;

            Vector3 v2 = new Vector3();
            v2.x = 200;
            v2.y = 200;
            v2.z = 200;

            v2 = v;
            v2.x = 300;
            //300 100 100
            Console.WriteLine(v.x);
            Console.WriteLine(v.y);
            Console.WriteLine(v.z);
            //300 100 100
            Console.WriteLine(v2.x);
            Console.WriteLine(v2.y);
            Console.WriteLine(v2.z);
        }

        static void Test5()
        {
            Vector3[] vArray = new Vector3[]{ new Vector3(), new Vector3(), new Vector3() };
            //如果数组是一个值类型的数组,那么数组中直接存储值,如果是一个引用类型的数组(数组中存储的是引用类型),那么数组中存储的是引用(内存地址)
            Vector3 v1 = vArray[0];
            vArray[0].x = 100;
            v1.x = 200;
            Console.WriteLine(vArray[0].x);
        }
    }
}



类的继承

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _009_面向对象编程_继承 {
    class Enemy
    {
        private float hp;
        private float speed;

        public float HP
        {
            get { return hp; }
            set { hp = value; }
        }

        public float Speed
        {
            get { return speed; }
            set { speed = value; }
        }

        public void AI()
        {
            
            Console.WriteLine("这里是Enemy1的公有AI方法");
        }

        
        public void Move()
        {
            Console.WriteLine("这里是Enemy1的公有Move方法");
        }

   
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _009_面向对象编程_继承 {
    class Boss : Enemy{
      
        
        public void Attack()
        {

            AI();
            Move();
            //hp = 100;  私有的,不允许
            HP = 100;//父类里面公有是数据和函数成员才可以在子类里面访问
            
            Console.WriteLine("Boss正在进行攻击");
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _009_面向对象编程_继承 {
    class Program {
        static void Main(string[] args) {
            //继承:父类里面所有的数据成员和函数成员都会继承到子类里面
            Boss boss = new Boss();
            boss.AI();
            boss.Attack();

            //父类声明的对象,可以使用子类去构造  子类声明的对象不可以使用父类构造
            //enemy虽然使用父类进行了声明,但是使用了子类构造,所以本质上是一个子类类型的,我们可以强制类型转换转换成子类类型
            Enemy enemy;
            enemy = new Boss();
            enemy.AI();
            enemy.Move();
            //强制转为子类,才可以调用子类的对象
            Boss boss1 = (Boss)enemy;
            boss1.Attack();

            //一个对象是什么类型的  主要看它是通过什么构造的  
            //这里enemy使用了父类的构造函数,所以只有父类中的字段和方法, 不能被强制转换成子类
            Enemy enemy1 = new Enemy();
            enemy1.AI();
            enemy1.Move();
            //Boss boss1 =(Boss) enemy;  这是不能转换的
            
            Console.ReadKey();
        }
    }
}



类的继承--关于方法的调用问题

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _009_面向对象编程_继承 {
    class Enemy
    {
       
        public void AI()
        {
            Move();
            Console.WriteLine("这里是Enemy1的公有AI方法");
        }

        public virtual void Move()
        {
            Console.WriteLine("这里是Enemy1的公有Move方法");
        }
      

       
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _009_面向对象编程_继承 {
    class Boss : Enemy{
        //重写 : 原来的方法不存在了
        public override void Move()
        {
            Console.WriteLine("这里是Boss的移动方法");
        }

        public void Attack()
        {
           
            Move();
            Console.WriteLine("Boss正在进行攻击");
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _009_面向对象编程_继承 {
    class Program {
        static void Main(string[] args) {

            //在子类里面重写虚函数之后,不管在哪里调用都是调用重写之后的方法(如果不是父类构造的话)
            Boss boss = new Boss();
            boss.Attack();
            boss.AI();
            Console.WriteLine();

            //如果是父类构造的话,就调用父类的方法
            Enemy enemy = new Enemy();
            enemy.AI();

            Console.ReadKey();
        }
    }
}


隐藏方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _009_面向对象编程_继承 {
    class Enemy
    {
             
        public void Move()
        {
            Console.WriteLine("这里是Enemy1的公有Move方法");
        }
    }
}


using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _009_面向对象编程_继承 {
    class Boss : Enemy{
        public new void Move()//当子类里面有一个签名和父类相同的方法的时候,就会把父类中的方法隐藏
        {//隐藏: 只是把父类中的方法隐藏了,看不到了,实际这个方法还存在
            //访问父类共有!!方法
            base.Move();
            Console.WriteLine("这里是Boss的移动方法");
        }
       
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _009_面向对象编程_继承 {
    class Program {
        static void Main(string[] args) {
            Enemy en = new Enemy();
            en.Move();
            Console.WriteLine();

            //如果使用父类声明对象,那么就会调用父类中的隐藏方法
            Enemy enemy = new Boss();
            enemy.Move();
            Console.WriteLine();

            //强转为子类,则调用子类对象的方法
            Boss bos = (Boss)enemy;
            bos.Move();
            Console.WriteLine();

            //隐藏方法: 如果使用子类声明的对象,调用隐藏方法会调用子类的
            Boss boss = new Boss();
            boss.Move();

            Console.ReadKey();
        }
    }
}



抽象类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _009_面向对象编程_继承 {
     abstract class Bird//一个抽象类 就是一个不完整的模板
    {
        private float speed;

        public void Eat()
        {
            
        }

        public abstract void Fly();
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _009_面向对象编程_继承 {
    class Crow:Bird {//我们继承了一个抽象类的时候,必须去实现抽象方法
        public override void Fly()
        {
            Console.WriteLine("乌鸦在飞行");
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _009_面向对象编程_继承 {
    class Program {
        static void Main(string[] args) {
           
            Crow crow = new Crow();
            crow.Fly();

            Bird bird = new Crow();//我们可以通过抽象类去声明对象 但是不可以去构造
            bird.Fly();
            //强转为子类也可以
            Crow c = (Crow)bird;
            c.Fly();

            Console.ReadKey();
        }
    }
}


密封类、密封方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _010_密封类和密封方法 {
    //sealed class BaseCLass { //这里声明了一个密封类
    class BaseCLass {
        public virtual void Move()
        {
            
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _010_密封类和密封方法 {
    class DerivedClass:BaseCLass {//sealed密封类无法被继承
        public sealed override void Move()//我们可以把重写的方法声明为密封方法,表示该方法不能 被重写 
        {
            base.Move();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _010_密封类和密封方法 {
    class Program {
        static void Main(string[] args) {
        }
    }
}

没有输出结果图~~~~

修饰符

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _011_派生类的构造函数 {
    class BaseClass
    {
        private int x;
        protected int z;
        public BaseClass()
        {
            Console.WriteLine("base class 无参构造函数");
        }

        public BaseClass(int x)
        {
            this.x = x;
            Console.WriteLine("x赋值完成");
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _011_派生类的构造函数 {
    class DerivedClass:BaseClass
    {
        private int y;
        //调用父类中无参的构造函数 当我们没有在子类的构造函数中
        //显示声明调用父类的构造函数,默认会调用父类中的无参构造函数
        public DerivedClass()
        {
            Console.WriteLine("这个是DerivedClass 无参的构造函数");   
        }

        public DerivedClass(int x, int y):base(x)
        {
            this.y = y;
            base.z = 100;
            Console.WriteLine("y赋值完成");
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _011_派生类的构造函数 {
    class ClassXyz
    {
        public static int z;//静态字段

        public static void TestMethod()
        {
            Console.WriteLine("这是静态方法");
        }
        private int x;
        private int y;
    }
}
using System;
using System.Collections.Generic;
using System.Diagnostics.Eventing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _011_派生类的构造函数 {
    class Program {
        static void Main(string[] args) {//public private
            DerivedClass o1 = new DerivedClass();
            DerivedClass o2= new DerivedClass(1,2);
            BaseClass o11 = new BaseClass();

            //z只能有类名来访问,不能用对象来访问
            ClassXyz.z = 100;
            Console.WriteLine(ClassXyz.z);
            ClassXyz.TestMethod();

            Console.ReadKey();
        }
    }
}


接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _012_定义和实现接口 {
    interface IA
    {
        void Method1();
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _012_定义和实现接口 {
    interface IB:IA
    {
        void Method2();
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _012_定义和实现接口 {
    interface IFly
    {
        void Fly();
        void MethodA();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _012_定义和实现接口 {
    class Bird :IFly,IB{
        public void Fly()
        {
            
        }

        public void MethodA()
        {
            
        }

        public void Method1()
        {
            
        }

        public void Method2()
        {
            
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _012_定义和实现接口 {
    class Program {
        static void Main(string[] args) {
        }
    }
}

没有输出~~~


数组

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _014_列表List的创建和使用 {
    class Program {
        static void Main(string[] args) {
            //List<int> scoreList = new List<int>();//创建了一个空的列表 通过类型后面的<>来表示这个列表存储的数据的类型
           // var scoreList = new List<int>();
            var scoreList = new List<int>(){1,2,3};//创建了一个列表,里面的初始值有三个分别为 1 2 3
            //默认Capacity是4--输出4、3
            Console.WriteLine("capacity:"+scoreList.Capacity+" count:"+scoreList.Count);
            //输出4、4
            scoreList.Add(12);//向列表中插入数据
            Console.WriteLine("capacity:" + scoreList.Capacity + " count:" + scoreList.Count);
            //输出8、5---会以4、8、16、32递增
            scoreList.Add(45);
            Console.WriteLine("capacity:" + scoreList.Capacity + " count:" + scoreList.Count);
            Console.WriteLine(scoreList[0]);//根据索引访问数据

            for (int i = 0; i < 20; i++)
            {
                Console.WriteLine("capacity:" + scoreList.Capacity + " count:" + scoreList.Count);
                scoreList.Add( 10 );
            }

           
            Console.ReadKey();
        }
    }
}


操作列表的属性和方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _016_操作列表的属性和方法 {
    class Program {
        static void Main(string[] args) {
            var scoreList = new List<int>();
            scoreList.Add(100);
            scoreList.Add(200);
            scoreList.Add(300);
            scoreList.Add(100);
            foreach (var temp in scoreList)
            {
                Console.Write(temp+" ");
            }
            Console.WriteLine();

            //向指定索引位置插入元素
            scoreList.Insert(3, -1);
            foreach (var temp in scoreList)
            {
                Console.Write(temp + " ");
            }
            Console.WriteLine();
            //移除第一个元素即可
            scoreList.RemoveAt(0);
            foreach (var temp in scoreList)
            {
                Console.Write(temp + " ");
            }
            Console.WriteLine();
            int index = scoreList.IndexOf(400);
            Console.WriteLine(index);
            Console.WriteLine(scoreList.IndexOf(100));
            Console.WriteLine(scoreList.LastIndexOf(100));

            //排序
            scoreList.Sort();
            foreach (var temp in scoreList) {
                Console.Write(temp + " ");
            }
            Console.WriteLine();
            Console.ReadKey();
        }
    }
}


泛型类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _017_泛型_泛型类 {
    class ClassA<T,A> {//T代表一个数据类型,当使用classA进行构造的时候,需要制定T的类型
        private T a;
        private T b;
        private A c;

        public ClassA(T a, T b )
        {
            this.a = a;
            this.b = b;
        }

        public string  GetSum()
        {
            
            return a +""+ b;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _017_泛型_泛型类 {
    class Program {
        static void Main(string[] args) {
      
            var o2 = new ClassA<string,int>("wwww.","taikr.cm");
            Console.WriteLine(o2.GetSum());
            Console.ReadKey();
        }
    }
}



泛型方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _018_泛型方法 {
    class Program {
        public static string GetSum<T,T2,T3,T4>(T a, T b)
        {
            return a + "" + b;
        } 
        static void Main(string[] args) {
            Console.WriteLine(GetSum<int,int,int,int>(12,34));
            Console.WriteLine(GetSum<double,double,double,double>(12.3,34.5));
            Console.WriteLine(GetSum<string,string,string,string>("23r,","wer3l2kj"));
            Console.ReadKey();
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值