IEG3080 Tutorial 3 Prepared by Ryan. Outline Object Oriented Programming Concepts Encapsulation...

20
IEG3080 Tutorial 3 Prepared by Ryan
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    228
  • download

    0

Transcript of IEG3080 Tutorial 3 Prepared by Ryan. Outline Object Oriented Programming Concepts Encapsulation...

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

IEG3080 Tutorial 3

Prepared by Ryan

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

Outline

Object Oriented Programming Concepts Encapsulation Inheritance Polymorphism Delegation

Course Project

Page 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 Cannot be instantiated May have some implementations

Interface Cannot be instantiated Purely interface, does not contain implementation

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

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

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

OOP Concepts - Inheritance

Multiple Inheritance Diamond Problem Solution: Use Interface

DerivedClass

BaseClass2Print()

BaseClass1Print()

DerivedClass dc = new DerivedClass;

dc.Print();BaseClass1’s Print() or BaseClass2’s Print() ??

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

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

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

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 interfacesabstract class

implementation

no implementation in interface

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

OOP Concepts - Polymorphism

One Interface, Different implementations Interface Speak() in class Animal Implementation Speak() in class Dog and Cat

AnimalSpeak()

DogSpeak()

CatSpeak()

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

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:DogCat

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

OOP Concepts - Polymorphism

The exact method being called is determined at run-time Run-time binding

Use keyword “virtual” Abstract method – implicitly virtual

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

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

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

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

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

Course Project

The Form consists of 2 .cs files Form1.cs (Right click “Form1.cs View Code) Form1.Designer.cs

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

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

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

Course Project

Form1.cs Write your own code here

Form1.Designer.cs Generate automatically (Refer to Lecture Note P.

233) InitializeComponent() Dispose()

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

Course Project

Draw Circle Two ways

1) Override OnPaint method

2) Use Paint Event Handlerpublic 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);}

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

Course Project

Draw Imagepublic void GamePaintHandler(object sender, PaintEventArgs e){ Bitmap image = new Bitmap(“ball.gif”); Graphics g = e.Graphics; g.DrawImage(image, 0, 0);}

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

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

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

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

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

Course Project

Double-click the timer icon

timer1_Tick() executes every “Interval” ms

Add your code here