200 likes | 295 Views
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
E N D
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
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
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() ??
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
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
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()
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
OOP Concepts - Polymorphism • The exact method being called is determined at run-time • Run-time binding • Use keyword “virtual” • Abstract method – implicitly virtual
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
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
Course Project • The Form consists of 2 .cs files • Form1.cs (Right click “Form1.cs View Code) • Form1.Designer.cs
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
Course Project • Form1.cs • Write your own code here • Form1.Designer.cs • Generate automatically (Refer to Lecture Note P.233) • InitializeComponent() • Dispose()
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);
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); }
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
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
Course Project • Double-click the timer icon • timer1_Tick() executes every “Interval” ms Add your code here