Presentation is loading. Please wait.

Presentation is loading. Please wait.

IEG3080 Tutorial 3 Prepared by Ryan. Outline Object Oriented Programming Concepts Encapsulation Inheritance Polymorphism Delegation Course Project.

Similar presentations


Presentation on theme: "IEG3080 Tutorial 3 Prepared by Ryan. Outline Object Oriented Programming Concepts Encapsulation Inheritance Polymorphism Delegation Course Project."— 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 DerivedClass BaseClass2 Print() BaseClass1 Print() 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 MyAbsClass { abstract protected void AbsMethod(); public void ImpMethod() { Console.WriteLine(“Run”); } interface IMyInterface1 { void Interface1Method(); } Interface IMyInterface2 { void Interface2Method(); } 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”); } multiple interfaces abstract class 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() Dog Speak() Cat 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 Properties You can set the properties of the form, such as Background Colour and Text Solution Explorer You can see all the files in the project Create a “Window Application” project

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 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); protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; g.FillEllipse(Brushes.Blue, 0, 0, 10, 10); }

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 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 Drag and drop the Timer from Toolbox to the Form

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


Download ppt "IEG3080 Tutorial 3 Prepared by Ryan. Outline Object Oriented Programming Concepts Encapsulation Inheritance Polymorphism Delegation Course Project."

Similar presentations


Ads by Google