C#Lesson06

2021-03-17 19:42发布

c#基础 第六天

 enum Color
    {
        Black,
        Red,
        White,
        Yelow,
        Pink,
        Blue,
        Purple
    }
    //class是关键字,用来定义一个类型
    class Car
    {
        //特征:颜色,价格,品牌
        public Color color;
        public double price;
        public string brand;

        //功能:刹车,行驶,开门,加油
        //功能用方法来表示
    }
    class Computer
    {
        //特征:颜色,大小,品牌,价格
        //功能:打字,看电影,聊天,玩游戏
    }
    class Cup
    {
        //特征:颜色,大小,品牌,容水量
        //功能:盛水,保暖,
    }

    class Tool
    {
        //方法定义的语法格式:
        //public返回值类型 方法名(形参列表 {方法体}
        //方法是有特殊功能的代码块,方法的名字就是该代码块的名字,方法是一种代码重用机制
        //第一种方法类型:有参有返回值
        //如果有返回值,方法体里面一定要有return关键字,并且其后面只能跟一个值
        //返回值的类型要和方法名前面的类型保持一致
        //当代码执行了return以后,程序会跳出方法体,回到调用该方法的位置

        public int Sum_Int(int a,int b)
        {
            int sum = a + b;
            return sum;
        }
        public int SumValue(int n)
        {
            int sum = 0;
            for (int i = 1; i <= n; i++)
            {
                 sum += i;
            }
            return sum;
        }
        public int Date(int d,int m,int y)
        {
            int sum = 0;
            //用数组写
            switch (m)
            {
                case 1:
                    sum = d;
                    break;
                case 2:
                    sum = d + 31;
                    break;
                case 3:
                    sum = d + 31 + 28;
                    break;
                case 4:
                    sum = d + 31 + 28+31;
                    break;
                case 5:
                    sum = d + 31 + 28+31+30;
                    break;
                case 6:
                    sum = d + 31 + 28 + 31 + 30+31;
                    break;
                case 7:
                    sum = d + 31 + 28 + 31 + 30+31+30;
                    break;
                case 8:
                    sum = d + 31 + 28 + 31 + 30 + 31 + 30+31;
                    break;
                case 9:
                    sum = d + 31 + 28 + 31 + 30 + 31 + 30+31+31;
                    break;
                case 10:
                    sum = d + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
                    break;
                case 11:
                    sum = d + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
                    break;
                case 12:
                    sum = d + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31+30+31+30;
                    break;
                default:
                    break;
            }
            if (y%400==0||y%4==0&&y%100!=0)
            {
                sum += 1;
            }
           
            return sum;

            return d;
        }

        //第二种方法类型:有参无返回值
        //如果方法没有返回值,方法名前面的返回值类型使用void代替,方法体里面的return后面不加任何值;
        //如果return在方法体里面的最后一行可以省略,当程序把方法体里面的代码执行完以后,会自动跳到调用该方法的位置
        public void PrintIntArray(int[] x)
        {
            foreach (var item in x)
            {
                Console.WriteLine(item);
            }
        }
        public void Total(float a,float b)
        {
            float sum = a + b;
            float c = a - b;
            float d = a * b;
            float e = a / b;
            Console.WriteLine("和是:{0:f2}",sum);
        }
        //第三种方法类型:无参无返回值
        public void HelloWorld()
        {
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("helloworld");
            }
        }

       


        //第四种方法:无参有返回值方法
        public  double GetPI()
        {
            return 3.1415926;
        }

    }

    class Student
    {
        private double money;
        public void UseMoney(double v)
        {
            money -= v;
        }
        //set方法,用来给类里面的字段赋值
        public void SetMoney(double m)
        {
            money = m;
        }
        //get方法,用来得到字段的值
        public double GetMoney()
        {
            return money;
        }
    }
   
   
    class Program
    {
        static void Main(string[] args)
        {
            #region 类和对象
            //如果是数值类型,默认值是0,如果是字符或者字符串,默认值为空
            //Car qiche = new Car();
            //qiche.brand = "五菱";
            //qiche.price = 3.5;
            //qiche.color = Color.White;
            //Console.WriteLine(qiche.color);
            //方法是类里面的成员,故需要创建方法所在的类的对象,通过对象使用点运算符调用
            Tool t = new Tool();
            //调用方法要注意的事项:
            //1.如果方法有返回值,就定义一个和返回值类型一样的变量,接收返回值
            //2.调用方法时传进来的数据都是有具体数值和实际意义的,故调用时传进来的数据称为实参
            //3.实参的类型以及个数要和形参一一对应
            //int sum = t.SumValue(100);
            //Console.WriteLine(sum);
            //1.调用方法时先看方法的返回值类型,如果是void,表示该方法没有返回值,就不用在定义变量接收该返回值, 如果有返回值类型,就定义一个和返回值类型一样的变量接收返回值
            //2.再看方法名后面的参数,实参类型和个数要和形参一一对应.如果没有返回值,方法名后面的小括号仍然要有
            //int[] arr = { 1, 2, 3, 4, 5 };
            //int[] brr = { 1, 2, 3, 4, 5 };
            //t.PrintIntArray(arr);
            //t.Total(1.2f,4.5f);
            t.HelloWorld();
            double p = t.GetPI();
            Console.Read();
            Console.ReadLine();
            Console.WriteLine();


            Console.ReadKey();
            #endregion
            #region 方法在类里面的使用
            Student s1 = new Student();
            Student s2 = new Student();
            s1.SetMoney(100);
            s2.SetMoney(200);
            s1.UseMoney(10);
            s2.UseMoney(50);
            Console.WriteLine(s1.GetMoney());
            Console.WriteLine(s2.GetMoney());
            #endregion
        }
    }