C#常用方法详细讲解二
- 抽象方法(abstract)
- 扩展方法(Extend)
- 扩展接口(InterfaceExtend)
转载请标明出处: Python Excellent的博客
一、抽象方法(abstract)
* 抽象方法:
* 定义:一定要写在抽象类里面,不带方法体代码,不能进行实例化
* 特点:只规范一次
* 使用场合:强制性一定要实现该抽象方法的子类
*
* 与接口区别及使用场合:
* 区别: 1--抽象类单继承 接口可以多继承
* 2--抽象类中也可声明其它普通方法,虚方法等,接口只能写规范,不能写实现
* 使用场合:抽象类 一般用于不经常改动,且抽象范围大的事件、人,汽车
* 接口 适用于一些经常修改,但是需要规范的地方(如人的行为)
class Test
{
//控制台测试输出
static void Main(string[] args)
{
//5--抽象方法
AbstractMethodChild abstractChild = new AbstractMethodChild();
Console.WriteLine($"重写抽象方法输出:7 + 8 = {abstractChild.Calculate(7, 8)}" + "\n");
InterfaceClass AddTest = new InterfaceClass();
InterfaceClass SubTest = new InterfaceClass();
Console.WriteLine($"接口方法输出:7 + 8 = {AddTest.Add(7, 8)}" + "\n");
Console.WriteLine($"接口方法输出:7 - 8 = {SubTest.Sub(7, 8)}" + "\n");
}
#region 抽象类
public abstract class AbstractMethod
{
/// <summary>
/// 已规范的抽象方法
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public abstract int Calculate(int a, int b);//无需具体方法体,只需规范即可
//抽象类中也可声明其它普通方法,虚方法等
public virtual int Subcal(int a, int b)
{
return a - b;
}
public int Maltical(int a, int b)
{
return a * b;
}
}
public class AbstractMethodChild : AbstractMethod
{
//规范好了方法让子类去实现
public override int Calculate(int a, int b)
{
return a + b;
}
}
#endregion
}
抽象方法Main测试的实现条件如下
- 1、添加一个接口1:Interface1.cs // .CS文件 一般接口文件命名以大写i开头
namespace TestMethod01
{
//接口和抽象类一样 只进行规范声明,并不实现
public interface Interface1//接口1
{
int Add(int a, int b);
}
//定义一个接口测试类 继承接口并在此类中实现接口的方法
class InterfaceClass : Interface1, Interface2 //可多继承,一旦继承接口2 就必须实现接口2成员
{
public int Add(int a,int b)
{
return a + b;
}
public int Sub(int a, int b)
{
return a - b;
}
}
}
- 2、添加一个接口2:Interface2.cs //
namespace TestMethod01
{
public interface Interface2//接口2
{
int Sub(int a, int b);
}
}
二、扩展方法(Extend)
* 扩展方法:
* 定义:在静态类中 定义静态方法 ---(即扩展方法)
* 使用场合:1--调用密封类中的对象,属性,或者方法---(扩展密封类)
* 2--扩展接口
class Test
{
//控制台测试输出
static void Main(string[] args)
{
//6--扩展方法(封闭类)
Person person = new Person()
{
Age = 24,
Name = "GGB",
City = "江西",
Phone = "9527-1234",
};
//需先实例化一个人的对象,将人传入扩展方法参数中,即可调用扩展方法输出电话
Console.WriteLine($"电话:{PersonExtend.ShowPhone(person)}" + "\n");
//非封闭类的扩展调用
PersonExtend01 personExtend01 = new PersonExtend01();
personExtend01.GetPhone();
}
}
扩展方法Main测试的实现条件如下
先构建一个密封类(sealed),以便于待会对此类进行扩展调用
- 1、添加一个密封类 Person.cs //在解决方案内新建该密封类
namespace TestMethod01
{
public sealed class Person //假设此类是别人那里拿来的一个 密封类
{
public string Name { get; set; }
public int Age { get; set; }
public string City { get; set; }
public string Phone { get; set; }
public string GetPhone()// 返回电话方法
{
return Phone;
}
}
}
- 2、添加一个扩展类PersonExtend.cs//声明一个静态类,里面的静态方法调用密封类中方法
namespace CSharpAdvanced01
{
/// <summary>
/// 定义一个静态类,再声明一个静态方法
/// </summary>
public static class PersonExtend
{
public static string ShowPhone(this Person person )
{
return person.GetPhone();
}
}
}
非密封类扩展的话就和类的继承一样即可进行调用
namespace TestMethod01
{
public class Person1 //假设此类是别人那里拿来的一个 密封类
{
public string Name { get; set; }
public int Age { get; set; }
public string City { get; set; }
public string Phone { get; set; }
public string GetPhone()// 返回电话方法
{
return Phone;
}
}
}
namespace TestMethod01
{
//非密封类不需要在静态类中声明方法,只需类继承后即可在Main函数中调用父类所有方法
public class PersonExtend1: Person
{
//无需声明方法和属性
}
}
三、扩展接口(InterfaceExtend)
- 扩展方法:
* 2–扩展接口
class Test
{
//控制台测试输出
static void Main(string[] args)
{
//7--扩展接口
CalssInterfaceExtTest classTestExt= new CalssInterfaceExtTest();
//可以调用扩展接口类中的所有方法,但是扩展接口类的继承接口只声明一个规范
classTestExt.Add(3, 5);
classTestExt.Sub(3, 5);
classTestExt.Malti(3, 5);
classTestExt.Div(3, 5);
}
}
扩展接口Main测试的实现条件如下
先构建一个密封类(sealed),以便于待会对此类进行扩展调用
- 1、添加一个计算类接口:Icalculate.cs //大写i开头的cs文件表示接口
namespace CSharpAdvanced01
{
//扩展接口中加减乘除均继承该接口
public interface Icalculate
{
int calculate(int a,int b);
}
}
- 2、添加一个扩展接口类:InterfaceExtend.cs //在解决方案内添加即可
namespace TestMethod01
{
public static class InterfaceExtend
{
//加减乘除均继承 计算类接口的规范方法
public static int Add(this Icalculate icalculate ,int a, int b)
{
return a + b;
}
public static int Sub(this Icalculate icalculate, int a, int b)
{
return a - b;
}
public static int Malti(this Icalculate icalculate, int a, int b)
{
return a * b;
}
public static int Div(this Icalculate icalculate, int a, int b)
{
return a / b;
}
}
}
- 3、添加一个扩展接口测试类:CalssInterfaceExtTest
namespace CSharpAdvanced01
{
public class CalssInterfaceExtTest : Icalculate
{
public int calculate(int a, int b)
{
return a + b;
}
}
}