1 / 20

IEG3080 Tutorial 3

IEG3080 Tutorial 3. Prepared by Ryan. Outline. Object Oriented Programming Concepts Encapsulation Inheritance Polymorphism Delegation Course Project. OOP Concepts - Inheritance. More about Inheritance Interface Reuse Program to an interface, not an implementation Abstract Class

Download Presentation

IEG3080 Tutorial 3

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 3 Prepared by Ryan

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

  3. OOP Concepts - Inheritance • More about Inheritance • Interface Reuse • Program to an interface, not an implementation • Abstract Class • Cannot be instantiated • May have some implementations • Interface • Cannot be instantiated • Purely interface, does not contain implementation

  4. OOP Concepts - Inheritance • Abstract Class • Represent a base class, but not want to create a object of this class • Its member can • be abstract or not abstract • be static (not for an abstract method) • have different access modifiers (e.g. public, protected) • An abstract method cannot be private

  5. OOP Concepts - Inheritance • Multiple Inheritance  Diamond Problem • Solution: Use Interface BaseClass1 Print() BaseClass2 Print() DerivedClass DerivedClass dc = new DerivedClass; dc.Print(); BaseClass1’s Print() or BaseClass2’s Print() ??

  6. OOP Concepts - Inheritance • Interface • Its members are all public • All members must override to its derived class • A class can inherit one or more interfaces, but only one (abstract) class

  7. OOP Concepts - Inheritance abstract class multiple interfaces class MyClass : MyAbsClass, IMyInterface1, IMyInterface2 { public MyClass(){} public override void AbsMethod() { Console.WriteLine(“AbsMethod”); } public void Interface1Method { Console.WriteLine(“Interface1Method”); } public void Interface2Method { Console.WriteLine(“Interface2Method”); } } abstract class MyAbsClass { abstract protected void AbsMethod(); public void ImpMethod() { Console.WriteLine(“Run”); } } interface IMyInterface1 { void Interface1Method(); } Interface IMyInterface2 { void Interface2Method(); } implementation no implementation in interface

  8. OOP Concepts - Polymorphism • One Interface, Different implementations • Interface  Speak() in class Animal • Implementation  Speak() in class Dog and Cat Animal Speak() Cat Speak() Dog Speak()

  9. OOP Concepts - Polymorphism class Animal { public virtual void Speak() { Console.WriteLine(“Animal”); } } class Dog : Animal { public override void Speak() { Console.WriteLine(“Dog”); } } class Cat : Animal { public override void Speak() { Console.WriteLine(“Cat”); } } class Test { public static void main() { Animal[ ] animals = new Animal[2]; animals[0] = new Dog(); animals[1] = new Cat(); foreach (Animal a in animals) { a.Speak(); } } } Output: Dog Cat

  10. OOP Concepts - Polymorphism • The exact method being called is determined at run-time • Run-time binding • Use keyword “virtual” • Abstract method – implicitly virtual

  11. Course Project • Design the whole program structure before implementation • Phase 1 • Draw the main character • Implement one bouncing ball • Use a container to store the ball • Deadline : 23 Feb 2007 • No Plagiarism

  12. Course Project • Create a “Window Application” project Solution Explorer You can see all the files in the project Properties You can set the properties of the form, such as Background Colour and Text

  13. Course Project • The Form consists of 2 .cs files • Form1.cs (Right click “Form1.cs  View Code) • Form1.Designer.cs

  14. Course Project • Keyword “partial” is used to split the definition of a class over 2 or more source files (“partial” can also be used for struct or interface) • Form1.cs • public partial class Form1 : Form • Form1.Designer.cs • partial class Form1

  15. Course Project • Form1.cs • Write your own code here • Form1.Designer.cs • Generate automatically (Refer to Lecture Note P.233) • InitializeComponent() • Dispose()

  16. Course Project • Draw Circle • Two ways • 1) Override OnPaint method • 2) Use Paint Event Handler protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; g.FillEllipse(Brushes.Blue, 0, 0, 10, 10); } public void GamePaintHandler(object sender, PaintEventArgs e) { Graphics g = e.Graphics; g.FillEllipse(Brushes.Blue, 0, 0, 10, 10); } Add in InitializeComponent() this.Paint += new System.Windows.Forms.PaintEventHandler(this.GamePaintHandler);

  17. Course Project • Draw Image public void GamePaintHandler(object sender, PaintEventArgs e) { Bitmap image = new Bitmap(“ball.gif”); Graphics g = e.Graphics; g.DrawImage(image, 0, 0); }

  18. Course Project • Timer • Tasks can be scheduled for repeated execution at regular intervals by using Timer • Examples for its usage • Update the position of the ball at a constant interval • Redraw the graphics at a constant interval

  19. Course Project Drag and drop the Timer from Toolbox to the Form Properties You can set the properties of the Timer such as Enabled and Interval You should set the Enabled value to “True” to enable the Timer

  20. Course Project • Double-click the timer icon • timer1_Tick() executes every “Interval” ms Add your code here

More Related