Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-03-2013, 05:54 PM   PM User | #1
Dev Hawk
New to the CF scene

 
Join Date: Dec 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Dev Hawk is an unknown quantity at this point
Anyone to explain this?

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
Quote:
fun1
123
fun2
Dev Hawk is offline   Reply With Quote
Old 01-03-2013, 07:28 PM   PM User | #2
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
Class 'Sample' is doing all the work... the class 'MyProgram' is calling the shots. let's look @ 'MyProgram'
Code:
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.

Code:
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
Code:
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
Code:
    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.
__________________

I code C hash-tag .Net
Reference: W3C W3CWiki .Net Lib
Validate: html CSS
Debug: Chrome FireFox IE
alykins is offline   Reply With Quote
Old 01-17-2013, 12:49 PM   PM User | #3
ahamadhussain
New to the CF scene

 
Join Date: Jan 2013
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
ahamadhussain is an unknown quantity at this point
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
ahamadhussain is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:40 PM.


Advertisement
Log in to turn off these ads.