There is a simple console application in which the recursive method is written. In general, it looks like this
namespace somenamespace
{
Class Program
{
Static Void Main (String [] Args)
{
Recursive (...)
}
Static Void Recursie (...)
{
}
}
}
, respectively, the method refers to himself and during his work I need a variable that would preserve any value associated with this method. Suppose in one branch of the cycle in the method I increase it, to another, reducing and using the method during operation. What to do in this case?
Why if you declare this method without static, the program requires an instance? How to make this instance? If via New Program ()
, what will happen to void main
?
Answer 1, Authority 100%
, respectively, the method refers to itself and during his work I need a variable that would preserve any value associated with this method.
Add a static field and work with it:
namespace somenamespace
{
Class Program
{
Private static int value = 0;
Static Void Main (String [] Args)
{
Recursive (...)
}
Static Void Recursive (...)
{
Value ++;
}
}
}
Why if you declare this method without static, the program requires an instance? How to make this instance? If through New Program (), what will happen to Void Main?
Because of static methods without specifying the instance, only static methods can be called. The reason for this is an implicit method to an instance of an object that is called this method (in case of calling the static method of this reference).
In the simplest case, you can really make Recursive
instance and create an instance of Program
. With the Main
method, there will be nothing.
namespace somenamespace
{
Class Program
{
Private int value = 0;
Static Void Main (String [] Args)
{
VAR P = NEW PROGRAM ();
P.Recursive (...);
Console.WriteLine (P.Value);
}
Void Recursive (...)
{
Value ++;
}
}
}
Answer 2, Authority 100%
Why if you declare this method without static, the program requires an instance?
Ultimate method requires an instance because all non-static methods require an instance. I do not even know how to explain more. It is about how to explain why water is wet.
How to make this instance?
Object instances are usually created by the New
If via New Program (), what will happen to Void Main?
And what should happen to Void Main?
Answer 3, Authority 100%
What to do in this case?
You can declare a static field of the class Program
For example, so:
class program
{
Static int myfield;
Static Void Main (String [] Args)
{
Recursive (...)
}
Static Void Recursie (...)
{
// here the MyField field is available.
}
}
Why if you declare this method without
Static
, the program requires an instance?
Answer: By definition. On the household level, I can formulate it like this:
-
Appeal to static methods is carried out by name type. Example:
string s = "..."; BOOL B = string.isnullLempTy (s); // & lt; - Calling a static method `string.isnullLorempty`
-
Appeal to the non-static methods is carried out by reference to a type of type. Example:
string s = "..."; String T = s.trim (); // & LT; - Calling a non-Static method for an instance of `s`
How to make this instance?
The instance is always created using the new
statement. However, in some cases, this operator may not appear in the code explicitly. For example:
int i = 0;
String S = "...";
double [] d = {0.0, 1.0, 1.5};
List is not complete.
Answer 4, Authority 100%
disagree with the tips that offer to drag the state through the fields.
Similar side effects, especially at the level of type, and not an object – it is bad in terms of understanding of the inputs / output of the method.
After all, no one canceled the classics. The recursive method usually pulls the state through the arguments of the method. That way we get stateful programming in FP.
Here is true, the problem appears. Falling in the condition it says that the method must return void, then this means that it must either output something on the screen (or to otherwise show the result of his work) or to change some other way the state of the outside world (write something In the database, or change the static field).
static void recurse (int state)
{
// First Boundary Condition
if (state == 0)
{
Console.WriteLine ("Everyone, arrived!");
Return;
}
// First way to reduce the problem
If (State & LT; 10)
{
Console.WriteLine ("Recurse. State: {0}", State);
Recurse (State - 1);
Return;
}
Console.WriteLine ("State is too big! Reduce it by 10!");
Recurse (State - 10);
}
Void Main ()
{
Recurse (25);
}
State too big! Reduce it to 10!
State too big! Reduce it to 10!
Recurse. State: 5.
Recurse. State: 4.
Recurse. State: 3.
Recurse. State: 2.
Recurse. State: 1.
All came!