为什么这种方法不增加? [英] Why this method does not increment?

查看:64
本文介绍了为什么这种方法不增加?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能我昨晚要喝很多酒。为什么每次在getTurns中收到值1时,此方法不会将数字的值增加1?





 class转
{
private int number;

public void Counts(int getTurn,out int getBack)
{
if(getTurn.Equals(1))
{
number ++;
}

getBack = number;

}
}

解决方案

@Millone,请阅读评论!



Manfred R. Bihy你应该关心实例字段初始化是对的。请阅读这些文章:

10.4。 5变量初始值设定项 [ ^ ]

10.4.5.1静态字段初始化 [ ^ ]

10.4.5.2实例字段初始化 [ ^ ]

实例构造函数(C#编程指南) [ ^ ]

静态构造函数(C#编程指南) [ ^ ]

静态类和静态类成员(C#编程指南) [ ^ ]

访问修饰符(C#编程指南) [ ^ ]



Millone写道:

问题是我基本上是从Turns类的两个不同实例调用Count方法。当number是一个实例字段时,它将从每个实例的0开始。但是,当我将它更改为静态时,它将从整个类开始为0,并且每个实例调用Count方法时将增加1。





可能,你不需要打电话给你班级的另一个实例,但你不知道如何访问现有的实例...

3.7范围(C#) [ ^ ]



我建议你阅读代表 [ ^ ]和界面 [ ^ ]。

使用代理或/和接口,你可以增加计数你想要的次数,只使用你班级的一个实例。



糟糕的做法总是不好的做法,无论结果如何(工作申请) !


这是如何实现的? int类型的变量号从未被初始化。

我很谦卑并且通过下面的评论进行了更正,但我仍然认为未初始化的值和在较小程度上也引用了类中的类型成员绝对没有去。我总是指定一个值,即使它看起来有点冗长,甚至连初学者都不会猜测实际发生了什么。



我的两分钱!



问候,

Manfred


试试这个

  class 
{
public 对象计数( int count)
{
if (count == 1
{
count + = 1 ;
}
返回计数;
}
}


May be I had to much drinks last night. Why this method does not increment the value of "number" by 1 every time I receive the value "1" in "getTurns"?


class Turns
    {
        private int number;

        public void Counts(int getTurn, out int getBack)
        {
            if (getTurn.Equals(1))
            {
                number++;
            }
           
            getBack = number;

        }
    }

解决方案

@Millone, please, read comments!

Manfred R. Bihy is right that you should care about instance field initialization. Please, read these articles:
10.4.5 Variable initializers[^]
10.4.5.1 Static field initialization[^]
10.4.5.2 Instance field initialization[^]
Instance Constructors (C# Programming Guide)[^]
Static Constructors (C# Programming Guide)[^]
Static Classes and Static Class Members (C# Programming Guide)[^]
Access Modifiers (C# Programming Guide)[^]

Millone wrote:

The problem was that I was basically calling the Count method from two different instances of the Turns class. When number was an instance field, it would start off at 0 for each instance. However, when I changed it to static, it would start off at 0 for the class as a whole and would be incremented by 1 as each instance called the Count method.



Probably, you don''t need to call another instance of your class, but you don''t know how to access to existing one...
3.7 Scopes (C#)[^]

I would suggest you to read about delegates[^] and interfaces[^].
Using delegates or/and interfaces you''ll be able to increase Counts value as many times as you wish, using only the one instance of your class.

Bad practice is always bad practice, no matter of result (working application)!


How does this even complile? The variable number of type int has never been initialized.
I stand humbled and corrected by the comments below, but I still think that uninitialized value and to some lesser degree also reference types in class members are an absolute no go. I always assign a value even if it may seem a bit verbose, not even the beginner is left guessing what is actually going on.

My two cents!

Regards,
Manfred


try this

class Turns
{
public object Counts(int count)
   { 
	if (count == 1) 
        {
	   count += 1;
	}
	return count;
   }
}


这篇关于为什么这种方法不增加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆