zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C#,入门教程(38)——大型工程软件中类(class)修饰词partial的使用方法

c#方法软件入门教程 工程 Class 大型 38
2023-09-11 14:15:48 时间

上一篇:

C#,入门教程(37)——优秀程序员的修炼之道icon-default.png?t=MBR7https://blog.csdn.net/beijinghorn/article/details/125011644 

一、大型(工程应用)软件倚重 partial

先说说大型(工程应用)软件对源代码的文件及函数“行”数的基本要求:
(1)每个class文件不要太多行,512行就算多的了;
(2)每个函数的行数也不要太多,256行就不少了;

而实际上,很多 class 却不得不又很多行程序,因而对于(1)而言 partial 就非常重要了。

首先,partial 允许将一个 class 拆分,写到多个 .cs 文件中!
其次,partial 允许大家分工编写 class 的不同部分!

总之,partial 非常有用!

二、一些不太重要的限制

至于下面的一下局限性与限制,大致了解即可:
1、仅适用于类class、接口interface、结构struct,不支持委托delegate、枚举enum;
2、每个部分必须都有修饰符 partial;
3、位于同一个的命名空间namespace;
4、如有部分使用static/abstract/sealed,整个类都被视为static/abstract/sealed;互相不能矛盾;
5、各个部分的基类必须一致;
6、局部类型上的接口具有累加效应。

三、partial 实例

字符串匹配(Pattern Search)有很多算法,各种算法可以写在一个 静态类中。

但应该分别写在不同的  cs 文件,便于维护、协作、管理。

文件1、Legalsoft.Truffer.PatternSearch.KMP.cs

using System;
using System.Collections;
using System.Collections.Generic;

namespace Legalsoft.Truffer.Algorithm
{
	/// <summary>
	/// 字符串匹配(模式搜索)算法集锦
	/// </summary>
	public static partial class PatternSearch
	{
		/// <summary>
		/// 字符串匹配的KMP算法
		/// </summary>
		/// <param name="pat"></param>
		/// <param name="txt"></param>
		public static List<int> KMPSearch(string pattern, string text)
		{
			List<int> result = new List<int>();

			int M = pattern.Length;
			int N = text.Length;

			int[] lps = new int[M];
			int j = 0;

			Build_LPS_Array(pattern, M, lps);

			int i = 0;
			while (i < N)
			{
				if (pattern[j] == text[i])
				{
					j++;
					i++;
				}
				if (j == M)
				{
					result.Add(i - j);
					j = lps[j - 1];
				}

				else if (i < N && pattern[j] != text[i])
				{
					if (j != 0)
					{
						j = lps[j - 1];
					}
					else
					{
						i = i + 1;
					}
				}
			}

			return result;
		}

		/// <summary>
		/// 构造 LPS 数组
		/// 最长后缀数组,Longest Proper Suffix 
		/// </summary>
		/// <param name="pattern"></param>
		/// <param name="M"></param>
		/// <param name="lps"></param>
		private static void Build_LPS_Array(string pattern, int M, int[] lps)
		{
			lps[0] = 0;
			int len = 0;
			int i = 1;
			while (i < M)
			{
				if (pattern[i] == pattern[len])
				{
					len++;
					lps[i] = len;
					i++;
				}
				else
				{
					if (len != 0)
					{
						len = lps[len - 1];
					}
					else
					{
						lps[i] = len;
						i++;
					}
				}
			}
		}
	}
}

PatternSearch 类的另外一个 暴力算法,写在:

文件2、Legalsoft.Truffer.PatternSearch.Native.cs

using System;
using System.Collections;
using System.Collections.Generic;

namespace Legalsoft.Truffer.Algorithm
{
	/// <summary>
	/// 字符串匹配(模式搜索)算法集锦
	/// </summary>
	public static partial class PatternSearch
	{
		/// <summary>
		/// 字符串匹配的暴力算法
		/// </summary>
		/// <param name="text"></param>
		/// <param name="pattern"></param>
		/// <returns></returns>
		public static List<int> NativeSearch(string text, string pattern)
		{
			int M = pattern.Length;
			int N = text.Length;
			int S = N - M;

			List<int> matchs = new List<int>();
			if (S <= 0) return matchs;
			for (int i = 0; i <= S; i++)
			{
				int j = 0;
				while (j < M)
				{
					if (text[i + j] != pattern[j])
					{
						break;
					}
					j++;
				}

				if (j == M)
				{
					matchs.Add(i);
				}
			}

			return matchs;
		}
	}
}

 ——————————————————————

POWER BY 315SOFT.COM &
TRUFFER.CN