C#--各种方法总结(静态,构造,析构,虚方法,重写方法,抽象,扩展)

以下是学习笔记:

【1】虚方法--virtrual(小蜜蜂)

  作用:允许子类/派生类,进行重写,也实现不一样的功能。

  特点:好维护

【2】重写方法--override

【3】抽象方法--abstract(大懒虫)

  定义:一定要写在抽象类中,而且不能new,不带方法体

  使用场所:强制性,一定要实现

  抽象方法与接口的区别:  

    区别:1,抽象类--单继承,接口可以多继承

               2,抽象类中可以写普通方法,虚方法,接口只能写规范,不写实现

    使用场所:1,抽象类一般用于不会经常改动,然后抽象范围大一些的事物,人》男人》女人

                  2,接口适用于:经常修改,只是写一个规范的地方。

【4】扩展方法--ExtendMethod

  定义:把范围扩大了,不是修改,不修改源码(类,方法)的情况下进行扩展

     在静态类中,定义静态方法

  使用场所:1,调用密封类中的对象,属性,或者方法(扩展密封类)

               2,扩展接口

          3,在Linq链式编程

一,扩展方法

1,扩展方法运用:扩展密封类的方法

【1】密封类:

namespace _016_扩展方法
{
    /// <summary>
    /// 假如这个类是 别人那里拿过来的,不是自己写的,是个密封类
    /// </summary>
    public sealed class Person
    {
        public string Name { get; set; }
        public int  Age { get; set; }
        public string Phone { get; set; }

        /// <summary>
        /// 返回电话
        /// </summary>
        /// <returns></returns>
        public string GetPhone()
        {
            return Phone;
        }
    }
}

  

【2】给Person类添加扩展方法

namespace _016_扩展方法
{
    /// <summary>
    /// 定义一个静态类
    /// </summary>
    public static class PersonExtend
    {
        /// <summary>
        /// 写一个静态方法
        /// </summary>
        public static void ShowPhone(this Person person)//this + 要使用的类型:表示给Person类型添加了一个ShowPhone的扩展方法
        {
            Console.Write("这是扩展方法输出的结果");
            string str = person.GetPhone();
            Console.WriteLine(str);            
        }
    }
}

  

【3】在主程序中调用

            Person person=new Person()
            {
                Age = 20,
                Name = "jason",
                Phone = "18688888888",
            };

            person.ShowPhone();//Person就多了一个ShowPhone方法

  

结果:

【4】总结密封类的定义方法

2,扩展方法运用:接口的扩展

【1】定义一个接口

namespace _016_扩展方法
{
    public interface ICalculate
    {
        int Add(int a, int b);
    }
}

  

【2】定义接口的扩展方法

namespace _016_扩展方法
{
    public static class InterfaceExtendcs 
    {
        public static int Maltiply(this ICalculate calculate, int a, int b)
        {
            return a*b;
        }

        public static int Division(this ICalculate calculate, int a, int b)
        {
            return a / b;
        }
        public static int Sub(this ICalculate calculate, int a, int b)
        {
            return a - b;
        }
    }
}

  

【3】接口的实现类

namespace _016_扩展方法
{
    public class A : ICalculate
    {
        public int Add(int a, int b)
        {
            return a + b;
        }
    }
}

  

namespace _016_扩展方法
{
    public class C : ICalculate
    {
        public int Add(int a, int b)
        {
            return a+b;
        }
    }
}

  

【4】在主程序中调用

            A a =new A();
            int i1= a.Add(1, 2);//接口方法
            int i2= a.Maltiply(1, 2);//接口的扩展方法
            Console.WriteLine($"接口方法输出:{i1.ToString()},接口的扩展方法输出:{i2.ToString()}");

            C c=new C();
            int i3 = c.Add(1 , 2);//接口方法
            int i4 = c.Division(1, 2);//接口的扩展方法
            Console.WriteLine($"接口方法输出:{i3.ToString()},接口的扩展方法输出:{i4.ToString()}");

  

结果:

还不够完整,待续。。。

原文地址:https://www.cnblogs.com/baozi789654/p/14233042.html