关于static修饰一个类中的成员函数的问题

静态方法与成员变量

面向对象语言中为什么会有固定在类中的函数–STATIC

这个代码片中的method方法是Myclass类中定义的static成员方法

 class Myclass
 {
     public string method(int i)
     {
         string[] str = { "This is a method", "This is the second string" };
         string ret = null;
         if (1 == i)
         {
             ret = str[0];
             Console.WriteLine("hahaha1" + str[0]);
         }
         else
         {
             ret = str[1];
             Console.WriteLine("hahaha2" + str[1]);
         }
         return ret;
     }
 }
static void Main(string[] args)
{
    ConsoleKeyInfo key;
    Myclass entity = new Myclass();
    for (int i = 0; i < 20; i++)
    {
        key = Console.ReadKey();
        switch (key.Key)
        {
            case ConsoleKey.A:
                Console.WriteLine("This is " + ConsoleKey.A);
                Myclass.method(1);
                break;
            case ConsoleKey.B:
                Console.WriteLine("This is " + ConsoleKey.B);
                Myclass.method(2);
                break;
            default:
                Console.WriteLine("This is default");
                break;
        }
    }
}

这段代码片去掉了static的修饰,只有实例化entity之后才能够调用Myclass类的方法

 class Myclass
 {
     public string method(int i)
     {
         string[] str = { "This is a method", "This is the second string" };
         string ret = null;
         if (1 == i)
         {
             ret = str[0];
             Console.WriteLine("hahaha1" + str[0]);
         }
         else
         {
             ret = str[1];
             Console.WriteLine("hahaha2" + str[1]);
         }
         return ret;
     }
 }
static void Main(string[] args)
{
    ConsoleKeyInfo key;
    Myclass entity = new Myclass();
    for (int i = 0; i < 20; i++)
    {
        key = Console.ReadKey();
        switch (key.Key)
        {
            case ConsoleKey.A:
                Console.WriteLine("Thi is " + ConsoleKey.A);
                entity.method(1);
                break;
            case ConsoleKey.B:
                Console.WriteLine("This is " + ConsoleKey.B);
                entity.method(2);
                break;
            default:
                Console.WriteLine("This is default");
                break;
        }
    }
}

上面是两个现象,先记录在这个博客中,等有一定的编码经验之后再回头进行解释


17/03/22

紧接着上面的问题我们做一些补充

  • 我们做了一些小小的探究,似乎发现了程序语言的设计者为什么要定义这样一个关键字
  • 我们试想这样一个场景,某理工大学食堂只有100个苹果,厨师做菜品A的时候要使用一个苹果,厨师做菜品B的时候要使用两个苹果。场景介绍到这里,我们开始对上面的场景进行简单的建模。
  • 这一百个苹果应该是属于某理工大学食堂的。那么这100个苹果就作为和食堂“同生共死”的兄弟。我们为这个食堂建立一个面向对象编程概念中的类,其中有一个成员叫做“苹果数量”(appleNum)。
/*一个流弊的食堂类*/
class AppleClass
{
    public static int appleNum = 100;

    public int Eatapple(int remnant)
    {
        appleNum -= remnant;
        return remnant;
    }
}
  • 在这里我们新建了一个实体,这个实体来吃这个类中的苹果。可以看到这样的建模是适合这样的场景的。
/*主程序*/
ConsoleKeyInfo key;
AppleClass entity = new AppleClass();
while (true)
{
    key = Console.ReadKey();
    switch (key.Key)
    {
        case ConsoleKey.A:
            entity.Eatapple(1);
            Console.WriteLine("吃了一个苹果");
            Console.WriteLine("苹果的总数" + AppleClass.appleNum);
            break;
        case ConsoleKey.B:
            entity.Eatapple(2);
            Console.WriteLine("吃了两个苹果");
            Console.WriteLine("苹果的总数" + AppleClass.appleNum);
            break;
        default:
            Console.WriteLine("苹果的总数" + AppleClass.appleNum);
            Console.WriteLine("This is default");
            break;
    }
}

cont’d

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值