Beispiel #1
0
        public static void Main(string[] args)
        {
            //			"Interfaces"
            Some s = new Some ();
            s.DoInform ();
            Some2 s2 = new Some2 ();
            s2.DoInform ();

            //			another example of interface
            Console.WriteLine ();
            CellPhone c = new CellPhone ();
            c.SwitchOn ();
            c.SwitchOff ();
            c.example ();
            c.example2 ();
            c.example3 ();
            c.VolumeUp ();
            c.VolumeDown ();
            c.PlugIn ();
            c.PlugOff ();

            //			another example2 of interface
            Console.WriteLine ();
            DBConnect d = new DBConnect ();
            d.DoInform ();
            d.GetVersion ();
            d.Dolog ();
            d.Connect ();
            /* So the conclusion of this materi is interface
             * has the same function like implement in Java
             * */

            //			"Polymorphism"
            Shape[] shapes = {new Square (5),new Rectangle (9, 4),new Square (12)};
            //item should be as Square type because it's an array of object
            foreach (Shape item in shapes) {
                Console.WriteLine (item.Area ());
            }

            //			Sealed classes
            //			Derived dm = new Derived ();
            //			d.GetType ();
            //
            //			"DEEP COPY VS SHALLOW COPY"

            /* Mengingat kembali pelajaran passing by value dan by reference
             * dimana jika menggunakan passing by value data asli tidak akan berubah
             * sedangkan jika menggunakan passing by reference maka data asli akan berubah
             *
             * Pointer: variabel bertipe int32(integer 32 bit direpresentasikan
             * dalam 8 digit heksadesimal) yang bersi/menyimpan alamat di memory*/
            Console.WriteLine ("\nthis is the example of shallow copy");
            Color col=new Color(23,42,223);
            MyObject obj1=new MyObject(123,"small",col);

            MyObject obj2=(MyObject)obj1.Clone();
            obj2.id+=1;
            obj2.size="big";
            obj2.col.red=255;
            Console.WriteLine (obj1);
            Console.WriteLine (obj2);
            //			We can see that the ids are different (23 vs 24).
            //			The size is different ("small" vs "big").
            //			But the red part of the color object is same
            //			for both instances (255). Changing member values
            //			of the cloned object (id, size)
            //			did not affect the original object.
            //			Changing members of the referenced object (col)
            //			has affected the original object too.
            //			In other words, both objects refer to
            //			the same color object in memory.

            //			To change this behaviour, we will do a deep copy next.
            Console.WriteLine ("\nthis is example of deep copy");
            Color2 col2=new Color2(23,42,223);
            MyObject2 obj1_2=new MyObject2(123,"small",col2);

            MyObject2 obj2_2=(MyObject2)obj1_2.Clone();
            obj2_2.id+=1;
            obj2_2.size="BIG";
            obj2_2.col.red=255;
            Console.WriteLine (obj1_2);
            Console.WriteLine (obj2_2);
        }
Beispiel #2
0
 public MyObject2(int id, string size, Color2 col)
 {
     this.id=id;
     this.size=size;
     this.col=col;
 }