Top Banner
Hoofdstuk 2 Hallo, C# !
44

Hoofdstuk 2

Jan 08, 2016

Download

Documents

Toshi

Hoofdstuk 2. Hallo, C# !. Soorten programma’s. Console- applicatie. Soorten programma’s. Console- applicatie Windows- applicatie. Soorten programma’s. Console- applicatie Windows- applicatie Web- applicatie. Soorten programma’s. Console- applicatie Windows- applicatie Web- applicatie - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Hoofdstuk 2

Hoofdstuk 2

Hallo, C# !

Page 2: Hoofdstuk 2

Soorten programma’s Console-

applicatie

Page 3: Hoofdstuk 2

Soorten programma’s Console-

applicatie Windows-

applicatie

Page 4: Hoofdstuk 2

Soorten programma’s Console-

applicatie Windows-

applicatie Web-

applicatie

Page 5: Hoofdstuk 2

Soorten programma’s Console-

applicatie Windows-

applicatie Web-

applicatie Game

Page 6: Hoofdstuk 2

Soorten programma’s Console-

applicatie Windows-

applicatie Web-

applicatie Game

Applet

Page 7: Hoofdstuk 2

Soorten programma’s Console-

applicatie Windows-

applicatie Web-

applicatie Game

Applet

App

Page 8: Hoofdstuk 2

Opbouw broncode

Opdrachten omhet geheugente veranderen

Opdrachten zijn gegroepeerd inmethoden

Methoden zijn gegroepeerd inklassen

Page 9: Hoofdstuk 2

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

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

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

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

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

Methode-header en -aanroep

methode-header

methode-aanroep

using System;

class Hallo { static void Main( ) { Console.WriteLine("Hallo!"); Console.ReadLine( ); }}

Page 15: Hoofdstuk 2

Syntax en semantiek

Syntax:de vorm van het programmacompilatieeenheid

using ;

klassedeclaratie

librarynaam

Semantiek:wat het programmabetekent

Page 16: Hoofdstuk 2

Syntax van klasse-declaratie

klassedeclaratie

public private

class naam { member }

: naam

Page 17: Hoofdstuk 2

Syntax van member

member

static

public private

naam

type

void ( ) blok

,

naamtype

Page 18: Hoofdstuk 2

Syntax van blok

blok

declaratie

opdracht{ }

Page 19: Hoofdstuk 2

Syntax van opdracht

opdracht

( )

,

;expressieklassenaam

objectexpressie

.

methodenaam

= expressie ;variabele

propertynaam +=

Page 20: Hoofdstuk 2

Syntax van opdracht

opdracht

( )

,

;expressieklassenaam

objectexpressie

.

methodenaam

= expressie ;variabele

propertynaam +=

Page 21: Hoofdstuk 2

Console-applicatie

using System;

class Hallo2 { static void Main ( ) { Console.WriteLine("Hallo!"); Console.ReadLine( );

}}

Page 22: Hoofdstuk 2

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

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

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

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

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

Syntax van opdracht

opdracht

( )

,

;expressieklassenaam

objectexpressie

.

methodenaam

= expressie ;variabele

propertynaam +=

Page 28: Hoofdstuk 2

Syntax van opdracht

opdracht

( )

,

;expressieklassenaam

objectexpressie

.

methodenaam

= expressie ;variabele

propertynaam +=

toeken

nin

g

Page 29: Hoofdstuk 2

Syntax van declaratie

declaratie

type naam ;

,

type

klassenaam

int

string

Page 30: Hoofdstuk 2

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

Syntax van opdracht

opdracht

( )

,

;expressieklassenaam

objectexpressie

.

methodenaam

= expressie ;variabele

propertynaam +=

Page 32: Hoofdstuk 2

Syntax van opdracht

opdracht

( )

,

;expressieklassenaam

objectexpressie

.

methodenaam

= expressie ;

+=propertynaam

variabele

Page 33: Hoofdstuk 2

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

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

Syntax van opdracht

opdracht

( )

,

;expressieklassenaam

objectexpressie

.

methodenaam

= expressie ;variabele

propertynaam +=

Page 36: Hoofdstuk 2

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

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

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

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

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

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

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

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

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>&#32;</Start> <End>&#126;</End></CharacterRegion></CharacterRegions> </Asset></XnaContent>

Tahoma 30

<FontName> </FontName> <Size> </Size>

this.Content.RootDirectory = "Hulpbestanden";