C#基本知识点收录:delegate,event,interface,abstract,virtual,indexer

1.delegate问题:

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

namespace ConsoleApp1
{
    class Program
    {
        static void OtherClassMethod()
        {
            Console.WriteLine("Delegate an other class's method");
        }

        static void Main(string[] args)
        {
            var test = new TestDelegate();
            test.delegateMethod = new TestDelegate.DelegateMethod(test.NonStaticMethod);
            test.delegateMethod += new TestDelegate.DelegateMethod(TestDelegate.StaticMethod);
            test.delegateMethod += Program.OtherClassMethod;
            test.RunDelegateMethods();
        }
    }

    class TestDelegate
    {
        public delegate void DelegateMethod();  //声明了一个Delegate Type

        public DelegateMethod delegateMethod;   //声明了一个Delegate对象

        public static void StaticMethod()
        {
            Console.WriteLine("Delegate a static method");
        }

        public void NonStaticMethod()
        {
            Console.WriteLine("Delegate a non-static method");
        }

        public void RunDelegateMethods()
        {
            if (delegateMethod != null)
            {
                Console.WriteLine("---------");
                delegateMethod.Invoke();
            }
            Console.WriteLine("---------");
        }
    }
}

 2.delegate,event问题:

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

namespace ConsoleApp2
{
    
        class Program
        {
            static void Main(string[] args)
            {
                var car = new Car(15);
                new Alerter(car);
                car.Run(120);
            }
        }

        class Car
        {
            public delegate void Notify(int value);
            public event Notify notifier;

            private int petrol = 0;
            public int Petrol
            {
                get { return petrol; }
                set
                {
                    petrol = value;
                    if (petrol < 10)  //当petrol的值小于10时,出发警报
                    {
                        if (notifier != null)
                        {
                            notifier.Invoke(Petrol);
                        }
                    }
                }
            }

            public Car(int petrol)
            {
                Petrol = petrol;
            }

            public void Run(int speed)
            {
                int distance = 0;
                while (Petrol > 0)
                {
                    Thread.Sleep(500);
                    Petrol--;
                    distance += speed;
                    Console.WriteLine("Car is running... Distance is " + distance.ToString());
                }
            }
        }

        class Alerter
        {
            public Alerter(Car car)
            {
                car.notifier += new Car.Notify(NotEnoughPetrol);
            }

            public void NotEnoughPetrol(int value)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("You only have " + value.ToString() + " gallon petrol left!");
                Console.ResetColor();
            }
        }
    }

 3.interface问题:

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

namespace ConsoleApp3
{
    public interface IPerson
    {
        string Name
        {
            get;
            set;
        }
        void Show(string name);
    }

    public interface IStudent
    {
        string StudentId
        {
            get;
            set;
        }
        void Show(string studentid);
    }

    public class Student : IPerson, IStudent
    {
        private string _name;
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }

        private string _studentid;
        public string StudentId
        {
            get
            {
                return _studentid;
            }
            set
            {
                _studentid = value;
            }
        }

        void IPerson.Show(string name)
        {
            Console.WriteLine("姓名为{0}", name);
        }

        void IStudent.Show(string studentid)
        {
            Console.WriteLine("学号为{0}", studentid);
        }
    }

    class Program
    {
        static void Main()
        {
            Student s = new Student();
            Console.WriteLine("输入姓名");
            s.Name = Console.ReadLine();
            Console.WriteLine("输入学号");
            s.StudentId = Console.ReadLine();
            IPerson per = s;
            per.Show(s.Name);
            IStudent stu = s;
            stu.Show(s.StudentId);
        }
    }
}

4.abstract,virtual问题:

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

namespace ConsoleApp4
{
    public abstract class Book
    {
        public Book()
        {
        }
        public abstract void getPrice(); //抽象方法,不含主体
        public virtual void getName() //虚方法,可覆盖
        {
            Console.WriteLine("this is a test:virtual getName()");
        }
        public virtual void getContent() //虚方法,可覆盖
        {
            Console.WriteLine("this is a test:virtual getContent()");
        }
        public void getDate() //一般方法,若在派生类中重写,须使用new关键字
        {
            Console.WriteLine("this is a test: void getDate()");
        }
    }
    public class JavaBook : Book
    {
        public override void getPrice() //实现抽象方法,必须实现
        {
            Console.WriteLine("this is a test:JavaBook override abstract getPrice()");
        }
        public override void getName() //覆盖原方法,不是必须的
        {
            Console.WriteLine("this is a test:JavaBook override virtual getName()");
        }
    }
    class test
    {
        public  test()
        {
            JavaBook jbook = new JavaBook();
            jbook.getPrice(); //将调用JavaBook中getPrice()
            jbook.getName(); //将调用JavaBook中getName()
            jbook.getContent(); //将调用Book中getContent()
            jbook.getDate(); //将调用Book中getDate()
        }
      
    }
    class Program
    {
        public static void Main()
        {
            test t = new test();
        }
    }
}

5.Indexer问题1:

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

namespace ConsoleApp5
{
    public class IDXer
    {
        private string[] name = new string[10];

        //索引器必须以this关键字定义,其实这个this就是类实例化之后的对象
        public string this[int index]
        {
            get
            {
                return name[index];
            }
            set
            {
                name[index] = value;
            }
        }
    }

    public class Program
    {
        static void Main(string[] args)
        {
            //最简单索引器的使用           
            IDXer indexer = new IDXer();
            //“=”号右边对索引器赋值,其实就是调用其set方法
            indexer[0] = "张三";
            indexer[1] = "李四";
            //输出索引器的值,其实就是调用其get方法
            Console.WriteLine(indexer[0]);
            Console.WriteLine(indexer[1]);
            Console.ReadKey();
        }
    }
}

6.indexer问题2:

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

namespace ConsoleApp6
{
    /// <summary>
    /// 成绩类
    /// </summary>
    public class Scores
    {
        /// <summary>
        /// 学生姓名
        /// </summary>
        public string StuName { get; set; }

        /// <summary>
        /// 课程ID
        /// </summary>
        public int CourseId { get; set; }

        /// <summary>
        /// 分数
        /// </summary>
        public int Score { get; set; }

    }

    /// <summary>
    /// 查找成绩类(索引器)
    /// </summary>
    public class FindScore
    {
        private List<Scores> listScores;

        public FindScore()
        {
            listScores = new List<Scores>();
        }

        //索引器 通过名字&课程编号查找和保存成绩
        public int this[string stuName, int courseId]
        {
            get
            {
                Scores s = listScores.Find(x => x.StuName == stuName && x.CourseId == courseId);
                if (s != null)
                {
                    return s.Score;
                }
                else
                {
                    return -1;
                }
            }
            set
            {
                listScores.Add(new Scores() { StuName = stuName, CourseId = courseId, Score = value });
            }
        }

        //索引器重载,根据名字查找所有成绩
        public List<Scores> this[string stuName]
        {
            get
            {
                List<Scores> tempList = listScores.FindAll(x => x.StuName == stuName);
                return tempList;
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            //多参数索引器和索引器重载          
            FindScore fScore = new FindScore();
            fScore["张三", 1] = 98;
            fScore["张三", 2] = 100;
            fScore["张三", 3] = 95;
            fScore["李四", 1] = 96;
            //查找 张三 课程编号2 的成绩
            Console.WriteLine("李四 课程编号2 成绩为:" + fScore["李四", 1]);
            //查找所有张三的成绩
            List<Scores> listScores = fScore["张三"];
            if (listScores.Count > 0)
            {
                foreach (Scores s in listScores)
                {
                    Console.WriteLine(string.Format("张三 课程编号{0} 成绩为:{1}", s.CourseId, s.Score));
                }
            }
            else
            {
                Console.WriteLine("无该学生成绩单");
            }
            Console.ReadKey();
        }
    }
}

 

posted on 2020-10-06 23:26  格码拓普  阅读(112)  评论(0编辑  收藏  举报