View Single Post
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