200 likes | 349 Views
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
E N D
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 • 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
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
Polymorphism class Shape : Object { public void Draw() { Console.WriteLine("Draw apolygon"); } public void Draw(string s) { Console.WriteLine("Draw a {0}",s); } };
Polymorphism Shapes1 = new Shape(); s1.Draw(); s1.Draw(“square"); //Output Draw apolygon Draw asquare
Polymorphism class NewShape : Shape { public void Draw(inti) { Console.WriteLine("Draw many polygon"); } };
Polymorphism Shapes1 = new NewShape(); s1.Draw(); s1.Draw(“square"); s1.Draw(1); //Output Draw apolygon Draw asquare Draw many polygon
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.
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 } }
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
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
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 } }
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]);
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(); } }}
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); } }
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); } }
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
Reference • http://www.akadia.com/services/dotnet_delegates_and_events.html • http://en.wikipedia.org/wiki/Delegation_(programming)
Office : Room 803 • Office Hour : Tuesdays 16:30 – 17:15