...

View Full Version : Anyone to explain this?



Dev Hawk
01-03-2013, 06:54 PM
I didn't understand how does it work.

class Sample
{
public static void fun1()
{
Console.WriteLine("fun1");
}
public void fun2()
{
Console.WriteLine("fun2");
}
public void fun2(int i)
{
Console.WriteLine(i); fun2();
}
class MyProgram
{
static void Main(string[] args)
{
Sample s = new Sample();
Sample.fun1();
s.fun2(123);
}
}
}
}

Output
fun1
123
fun2

alykins
01-03-2013, 08:28 PM
Class 'Sample' is doing all the work... the class 'MyProgram' is calling the shots. let's look @ 'MyProgram'


Sample s = new Sample();


this is creating a new object of the class 'Sample'. Since there is no constructor telling it to do anything, it is just going to sit there and await instruction.



Sample.fun1();

try to access fun1 via 's'.... you can't (ie s.fun1() will not compile). To access the static method you use the class name.method, in this case "Sample.fun1();"
and finally


s.fun2(123);


this is saying use the method "fun2" that is part of the object "s" (remember he is sitting there waiting instruction). The method "fun2" accepts an integer as an argument (fun2( int i) ) and ypu are passing it the int '123'... then after it writes this to the console, it calls another method within the class "fun2()" (notice without any arguments) and then executes that function. IMO that is really sloppy, it shouldn't be on one line like that... again my opinion it should look more like


class MyProgram
{
static void Main(string[] args)
{
Sample s = new Sample();
Sample.fun1();
s.fun2(123);

Console.ReadLine();
}
}

class Sample
{

public static void fun1()
{
Console.WriteLine("fun1");
}

public void fun2()
{
Console.WriteLine("fun2");
}

public void fun2(int i)
{
Console.WriteLine(i);
fun2();
}
}
}

See how much easier it is to read? Also note the readline() is so you can see the output.

ahamadhussain
01-17-2013, 01:49 PM
Hai Dave Hawk..

class Sample
{
public static void fun1() \\ Step 3: fun1() function definition.
{
Console.WriteLine("fun1"); \\ Step 4: Print the first output:"fun1". then go to main function
}
public void fun2() \\ Step 8: fun2() function definition
{
Console.WriteLine("fun2"); \\ step 9: print the Third output:"fun2"
}
public void fun2(int i) \\ Step 6:fun2(123) function definition.
{
Console.WriteLine(i); fun2(); \\ Step 7: Print the value Second output:"123" then callling the fun2() function(passing without parameter)
}
class MyProgram
{
static void Main(string[] args)
{
Sample s = new Sample(); \\ Step 1: create a object s.(i.e class name:Sample object name:s)
Sample.fun1(); \\ Step 2: calling the fun1() fuction. Calling based on classname
s.fun2(123); \\ Step 5: objectname(s) s.fun2(123) . calling based on objectname.(passing parameter)
}
}
}
}

The following steps are how to the above program executed:

Step 1: create a object s.(i.e class name:Sample object name:s)
Step 2: calling the fun1() fuction. Calling based on classname
Step 3: fun1() function definition.
Step 4: Print the first output:"fun1". then go to main function
Step 5: objectname(s) s.fun2(123) . calling based on objectname.(passing parameter)
Step 6:fun2(123) function definition.
Step 7: Print the value Second output:"123" then callling the fun2() function(passing without parameter)
Step 8: fun2() function definition
step 9: print the Third output:"fun2"

Output:
fun1
123
fun2



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum