C#基础day01

来源:互联网 发布:微信淘宝返利骗局揭秘 编辑:程序博客网 时间:2024/05/21 14:10

01基础知识练习

Person类

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace _01基础知识练习{    public class Person    {        private string _name;        public string Name        {            get            {                return _name;            }            set            {                _name = value;            }        }        //ctrl + R + E        private int _age;        public int Age        {            get { return _age; }            set { _age = value; }        }        //自动属性        public string Gender        {            get;            set;        }        public virtual void SayHi()        {            Console.WriteLine("大家好!,我叫{0}", Name);        }    }}

Employee类

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace _01基础知识练习{    public class Employee : Person    {        public double Salary        {            get;            set;        }        public override void SayHi()        {            Console.WriteLine("大家好,我的名字叫{0},工资是:{1}", Name, Salary);        }    }}

测试代码

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace _01基础知识练习{    class Program    {        static void Main(string[] args)        {            #region 交换两个变量            //int num1 = 10;            //int num2 = 20;            ////普通的变量交换。            ////int tmp = num1;            ////num1 = num2;            ////num2 = tmp;            ////不声明第三个变量,实现两个变量的交换。            //num1 = num1 + num2;            //num2 = num1 - num2;            //num1 = num1 - num2;            //Console.WriteLine("num1:{0},num2:{1}", num1, num2);            //Console.ReadKey();            #endregion            #region 求两个数的最大值。            //Ctrl + K + C //注释代码            //Ctrl + K + U //取消代码注释            //int max = GetMaxVal(100, 200);            //Console.WriteLine(max);            //Console.ReadKey();            #endregion            #region 求1-100之间所有奇数的和            //int sum = GetSum(100);            //Console.WriteLine(sum);            //Console.ReadKey();            #endregion            #region 求数组中的最大值            //int[] arrInt = new int[] { 200, 120, 90, 180, 110 };            ////shift + alt + F10            ////ctrl + .            //int max = GetMaxFromArray(arrInt);            //Console.WriteLine(max);            //Console.ReadKey();            #endregion            #region 日期转换            //string date = "2012年6月4日";            //date = ConvertDateToCn(date);            //Console.WriteLine(date);            //Console.ReadKey();            #endregion            #region 测试             Employee emp = new Employee();            emp.Name = "杨中科";            emp.Age = 18;            emp.Gender = "男";            emp.Salary = 1000000;            emp.SayHi();            Console.ReadKey();            Console.WriteLine();            #endregion        }        private static string ConvertDateToCn(string date)        {            //1.得到字符串的字符数组            char[] ch = date.ToCharArray();            for (int i = 0; i < ch.Length; i++)            {                #region switch                switch (ch[i])                {                    case '0':                        ch[i] = '零';                        break;                    case '1':                        ch[i] = '一';                        break;                    case '2':                        ch[i] = '二';                        break;                    case '3':                        ch[i] = '三';                        break;                    case '4':                        ch[i] = '四';                        break;                    case '5':                        ch[i] = '五';                        break;                    case '6':                        ch[i] = '六';                        break;                    case '7':                        ch[i] = '七';                        break;                    case '8':                        ch[i] = '八';                        break;                    case '9':                        ch[i] = '九';                        break;                }                #endregion            }            //根据char数组构建一个新的一个字符串            return new string(ch);        }        /// <summary>        /// 求数组的最大值。xxxxxxxx        /// </summary>        /// <param name="arrInt"></param>        /// <returns></returns>        private static int GetMaxFromArray(int[] arrInt)        {            int max = arrInt[0]; //假设数组中第0个元素是最大值。            for (int i = 1; i < arrInt.Length; i++)            {                if (arrInt[i] > max)                {                    max = arrInt[i];                }            }            return max;        }        //定义一个方法,用来计算两个数的最大值。        //方法职责单一,不要在一个方法中写过多的其他代码。        static int GetMaxVal(int n1, int n2)        {            return n1 > n2 ? n1 : n2;            //int max = n1 > n2 ? n1 : n2;            //Console.WriteLine(max);        }        //计算1-targetNum的奇数值。        static int GetSum(int targetNum)        {            int sum = 0;            for (int i = 1; i <= targetNum; i++)            {                // sum += i;                //判断i是否为一个奇数                if (i % 2 != 0)                {                    sum += i;                }            }            return sum;        }    }}


 

0 0
原创粉丝点击