C#学习笔记(控制台程序)

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

//namespace test
//{
    
    
//    class Program
//    {
    
    
//        public delegate bool ShortDelegate(int[] x);
//        static void Main(string[] args)
//        {
    
    
//            int[] arr = new int[] { 8, 4, 5, 6, 2, 1, 3, 7, 9, 0 };
//            Console.WriteLine("未排序前数组序列为:");
//            foreach (int i in arr)
//            {
    
    
//                Console.Write("{0},", i);
//            }

//            ShortDelegate MyDelegate = array => 
//            {
    
    
//                for (int i = array.GetUpperBound(0); i >= 0; i--)//循环从后向前分别取出数组中的一个元素
//                {//冒泡排序,将数值小的元素移到后面,直到所有的元素按照降序排列
//                    for (int j = 0; j <= i; j++)
//                    {
    
    
//                        if (array[j] <= array[i])
//                        {//交换两个元素位置
//                            int z = array[j];
//                            array[j] = array[i];
//                            array[i] = z;
//                        }
//                    }
//                }
//                return true;
//            };
            
//            MyDelegate(arr);//调用委托排序
//            Console.WriteLine("\n排序后数组序列为:");
//            foreach (int i in arr)
//            {
    
    
//                Console.Write("{0},", i);
//            }
//            Console.ReadKey();


//        }
//    }
    
//}
//namespace test
//{
    
    
//    class Program
//    {
    
    
//        static void Main(string[] args)
//        {
    
    
//            Heater heater = new Heater();
//            heater.BoilEvent += delegate(int param)
//            {
    
    
//                Console.WriteLine("Alarm:dididid,水已经{0}℃了", param);
//            };

//            heater.BoilEvent += delegate(int param)
//            {
    
    
//                Console.WriteLine("Display:水快开了,当前温度为:{0}℃", param);
//            };
//            heater.BoilWater();
//            Console.ReadKey();


//        }
//    }
//    public class Heater
//    {
    
    
//        private int temperature;//水温
//        public delegate void BoilHandle(int x);//声明关于事件的委托
//        public event BoilHandle BoilEvent;//声明水要烧开的事件
//        public void BoilWater()
//        { //烧水的方法
//            for (int i = 0; i <= 100; i++)
//            {
    
    
//                temperature = i;
//                if (temperature > 96)
//                {
    
    
//                    if (BoilEvent != null)
//                    {
    
    
//                        BoilEvent(temperature);
//                    }
//                }
//            }
//        }

//    }


//}
//class Program
//    {
    
    
//        static void Main(string[] args)
//        {
    
    
//            Point p1 = new Point();

//            Console.WriteLine("无参数构造函数演示");
//            Console.WriteLine("X:" + p1.X);
//            Console.WriteLine("Y:" + p1.Y);

//            Point p2 = new Point(2.0,3.0);

//            Console.WriteLine("有参数构造函数演示");
//            Console.WriteLine("X:" + p2.X);
//            Console.WriteLine("Y:" + p2.Y);

//            Console.ReadKey();

           
//        }
//    }
//    public class Point               //定义Point类
//    {                                // 类的属性定义中set,get可以简写为以下形式  
//        public Point()
//        {
    
    
//            X = 0.0; Y = 2.0;//构造函数               //构造函数内容
//        }
//        public Point(double x,double y)
//        {
    
    
//            X = x; Y = y;//构造函数               //构造函数内容
//        }
//        ~Point()                     //析构函数名
//        {
    
    
//            Console.WriteLine("清理资源");//析构函数               //析构函数内容
//        }
//        public double X { get; set; }//定义属性X,点的横坐标
//        public double Y { get; set; }//定义属性Y,点的纵坐标
//    }

//namespace test
//{
    
    
//    class Program
//    {
    
    
//        static void Main(string[] args)
//        {
    
    
//            Person person1 = new Person("小明", 18, "男");//利用有参构造函数创建对象
//            person1.DisPlay();                              //调用DisPlay方法显示信息


//            Student Stu1 = new Student("小美", 19, "女"); //利用有参构造函数创建对象
//            Stu1.Class = "2014级护理专业";
//            Stu1.DisPlay();                             //调用基类DisPlay方法显示信息
//            Stu1.Study();

//            Person person2 = new Person();              //创建person类对象
//            person2 = Stu1;                             //把派生类对象赋值给基类对象
//            person2.DisPlay();                          //调用DisPlay方法显示信息
//            ((Student)person2).Study();                 //基类对象调用派生类的方法

//            Console.ReadKey();
//        }
//    }
//    public class Person
//    {
    
    
//        private String _id;         //身份证号码字段id
//        public string ID            //身份证号码属性id
//        {
    
    
//            get { return this._id; }
//            set { this._id = value; }
//        }

//        private String _name;       //姓名字段
//        public string Name          //姓名属性
//        {
    
    
//            get { return this._name; }
//            set { this._name = value; }
//        }

//        private int _age;           //年龄字段
//        public int Age              //年龄属性
//        {
    
    
//            get { return this._age; }
//            set { this._age = value; }
//        }

//        private String _gender;       //性别字段
//        public string Gender          //性别属性
//        {
    
    
//            get { return this._gender; }
//            set { this._gender = value; }
//        }

//        public Person()                 //无参构造函数
//        { }
//        public Person(string name, int age, string gender)    //有参构造函数
//        {
    
    
//            this._name = name;
//            this._age = age;
//            this._gender = gender;
//        }
//        public void DisPlay()           //基类共用方法,显示人的信息
//        {
    
    
//            Console.WriteLine("{0}是{1}性,年龄为{2}岁", this._name, this._gender, this._age);
//        }
//    }
//    public class Student : Person           //创建派生类,派生自person类
//    {
    
    
//        //定义派生类自己的属性
//        private String _class;               //学生班级
//        public string Class
//        {
    
    
//            get { return _class; }
//            set { _class = value; }
//        }
//        private String _department;               //学生院系
//        public string Department
//        {
    
    
//            get { return _department; }
//            set { _department = value; }
//        }
//        private String _no;               //学生学号
//        public string No
//        {
    
    
//            get { return _no; }
//            set { _no = value; }
//        }

//        public Student()                // 无参构造函数
//        {
    
    

//        }
//        public Student(string name, int age, string gender)
//            : base(name, age, gender)
//        {                               // 调用基类的构造函数

//        }
//        //定义派生类独有方法Study,表示在校学习
//        public void Study()
//        {
    
    
//            Console.WriteLine("学生在校学习");
//        }


//    }

//}
    
//}
//namespace test
//{
    
    
//    class PhoneClass
//    {
    
    
//        public string brand;
//        public string type;
//    }
//    class Program
//    {
    
    
//        static void Main(string[] args)
//        {
    
    
//            PhoneClass Phone = new PhoneClass();
//            Phone.brand = "huawei";
//            Phone.type = "5G";
//            Console.WriteLine("手机品牌是:" + Phone.brand);
//            Console.WriteLine("手机种类是:" + Phone.type);
//            Console.ReadKey();

//            var p = new { X = 0.0, Y = 0.0 };   //创建匿名类型p,并使用对象初始化器赋值。
//            //Point P = new Point() { X = 0.0, Y = 0.0 };     //创建对象P并对XY赋值
//        }
//    }
//    public class Point               //定义Point类
//    {                                // 类的属性定义中set,get可以简写为以下形式  
//        Point()
//        {
    
    
//            X = 0.0; Y = 2.0;//构造函数               //构造函数内容
//        }
//        Point(double x, double y)
//        {
    
    
//            X = x; Y = y;//构造函数               //构造函数内容
//        }
//        ~Point()                     //析构函数名
//        {
    
    
//            Console.WriteLine("清理资源");//析构函数               //析构函数内容
//        }
//        public double X { get; set; }//定义属性X,点的横坐标
//        public double Y { get; set; }//定义属性Y,点的纵坐标
//    }
//}
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;

//namespace test
//{
    
    
//    class Program
//    {
    
    
//        static void Main(string[] args)
//        {
    
    
//            Student Stu1;
//            Stu1.stuNum = "14031403";
//            Stu1.stuName = "小明";
//            Stu1.stuAge = 19;
//            Stu1.stuClass = 1;
//            Stu1.stuGrade = 12;


//            Console.WriteLine("结构体演示");
//            Console.WriteLine("学生 " + Stu1.stuName.ToString() + ":");
//            Console.WriteLine("学号:{0},年龄:{1},年级:{2},班级:{3}",Stu1.stuNum,Stu1.stuAge,Stu1.stuGrade,Stu1.stuClass);

//            Stu1.ChengeStudentInf("14031403", "小明", 19, 1, 13);
//            Console.WriteLine("调用结构体方法演示");
//            Console.WriteLine("学生 " + Stu1.stuName.ToString() + ":");
//            Console.WriteLine("学号:{0},年龄:{1},年级:{2},班级:{3}", Stu1.stuNum, Stu1.stuAge, Stu1.stuGrade, Stu1.stuClass);

//            Student stu2 = new Student("14081408", "小王", 18, 3, 12);
//            Console.WriteLine("调用结构体构造函数演示");
//            Console.WriteLine("学生 " + stu2.stuName.ToString() + ":");
//            Console.WriteLine("学号:{0},年龄:{1},年级:{2},班级:{3}", stu2.stuNum, stu2.stuAge, stu2.stuGrade, stu2.stuClass);

//            Console.ReadKey();
//        }
//    }
//    public struct Student                   //定义结构体Student
//    {
    
    
//        public String stuNum;               //定义结构成员表示学生学号
//        public String stuName;              //定义结构成员表示学生名字
//        public int stuAge;                  //定义结构成员表示学生年龄
//        public int stuClass;                //定义结构成员表示学生班级
//        public int stuGrade;                //定义结构成员表示学生年级
//        public Student(String stuNum, String stuName, int stuAge, int stuClass, int stuGrade)
//        {                                   //结构体的构造函数,用参数给成员赋值
//            this.stuNum = stuNum;
//            this.stuName = stuName;
//            this.stuAge = stuAge;
//            this.stuClass = stuClass;
//            this.stuGrade = stuGrade;
//        }
//        public void ChengeStudentInf(String stuNum, String stuName, int stuAge, int stuClass, int stuGrade)
//        {                                   //结构体的方法函数,用参数改变成员的值
//            this.stuNum = stuNum;
//            this.stuName = stuName;
//            this.stuAge = stuAge;
//            this.stuClass = stuClass;
//            this.stuGrade = stuGrade;
//        }


//    }
    
//}
namespace test
{
    
    
    class Program
    {
    
    
        public delegate void stuDelegate(string name);
        static void Main(string[] args)
        {
    
    
            Person person1 = new Person("小明", 18, "男");//利用有参构造函数创建对象
            person1.DisPlay();                              //调用DisPlay方法显示信息
            person1.DoWork();
            person1.DoSport();
            Console.ReadKey();
            Manager Manager1 = new Manager("liu");
            Manager.display();
            Console.ReadKey();
        }
    }
    interface IPerson               //定义Point接口类
    {
    
    
        void DoWork();             //声明方法
        void DoSport();
    }
    public class Person : IPerson
    {
    
    
        private String _id;         //身份证号码字段id
        public string ID            //身份证号码属性id
        {
    
    
            get {
    
     return this._id; }
            set {
    
     this._id = value; }
        }

        private String _name;       //姓名字段
        public string Name          //姓名属性
        {
    
    
            get {
    
     return this._name; }
            set {
    
     this._name = value; }
        }

        private int _age;           //年龄字段
        public int Age              //年龄属性
        {
    
    
            get {
    
     return this._age; }
            set {
    
     this._age = value; }
        }

        private String _gender;       //性别字段
        public string Gender          //性别属性
        {
    
    
            get {
    
     return this._gender; }
            set {
    
     this._gender = value; }
        }

        public Person()                 //无参构造函数
        {
    
     }
        public Person(string name, int age, string gender)    //有参构造函数
        {
    
    
            this._name = name;
            this._age = age;
            this._gender = gender;
        }
        public void DoWork()
        {
    
    
            Console.WriteLine("{0}的任务是工作", this._name);
        }
        public void DoSport()
        {
    
    
            Console.WriteLine("{0}喜欢做运动", this._name);
        }
        public void DisPlay()           //基类共用方法,显示人的信息
        {
    
    
            Console.WriteLine("{0}是{1}性,年龄为{2}岁", this._name, this._gender, this._age);
        }
    }
    public abstract class Employee
    {
    
    
        public abstract void Display();             //抽象方法Display
        public static void display()
        {
    
    
            Console.WriteLine("1是性,年龄1为1岁");
        }
    }
    public class Manager : Employee
    {
    
    
        private string name;
        public Manager(string Name)
        {
    
    
            name = Name;
        }
        public override void Display()
        {
    
    
            Console.WriteLine("Name:" + name);
        }
    }
    public sealed class Animal
    {
    
    
        public Animal()
        {
    
    
            Console.WriteLine("动物被构造");
        }
    }
}
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;

//namespace test
//{
    
    
//    class Program
//    {
    
    
//        public delegate bool ShortDelegate(int[] x);
//        static void Main(string[] args)
//        {
    
    
//            int[] arr = new int[] { 8, 4, 5, 6, 2, 1, 3, 7, 9, 0 };
//            Console.WriteLine("未排序前数组序列为:");
//            foreach (int i in arr)
//            {
    
    
//                Console.Write("{0},", i);
//            }

//            ShortDelegate MyDelegate;//声明委托变量
//            MyDelegate = new ShortDelegate(ArrayShort.SortArray);//实例化委托

//            MyDelegate(arr);//调用委托排序
//            Console.WriteLine("\n排序后数组序列为:");
//            foreach (int i in arr)
//            {
    
    
//                Console.Write("{0},", i);
//            }
//            Console.ReadKey();

            
//        }
//    }
//    class ArrayShort
//    {
    
    
//        public static bool SortArray(int[] Array)
//        { //定义方法SortArray用于按照降序排列
//            for (int i = Array.GetUpperBound(0); i >= 0; i--)//循环从后向前分别取出数组中的一个元素
//            {//冒泡排序,将数值小的元素移到后面,直到所有的元素按照降序排列
//                for (int j = 0; j <= i; j++)
//                {
    
    
//                    if (Array[j] <= Array[i])
//                    {
    
    
//                        Swap(ref Array[j], ref Array[i]);//交换两个元素位置
//                    }
//                }
//            }
//            return true;
//        }
//        static void Swap(ref int x,ref int y)//交换两个数
//        {
    
    
//            int z = x;
//            x = y;
//            y = z;
//        }
//    }

//}
//namespace test
//{
    
    
//    class Program
//    {
    
    
//        public delegate bool ShortDelegate(int[] x);
//        static void Main(string[] args)
//        {
    
    
//            Heater heater = new Heater();
//            Alarm alarm = new Alarm();

//            heater.BoilEvent += alarm.MakeAlert;
//            heater.BoilEvent += Display.ShowMsg;
//            heater.BoilWater();
//            Console.ReadKey();


//        }
//    }
//    public class Heater
//    {
    
    
//        private int temperature;//水温
//        public delegate void BoilHandle(int x);//声明关于事件的委托
//        public event BoilHandle BoilEvent;//声明水要烧开的事件
//        public void BoilWater()
//        { //烧水的方法
//            for (int i = 0; i <= 100; i++)
//            {
    
    
//                temperature = i;
//                if (temperature > 96)
//                {
    
    
//                    if (BoilEvent != null)
//                    {
    
    
//                        BoilEvent(temperature);
//                    }
//                }
//            }
//        }

//    }
//    public class Alarm
//    {
    
    
//        public void MakeAlert(int x)
//        { //水开烧开的报警方法
//            Console.WriteLine("Alarm:dididid,水已经{0}℃了", x);
//        }
//    }
//    public class Display
//    {
    
    
//        public static void ShowMsg(int x)
//        {
    
    
//            Console.WriteLine("Display:水快开了,当前温度为:{0}℃", x);
//        }
//    }

//}

猜你喜欢

转载自blog.csdn.net/cashmood/article/details/112176992