1 / 20

IEG3080 Tutorial 4

IEG3080 Tutorial 4. Prepared by KK. Outline. Object Oriented Programming Concepts Encapsulation Inheritance Polymorphism Delegation Course Project. Polymorphism. What is it? one name, many forms

feryal
Télécharger la présentation

IEG3080 Tutorial 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. IEG3080 Tutorial 4 Prepared by KK

  2. Outline • Object Oriented Programming Concepts • Encapsulation • Inheritance • Polymorphism • Delegation • Course Project

  3. Polymorphism • What is it? • one name, many forms • future extension in the form of new types of objects is easy, if the new objects conform to the original interface. • How to do? • Method overloading • Method overriding through inheritance • Method overriding through the C# interface

  4. Polymorphism • Method overloading • compile-time polymorphism • have the same name, but have different formal argument lists • within a class definition • within the class inheritance hierarchy

  5. Polymorphism class Shape : Object { public void Draw() { Console.WriteLine("Draw apolygon"); } public void Draw(string s) { Console.WriteLine("Draw a {0}",s); } };

  6. Polymorphism Shapes1 = new Shape(); s1.Draw(); s1.Draw(“square"); //Output Draw apolygon Draw asquare

  7. Polymorphism class NewShape : Shape { public void Draw(inti) { Console.WriteLine("Draw many polygon"); } };

  8. Polymorphism Shapes1 = new NewShape(); s1.Draw(); s1.Draw(“square"); s1.Draw(1); //Output Draw apolygon Draw asquare Draw many polygon

  9. Polymorphism • Method overriding • runtime polymorphism • The decision cannot be made at compile time • The version of the method that was executed was based on the actual type of the object, and not on the type of the reference.

  10. Polymorphism class A{   public virtual void m(){     System.Console.WriteLine("m in class A");   } } class B : A{   public override void m(){     System.Console.WriteLine("m in class B");   } } public class Test{   public static void TestMain(){     Object var = new B(); ((B)var).m();    //m in class B ((A)var).m();    //m in class B    //var.m();   //will not compile       } }

  11. Reference • http://www.dickbaldwin.com/tocCsharp.htm • http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming • http://msdn2.microsoft.com/en-us/vcsharp/aa336809.aspx • http://msdn2.microsoft.com/en-us/library/ms173152(VS.80).aspx

  12. Delegation • What is it? • take place at run-time • pass control to some other object through a delegation link, not an object reference • similar to a function pointer in C or C++ • encapsulate a reference to a method inside a delegate object

  13. Delegation •     public class A    {        public void Process()        {            Console.WriteLine("Process() begin");            Console.WriteLine("Process() end");        }    }    public class B    {        static void Process()        {            A a1 = new A();            a1.Process(); //no delegation        }    }

  14. Delegation • A delegate will allow us to specify what the function we'll be calling looks like without having to specify which function to call • delegate result-type identifier ([parameters]);

  15. Delegation • namespace Tutorial.BasicDelegate{    // Declarationpublic delegate void SimpleDelegate();    class A    {        public static void foo()        {            Console.WriteLine(“Delegate...");        }        public static void Main()        {            // InstantiationSimpleDelegate simpleDelegate = new SimpleDelegate(foo);            // InvocationsimpleDelegate();        }    }}

  16. Delegation     // Delegate Specification    public class A    {        // Declarationpublic delegate void ADelegate(string s);        // Checking         public void Process(ADelegate aDelegate)        {            if (aDelegate != null){                aDelegate(“Delegate…");            }        }    }public class B     {        // Constructor        public B(string s){}        // Member Function which is used in the Delegate        public void Func(string s)        {            Console.WriteLine(“{0}“,s);        }    }

  17. Delegation     public class TestApplication    {        static void Main(string[] args)        {            B b1 = new B(“test");            A a1 = new A();            // Crate an instance of the delegate, pointing to the Logger()            // function on the fl instance of a FileLogger.            A.ADelegate aDelegate = new A.ADelegate(b1.Func);            A.Process(aDelegate);        }    }

  18. Delegation • classes are indepentdently of one another, which makes for code that is easier to maintain • any number of classes can be notified when an event is raised

  19. Reference • http://www.akadia.com/services/dotnet_delegates_and_events.html • http://en.wikipedia.org/wiki/Delegation_(programming)

  20. Office : Room 803 • Office Hour : Tuesdays 16:30 – 17:15

More Related