首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

帮忙看下这段程序,不让用对象,而直接用类解决方法

2012-05-07 
帮忙看下这段程序,不让用对象,而直接用类C# codeusing Systempublic class MyMath{public static long Ad

帮忙看下这段程序,不让用对象,而直接用类

C# code
using System;public class MyMath{    public static long Add(params int[] args)    {        int ctr=0;        long Answer=0;                for (ctr=0; ctr<args.Length;ctr++)        {            Answer +=args[ctr];        }        return Answer;    }        public static long Subtract(int arg1, int arg2)    {        long Answer=0;        Answer=arg1-arg2;        return Answer;    }}class MyAp{    public static void Main()    {            MyMath math=new MyMath();        long Result=0;                Result=math.Add(1,2,3);        Console.WriteLine("Add result is {0}", Result);                Result=math.Subtract(5,2);        Console.WriteLine("Subtract result is {0}",Result);    }}            


[解决办法]
Result = MyMath.Add(1,2,3);

[解决办法]
因为MyMath类里面的 Add、Subtract方法都是static就是说是静态方法。静态方法是不用实例化类直接 "类名.方法名"调用的...
[解决办法]
探讨
引用:

public static class MyMath

调用时直接用MyMath.add


我的类不是static啊

热点排行