using System; namespace ConsoleApp { class Program { /// <summary> /// 入口 /// </summary> /// <param name="args"></param> static void Main(string[] args) { #region 交换两个变量的值 //int num1 = 5; //int num2 = 6; //Console.WriteLine(" num1={0}\r\n num2={1}", num1, num2); //int temp = num1; //num1 = num2; //num2 = temp; //Console.WriteLine(" num1={0}\r\n num2={1}", num1, num2); //Console.ReadKey(); #endregion #region 求两个数的最大值 //int num1 = 5; //int num2 = 8; //int num3 = GetMax(num1, num2); //Console.WriteLine("最大值为:{0}", num3); //Console.ReadKey(); #endregion #region 1-100之间所有奇数的和 //int result = 0; //for (int i = 1; i <= 100; i++) //{ // if (i % 2 != 0) // { // result += i; // } //} //Console.WriteLine("1-100之间所有奇数的和为:{0}", result); //Console.ReadKey(); #endregion #region 找数组中最大的数 //int[] array = new int[] { 5, 4, 7, 9, 6, 3, 1 }; //int max = array[0]; //for (int i = 0; i < array.Length; i++) //{ // if (array[i] > max) // { // max = array[i]; // } //} //Console.WriteLine("数组中最大的值为:{0}", max); //Console.ReadKey(); #endregion #region 时间格式转换 //string time = "2014年6月7号"; //string msg = FormatConversion(time); //Console.WriteLine(msg); //Console.ReadKey(); #endregion Person per = new Person(); per.Name = "刘先生"; per.Sex = "男"; per.Age = 22; per.SayHi(); Employee emp = new Employee(); emp.Name = "孙悟空"; emp.Sex = "公"; emp.Age = 5000; emp.Salary = 10000; emp.SayHi(); Console.ReadKey(); } /// <summary> /// 求两个数的最大值 /// </summary> /// <param name="num1"></param> /// <param name="num2"></param> /// <returns></returns> public static int GetMax(int num1, int num2) { return num1 > num2 ? num1 : num2; } /// <summary> /// 时间格式转换 /// </summary> /// <param name="time"></param> /// <returns></returns> public static string FormatConversion(string time) { char[] cha = time.ToCharArray(); for (int i = 0; i < cha.Length; i++) { switch (cha[i]) { case ‘0‘: cha[i] = ‘零‘; break; case ‘1‘: cha[i] = ‘一‘; break; case ‘2‘: cha[i] = ‘二‘; break; case ‘3‘: cha[i] = ‘三‘; break; case ‘4‘: cha[i] = ‘四‘; break; case ‘5‘: cha[i] = ‘五‘; break; case ‘6‘: cha[i] = ‘六‘; break; case ‘7‘: cha[i] = ‘七‘; break; case ‘8‘: cha[i] = ‘八‘; break; case ‘9‘: cha[i] = ‘九‘; break; } } return new string(cha); } } }
person:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp { public class Person { private string _name; private string _sex; private int _age; /// <summary> /// 姓名 /// </summary> public string Name { get { return _name; } set { _name = value; } } /// <summary> /// 性别 /// </summary> public string Sex { get { return _sex; } set { _sex = value; } } /// <summary> /// 年龄 /// </summary> public int Age { get { return _age; } set { _age = value; } } public virtual void SayHi() { Console.WriteLine("我叫:{0},性别:{1},今年:{2}岁", Name, Sex, Age); } } }
Employee:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp { public class Employee : Person { private double _salary; /// <summary> /// 工资 /// </summary> public double Salary { get { return _salary; } set { _salary = value; } } public override void SayHi() { Console.WriteLine("我叫:{0},性别:{1},今年:{2}岁,工资:{3}", Name, Sex, Age, Salary); } } }
时间: 2024-12-29 11:40:42