C#同一项目中,类的相互调用

来源:互联网 发布:oracle数据库有可视化 编辑:程序博客网 时间:2024/06/05 08:36

主要功能:在用一个项目内,类之间的相互调用,从而实现字符拷贝的功能;

软件环境:visual studio 2015;

在program类中:using System;namespace MyStrcpy{    class Program    {        static void Main(string[] args)        {            char[] Strsnd = { 'h', 'e', 'l', 'l', 'o' };    //定义目标字符数组;            char[] Strrcv = new char[Strsnd.Length];    //定义目的字符数组;            int Cpy_bck = StrDst.StrCpy(Strsnd, Strsnd.Length, Strrcv, Strrcv.Length);    //调用StrDst类中的StrCpy;            if(Cpy_bck < 0)            {                Console.WriteLine("cpy fault");                return ;            }            for(int i = 0;i < Strrcv.Length;i++)    //循环打印目的字符串            {                Console.Write(Strrcv[i]);            }            Console.WriteLine();            Console.ReadKey();        }    }}

在StrDst类中:using System;namespace MyStrcpy{    public class StrDst    //在类前加public,从而使其能够被外部调用,默认为private;    {        public static int StrCpy(char[] dst, int dst_len, char[] apt, int apt_len)    //在StrCpy前加public static使其成为静态共有方法,能够被类外调用        {            if(dst_len > apt_len)            {                return -1;            }            for(int i = 0;i < dst_len;i++)            {                apt[i] = dst[i];            }                        return 0;                    }    }}

运行结果: