De presentatie wordt gedownload. Even geduld aub

De presentatie wordt gedownload. Even geduld aub

Hoofdstuk 8 Objecten en klassen.

Verwante presentaties


Presentatie over: "Hoofdstuk 8 Objecten en klassen."— Transcript van de presentatie:

1 Hoofdstuk 8 Objecten en klassen

2 Programma en Geheugen opdrachten variabelen methoden objecten klasse
veranderen zijn gegroepeerd in zijn gegroepeerd in methoden objecten bewerken zijn gegroepeerd in hebben als type klasse klasse klasse

3 Klasse-definitie Opsomming van methoden ieder bestaand uit opdrachten
class Teller : Form { int t; Teller() { } Opsomming van methoden ieder bestaand uit opdrachten Paint += teken; MouseClick+=klik; t = 0; ... bewerken ... teken(...) { } g.DrawString(t...); Beschrijving van objecten door declaraties van variabelen ... klik(...) { } t = t+1; this.Invalidate(); }

4 Declaratie Declaratie reserveert geheugenruimte
Voor een primitieve waarde zoals be- schreven in struct Size int x; x 5 x = 5; zoals be- schreven in klasse Button Voor een object-waarde s Size s; w 10 s = new Size(10,20); h 20 Voor een object-verwijzing Button b; b b = new Button();

5 Creatie van nieuwe objecten
Objecten met als type een library-klasse type van het gewenste object Button b; b = new Button( ); Objecten met als type een zelfgemaakte klasse CirkelGroei cg; cg = new CirkelGroei( );

6 Creatie van objecten Opbouw van object is gespecificeerd in de klasse
class CirkelGroei { Opbouw van object is gespecificeerd in de klasse : Form int straal; Button kleiner , groter; // methoden... cg clientsize geërfd paint text controls } zelf gedeclareerd straal kleiner groter

7 Constructor CirkelGroei( ) { }
this // roept automatisch // constructor van // Form aan clientsize paint text controls null null } straal kleiner groter null null cs-cirkelobjecten

8 Constructor CirkelGroei( ) { }
this // roept automatisch // constructor van // Form aan this.straal = 100; clientsize paint this.ClientSize = new Size(300,300); 500 300 text controls 500 } 300 straal kleiner groter 100 null null cs-cirkelobjecten

9 Constructor CirkelGroei( ) { } … this.Text = "CirkelGroei"; 500 300
clientsize paint 500 300 text controls 500 } 300 straal kleiner groter 100 null null cs-cirkelobjecten

10 Constructor CirkelGroei( ) { } … this.Text = "CirkelGroei";
this.Paint += this.teken; clientsize paint 500 300 text controls teken() {…} ...new Button()... 500 } 300 straal kleiner groter 100 null null location text click cs-cirkelobjecten

11 Constructor CirkelGroei( ) { } … this.kleiner = new Button();
this.groter = new Button(); clientsize paint 500 300 text controls teken() {…} 500 } 300 straal kleiner groter 100 null location click text cs-cirkelobjecten

12 Constructor CirkelGroei( ) { } … this.kleiner = new Button();
this.groter = new Button(); clientsize paint 500 300 text controls teken() {…} 500 } 300 straal kleiner groter 100 location location click click text text cs-cirkelobjecten

13 Constructor CirkelGroei( ) { } … this.kleiner.Text = "Kleiner";
this.groter.Text = "Groter"; clientsize paint 500 300 text controls teken() {…} 500 } 300 straal kleiner groter 100 location location click click text text cs-cirkelobjecten K l e i n r G r o t e

14 Constructor CirkelGroei( ) { } …
this this.kleiner.Location = new Point(30,20); C i r k e l G o this.groter.Location = new Point(200,20); clientsize paint 500 300 text controls teken() {…} 500 } 300 straal kleiner groter 100 location location click click 30 text 200 text cs-cirkelobjecten 20 20 K l e i n r G r o t e

15 Constructor CirkelGroei( ) { } … this.kleiner.Click = this.klik;
this.groter.Click = this.klik; clientsize paint 500 300 text controls teken() {…} 500 } 300 straal kleiner groter 100 location location click click 30 text 200 text cs-cirkelobjecten 20 20 K l e i n r G r o t e klik() {…}

16 Constructor CirkelGroei( ) { } … this.Controls.Add (this.groter);
this.Controls.Add (this.kleiner); clientsize paint 500 300 text controls teken() {…} 500 } 300 straal kleiner groter 100 location location click click 30 text 200 text cs-cirkelobjecten 20 20 K l e i n r G r o t e klik() {…}

17 Klassen en objecten Klasse-definitie in library Object-creatie in constructor Button Klasse-definitie in programma Object-creatie in Main CirkelGroei Klasse-definitie in programma Object-creatie in constructor Ruimte

18 Voorbeeld: Simulatie van bewegende deeltjes
Simulatie - object Ruimte - objecten Deeltje - objecten Button - objecten

19 De klasse Simulatie class Simulatie : Form { Button stap, auto;
Ruimte r1, r2, r3; Simulatie( ) { stap = new Button(); stap.Text = "stap"; auto = new Button(); auto.Text ="start"; r1 = new Ruimte(); r2 = new Ruimte(); r3 = new Ruimte(); ... } Klasse Ruimte gaan we zelf maken!

20 heeft drie eigen Deeltje - objecten
De klasse Ruimte Ruimte is een uitbreiding van een bestaande Control class Ruimte { } : UserControl Deeltje d1, d2, d3; ieder Ruimte -object heeft drie eigen Deeltje - objecten // methoden nog toevoegen

21 om het deeltje te tekenen
De klasse Deeltje Deeltje is helemaal zelfgemaakt class Deeltje { } positie van het Deeltje int x, y; int dx, dy; snelheid van het Deeltje Brush brush; // methoden nog toevoegen om het deeltje te tekenen

22 Samenhang van objecten
Simulatie Samenhang van objecten Button geërfd van Form zelf gedeclareerd r1 r2 r3 stap auto sim Button d1 d2 d3 Ruimte d1 d2 d3 Ruimte d1 d2 d3 Ruimte geërfd van UserControl x y dx dy brush Deeltje x y dx dy brush Deeltje x y dx dy brush Deeltje

23 Constructie van nieuw object
new-expressie doet twee dingen: r1 = new Ruimte ( ) ; r1 Geheugenruimte klaarzetten d1 d2 d3 Ruimte 100 200 Constructormethode aanroepen waarde is pijl naar het nieuwe object die je kunt opslaan

24 naam is hetzelfde als van de klasse
Constructormethode naam is hetzelfde als van de klasse class Ruimte : UserControl { Deeltje d1, d2, d3; Ruimte ( ) { } geen resultaat- type! this . BackColor = Color.LightYellow; this . Paint += this.tekenRuimte; d1 = new Deeltje (...); d2 = new Deeltje (...); d3 = new Deeltje (...); geërfd van Canvas

25 De klasse Deeltje class Deeltje { int x, y, dx, dy; Brush brush;
} int x, y, dx, dy; Brush brush; public Deeltje (...) {...} public void DoeStap (...) {...} public void TekenDeeltje(Graphics g ) {...}

26 Methoden van klasse Deeltje
class Deeltje { } int x, y, dx, dy; Brush brush; public void TekenDeeltje ( Graphics gr) { gr . FillEllipse( this.brush , this.x – 4, this.y – 4 , 9 , 9 ); }

27 Methoden van klasse Deeltje
class Deeltje { } int x, y, dx, dy; Brush brush; public void DoeStap ( ... ) { this.x += this.dx; this.y += this.dy; if (this.x<0) { } this.x = – this.x; this.dx = – this.dx; if (this.y<0) { ... } if (this.x>maxX) { ... } if (this.y>maxY) { ... }

28 Overzicht van klassen class Simulatie : Form
stap_Click, auto_Click(object o, EventArgs ea) class Ruimte : UserControl Ruimte( ) TekenRuimte( object o, PaintEventArgs pea ) DoeStap( ) class Deeltje Deeltje( ... ) TekenDeeltje( Graphics g ) DoeStap( Size s )

29 Methoden van klasse Simulatie
class Simulatie : Form { } Button stap, auto; Ruimte r1, r2, r3; void stap_Click(object o, EventArgs ea ) { r1 . DoeStap( ); r2 . DoeStap( ); r3 . DoeStap( ); }

30 Methoden van klasse Ruimte
class Ruimte : UserControl { } Deeltje d1, d2, d3; void TekenRuimte(object o, PaintEventArgs pea ) { Graphics gr = pea.Graphics; d1.TekenDeeltje(gr); d2.TekenDeeltje(gr); d3.TekenDeeltje(gr); } void DoeStap( ) { d1.DoeStap(this.Size); d2.DoeStap(this.Size); d3.DoeStap(this.Size); this.Invalidate(); }


Download ppt "Hoofdstuk 8 Objecten en klassen."

Verwante presentaties


Ads door Google