Download - Hoofdstuk 2
![Page 1: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/1.jpg)
Hoofdstuk 2
Hallo, C# !
![Page 2: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/2.jpg)
Soorten programma’s Console-
applicatie
![Page 3: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/3.jpg)
Soorten programma’s Console-
applicatie Windows-
applicatie
![Page 4: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/4.jpg)
Soorten programma’s Console-
applicatie Windows-
applicatie Web-
applicatie
![Page 5: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/5.jpg)
Soorten programma’s Console-
applicatie Windows-
applicatie Web-
applicatie Game
![Page 6: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/6.jpg)
Soorten programma’s Console-
applicatie Windows-
applicatie Web-
applicatie Game
Applet
![Page 7: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/7.jpg)
Soorten programma’s Console-
applicatie Windows-
applicatie Web-
applicatie Game
Applet
App
![Page 8: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/8.jpg)
Opbouw broncode
Opdrachten omhet geheugente veranderen
Opdrachten zijn gegroepeerd inmethoden
Methoden zijn gegroepeerd inklassen
![Page 9: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/9.jpg)
en dat kunnen zelf ook weer aanroepenzijn van weer andere methodes...
dus de “waar was ik gebleven” administratie is best ingewikkeld!
Soorten opdrachten
Toekennings -opdracht:verander het geheugen
Aanroep van een andere methode:voer eerst de opdrachten in die methode uit, en ga daarna verder waar je gebleven was
![Page 10: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/10.jpg)
Voorbeeld C#-programma
één klasse
...met éénmethode
...met tweeopdrachten
accolades begrenzenklasse, resp. methode
using System;
class Hallo { static void Main ( ) { Console.WriteLine("Hallo!"); Console.ReadLine( ); }}
![Page 11: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/11.jpg)
Klasse- en methode-header
using System;
class Hallo { static void Main ( ) { Console.WriteLine("Hallo!"); Console.ReadLine( ); }}
naam:bedacht door
de programmeur
naam:bedacht door
de programmeur
één van demethodes moet
Main hetenMain
![Page 12: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/12.jpg)
Opdracht: methode-aanroep
opdrachten:aanroep van
anderemethoden
naam van demethode
altijdeen punt
overigedetails
klasse waaruit de methode komt
using System;
class Hallo { static void Main( ) { Console.WriteLine("Hallo!"); Console.ReadLine( ); }}
![Page 13: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/13.jpg)
Klasse-bibliotheken
library-klassenmag je
gebruiken...
als je ze maaraangeeft in
welke libraryze staan
using System;
class Hallo { static void Main( ) { Console.WriteLine("Hallo!"); Console.ReadLine( ); }}
![Page 14: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/14.jpg)
Methode-header en -aanroep
methode-header
methode-aanroep
using System;
class Hallo { static void Main( ) { Console.WriteLine("Hallo!"); Console.ReadLine( ); }}
![Page 15: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/15.jpg)
Syntax en semantiek
Syntax:de vorm van het programmacompilatieeenheid
using ;
klassedeclaratie
librarynaam
Semantiek:wat het programmabetekent
![Page 16: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/16.jpg)
Syntax van klasse-declaratie
klassedeclaratie
public private
class naam { member }
: naam
![Page 17: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/17.jpg)
Syntax van member
member
static
public private
naam
type
void ( ) blok
,
naamtype
![Page 18: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/18.jpg)
Syntax van blok
blok
declaratie
opdracht{ }
![Page 19: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/19.jpg)
Syntax van opdracht
opdracht
( )
,
;expressieklassenaam
objectexpressie
.
methodenaam
= expressie ;variabele
propertynaam +=
![Page 20: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/20.jpg)
Syntax van opdracht
opdracht
( )
,
;expressieklassenaam
objectexpressie
.
methodenaam
= expressie ;variabele
propertynaam +=
![Page 21: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/21.jpg)
Console-applicatie
using System;
class Hallo2 { static void Main ( ) { Console.WriteLine("Hallo!"); Console.ReadLine( );
}}
![Page 22: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/22.jpg)
Console-applicatie
using System;
class Hallo2 { static void Main ( ) { Console.WriteLine("Wat is je naam?"); naam = Console.ReadLine( );
}}
Toekennings-opdrachtgeeft een waardeaan een variabele
![Page 23: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/23.jpg)
Console-applicatie
using System;
class Hallo2 { static void Main ( ) { string naam; Console.WriteLine("Wat is je naam?"); naam = Console.ReadLine( );
}}
Declaratieintroduceert een variabele
van een bepaald type
![Page 24: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/24.jpg)
Console-applicatie
using System;
class Hallo2 { static void Main ( ) { string naam; Console.WriteLine("Wat is je naam?"); naam = Console.ReadLine( ); Console.WriteLine("Hallo, " + naam + "!");
}}
gebruik vande variabele
![Page 25: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/25.jpg)
Console-applicatieusing System;
class Hallo2 { static void Main ( ) { string naam; Console.WriteLine("Wat is je naam?"); naam = Console.ReadLine( ); Console.WriteLine("Hallo, " + naam + "!"); Console.WriteLine("met " + naam.Length + " letters.");
}}
Property vaneen object
![Page 26: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/26.jpg)
Console-applicatieusing System;
class Hallo2 { static void Main ( ) { string naam; Console.WriteLine("Wat is je naam?"); naam = Console.ReadLine( ); Console.WriteLine("Hallo, " + naam + "!"); Console.WriteLine("met " + naam.Length + " letters."); Console.ReadLine( ); }}
![Page 27: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/27.jpg)
Syntax van opdracht
opdracht
( )
,
;expressieklassenaam
objectexpressie
.
methodenaam
= expressie ;variabele
propertynaam +=
![Page 28: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/28.jpg)
Syntax van opdracht
opdracht
( )
,
;expressieklassenaam
objectexpressie
.
methodenaam
= expressie ;variabele
propertynaam +=
toeken
nin
g
![Page 29: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/29.jpg)
Syntax van declaratie
declaratie
type naam ;
,
type
klassenaam
int
string
![Page 30: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/30.jpg)
Windows-applicatie
class HalloWin1 { static void Main ( ) {
}}
Form scherm;scherm = new Form( );
Application.Run(scherm);
using System.Windows.Forms;
scherm.Text = "Hallo";scherm.BackColor = Color.Yellow;scherm.Size = new Size(200,100);
using System.Drawing;
declaratie en toekenningvan een variabele met type Form
gebruik van de variabelebij aanroep van Run
aanpassen vanattributen
![Page 31: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/31.jpg)
Syntax van opdracht
opdracht
( )
,
;expressieklassenaam
objectexpressie
.
methodenaam
= expressie ;variabele
propertynaam +=
![Page 32: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/32.jpg)
Syntax van opdracht
opdracht
( )
,
;expressieklassenaam
objectexpressie
.
methodenaam
= expressie ;
+=propertynaam
variabele
![Page 33: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/33.jpg)
Windows-applicatieclass HalloWin2 { static void Main ( ) {
}}
class HalloForm{
}
Form scherm;scherm = new Form( );Application.Run(scherm);
HalloForm scherm;scherm = new HalloForm( );Application.Run(scherm);
: Form
subklasse is eengespecialiseerde versie
aanroep van deconstructor methode
public HalloForm( ){
}
definitie van deconstructor methode
this.Text = "Hallo";this.BackColor = Color.Yellow;this.Size = new Size(200,100);
![Page 34: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/34.jpg)
Windows-applicatieclass HalloForm{
}
: Formpublic HalloForm( ){
}
this.Text = "Hallo";this.BackColor = Color.Yellow;this.Size = new Size(200,100); Label groet;groet = new Label( );
this.Controls.Add(groet);
groet.Text = "Hallo allemaal";groet.Location = new Point(30,20);
![Page 35: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/35.jpg)
Syntax van opdracht
opdracht
( )
,
;expressieklassenaam
objectexpressie
.
methodenaam
= expressie ;variabele
propertynaam +=
![Page 36: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/36.jpg)
Syntax van opdracht
opdracht
klassenaam
objectexpressie
.
methodenaam
propertynaam
scherm . Size groet . Textnaam . Length
propertynaam
objectexpressie
methodenaam
this.Controls . Add
(
=
klassenaam
Color . Yellow
Console . WriteLineApplication . Run
static
![Page 37: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/37.jpg)
Staticgewone methoden static methodenstatic
Bewerken een object Bewerken geen object
Aanroep:object . methode (…)
Aanroep:klasse . methode (…)
In de body van de definitie:this is dat object
In de body van de definitie:this bestaat niet
Voorbeeld: Voorbeeld:Controls.Add(groet);
Application.Run(scherm); Main
is altijd static
Constructormethoden zijn nooit static
![Page 38: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/38.jpg)
Windows-applicatieclass HalloForm{
}
: Formpublic HalloForm( ){
}
this.Text = "Hallo";this.BackColor = Color.Yellow;this.Size = new Size(200,100); Label groet;groet = new Label( );
this.Controls.Add(groet);
groet.Text = "Hallo allemaal";groet.Location = new Point(30,20);
![Page 39: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/39.jpg)
Windows-applicatieclass HalloForm{
}
: Formpublic HalloForm( ){
}
this.Text = "Hallo";this.BackColor = Color.Yellow;this.Size = new Size(200,100); this.Paint += this.teken;
“Event”-property Methode-naam,maar geen aanroep!
![Page 40: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/40.jpg)
Windows-applicatieclass HalloForm{
}
: Formpublic HalloForm( ){
}
this.Text = "Hallo";this.BackColor = Color.Yellow;this.Size = new Size(200,100); this.Paint += this.teken;}
void teken({
Object o, PaintEventArgs pea )pea
.Graphics.
DrawString(
);
"Hallo!"
, 10, 10, Brushes.Blue
, new Font("Tahoma", 30)
![Page 41: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/41.jpg)
Windows-applicatieclass HalloWin3{ static void Main() { HalloForm scherm; scherm = new HalloForm(); Application.Run(scherm); }}class HalloForm : Form{ public HalloForm() { this.Text = "Hallo"; this.Paint += this.teken; } void teken(object o, PaintEventArgs p) { p.Graphics.DrawString( … ); }}
using System.Windows.Forms;
![Page 42: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/42.jpg)
Game-applicatieclass HalloWin3{ static void Main() { HalloForm scherm; scherm = new HalloForm(); Application.Run(scherm); }}class HalloForm : Form{ public HalloForm() { this.Text = "Hallo"; this.Paint += this.teken; } void teken(object o, PaintEventArgs p) { p.Graphics.DrawString( … ); }}
using System.Windows.Forms;
class HalloGame4{ static void Main() { HalloGame spel; spel = new HalloGame(); }}class HalloGame : Game{ public HalloGame() {
}
}
using Microsoft.XNA.Framework;
spel.Run();
this.Window.Title = "Hallo";
override void Draw(GameTime t){}
![Page 43: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/43.jpg)
Form vs. Gamevoid teken(object o, PaintEventArgs pea){ gr.DrawString(
);}
override void Draw(GameTime gt){
}
sb.DrawString(
);
SpriteBatch sb; sb = new SpriteBatch(this.GraphicsDevice);
sb.Begin();
sb.End();
Graphics gr; gr = pea.Graphics;"Hallo!"
"Hallo!"
, 10, 20new Point( )
, new Vector2(10, 20)
, Brushes.Blue
, Color.Blue
, new Font("Tahoma", 30)
this.Content.Load<SpriteFont>("Spelfont"),
gt.TotalRealTime.Milliseconds, 20)
![Page 44: Hoofdstuk 2](https://reader035.vdocuments.us/reader035/viewer/2022062723/56813fce550346895daaacd1/html5/thumbnails/44.jpg)
Content bij een Game
Fonts, Achtergrondplaatjes, Geluiden… this.Content.Load<SpriteFont>("Spelfont")
Hulpbestanden / Spelfont.spritefont
<?xml version="1.0" encoding="utf-8"?><XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics"> <Asset Type="Graphics:FontDescription">
<Spacing>0</Spacing> <UseKerning>true</UseKerning> <Style>Regular</Style> <CharacterRegions><CharacterRegion><Start> </Start> <End>~</End></CharacterRegion></CharacterRegions> </Asset></XnaContent>
Tahoma 30
<FontName> </FontName> <Size> </Size>
this.Content.RootDirectory = "Hulpbestanden";