Upload
donna-gilmore
View
242
Download
0
Tags:
Embed Size (px)
Citation preview
Openen en Opslaanclass Tekst : Form{
}
TextBox invoer;
void lees (string naam){
}void schrijf (string naam){
}
StreamReader
sr = new StreamReader (naam);sr . ReadToEnd ( )
this.invoer.Text =
;
sr . Close ( ) ;
StreamWriter
sw = new StreamWriter (naam);sw . Write (
) ;this.invoer.Text
sw . Close ( ) ;
this.Text = naam;
this.Text = naam;
Klassen om fileste lezen en te schrijven
Stream
FileStream
MemoryStream
NetworkStream
BufferedStream
GZipStream
CryptoStream
TextWriter
StreamWriter
StringWriter
BinaryWriter
XmlWriter
TextReader
StreamReader
StringReader
BinaryReader
XmlReader
Object
store
deco
rato
r
Klassen om fileste lezen en te schrijven
Stream
FileStream
MemoryStream
NetworkStream
StreamWriterStreamReader
leest en schrijftbytes
leest string en char
schrijftstring en char
leest/schrijft een file
leest/schrijft het geheugen
leest/schrijft het netwerk
Methoden van Stream
Stream
FileStream
MemoryStream
NetworkStream
class FileStream
{
}
int Read (byte[] doel, int max){
}
for (int t=0; t<max; t++){
}
int b = this.ReadByte();if (b==–1) return t;doel[t] =
b ;(byte)
return max;
Stream
int ReadByte ( )
virtual{ /* leeg */ }
class FileStream : Stream{
}
override int ReadByte ( ){ return ............... }
abstract
;
abstract
Abstracte methode/klasse
Stream
FileStream
MemoryStream
NetworkStream
Abstracte methode:methode zonder body
Abstracte klasse:bevat abstracte methode●kun je geen new
object van maken●alleen bedoeld als
superklasse
Brush
SolidBrush
HatchBrush
Decorator Streams
Stream
FileStream
MemoryStream
NetworkStream
Stream str;
BufferedStream
GZipStream
CryptoStream
store
deco
rato
r
str = new FileStream ( "test", FileMode.Create );
GZipStream klein;
klein = new GZipStream ( str ) ;klein.Write( mijnByteArray );
geef hetonderliggendestore-medium
"in beheer"
CryptoStream geheim;geheim = new CryptoStream ( klein ) ;
je kuntdecoratorscombineren
Reader en Writer
Stream
FileStream
MemoryStream
NetworkStream
BufferedStream
GZipStream
CryptoStream
TextWriter
StreamWriter
StringWriter
TextReader
StreamReader
StringReader
Object
store
deco
rato
r
leest en schrijftbytes
leest string en char
schrijftstring en char
vanuit een Stream
vanuit een String
Reader en Writer
Stream
FileStream
MemoryStream
NetworkStream
BufferedStream
GZipStream
CryptoStream
TextWriter
StreamWriter
StringWriter
TextReader
StreamReader
StringReader
Object
store
deco
rato
r
leest en schrijftbytes
leest string en char
schrijftstring en char
Stream str;str = new FileStream ( "test.txt", FileMode.Open );
TextReader lezer;lezer = new StreamReader ( str );
string s;s = lezer . ReadToEnd ( );
Reader en Writer
Stream
FileStream
MemoryStream
NetworkStream
BufferedStream
GZipStream
CryptoStream
TextWriter
StreamWriter
StringWriter
TextReader
StreamReader
StringReader
Object
store
deco
rato
r
leest en schrijftbytes
leest string en char
schrijftstring en char
Stream str;str = new FileStream ( "test.txt", FileMode.Open );
TextReader lezer;lezer = new StreamReader ( str );
string s;s = lezer . ReadToEnd ( );
lezer = new StreamReader ( "test.txt" );
convenience-methode
Reader en Writer
Stream
FileStream
MemoryStream
NetworkStream
BufferedStream
GZipStream
CryptoStream
TextWriter
StreamWriter
StringWriter
BinaryWriter
XmlWriter
TextReader
StreamReader
StringReader
BinaryReader
XmlReader
Object
store
deco
rato
r
leest en schrijftbytes
leest string en char
schrijftstring en char
leest int en double
leest XML-teksten
Figuur 32
Stream
FileStream
MemoryStream
NetworkStream
BufferedStream
GZipStream
CryptoStream
TextReader
StreamReader
StringReader
BinaryReader
XmlReader
Object
store
deco
rato
r
(Filenaam,FileMode)
( )
(Socket)
(Stream, int)
(Stream, CompressionMode)
(Stream, CryptoStreamMode)
(Stream)
(Filenaam)
(String)
(Stream)
.Create(…)
(Stream, Encoding)
(wat de constructor nodig heeft)
statische methodedie het constructie-werk doet
Encoding: char naar byte
ASCII● 1 byte per char, <128
Unicode● 2 bytes per char
a äA α
41 61 3F 3F
41 61 E4 B10000 00 03
BigEndianUnicode● 2 bytes, grote eerst 41 61 E4 B10000 00 03
Latin-1 (iso-8859-1)● 1 byte per char, <256 41 61 E4 3F
UTF8● 1/2/3 bytes
per char41 61 A4 B1C3 CE
header
FEFF
FFFE
EFBBBF
Encoding
Hoofdstuk 11.1
Algoritmen:Console-applicaties
Console applicatie
static void Main ( )
class Hallo{
}
{
}
Console . WriteLine("Hallo " + naam + "!" );
statische methodestatische methoden
Console . WriteLine("Wat is je naam?");string naam = Console . ReadLine( );
class Console
static void WriteLine( string s )
class Console{
}
{}
Console . Out . WriteLine( s );
static string ReadLine( ){}
return Console . In . ReadLine( );
TextReader In { get; }TextWriter Out { get; }
class TextReader
abstract string ReadLine( ) ;
class TextReader
{
}
string ReadToEnd( ){
}
string resultaat, regel;
return resultaat;
regel = this.ReadLine( );while (regel != null){
}
resultaat += regel + "\n" ;regel = this.ReadLine( );
TextReader
StreamReader
StringReader
abstract
class TextReader
abstract string ReadLine( ) ;
class TextReader{
}
string ReadToEnd( ){
}
string resultaat, regel;
return resultaat;
while (regel != null){
}
resultaat += regel + "\n" ;regel = this.ReadLine( );
( (regel = this.ReadLine()) != null )
en meteentestenregel =
this.ReadLine( );
toekennen
Commandline programma
$> dateFri Oct 29, 11:30
$> lsaap.txt hallo.doc prog.cs
$> grep void prog.csprog.cs, line 6: void Main()
prog.cs, line 10: void teken()
$> ls > files.txt
$>
parametersvoor het programma
parametersvoor het programma
output redirection
Voorbeeld file-processor
laat alle regels van de files zienwaarin het gegeven patroon voorkomt
(met filenaam en regelnummer)
grep patroon file1 file2 ...
Unix-commando
Grep: main
class Grep
if (params.Length==0) Console.WriteLine("Usage: java Grep pat file ...");else
static void Main (
for (int t=1; t<params.Length; t++)
Grep.bewerk( params[0], params[t] );
{
}
{)
string [ ] params
Grep: bewerk
static void bewerk (String pat, String naam){
regel=buffer.ReadLine()
Console.WriteLine( naam
if ( regel . Contains (pat) )
reader = new StreamReader (naam);
TextReader reader; String regel;
while (
)( ) != null
; n++
for (int n=1;
+ n
+ regel );
if (naam=="")
reader = Console.In;else
try {
} catch (Exception e) { Console.WriteLine(naam + e); }
Array
Declaratie Creatie Opvragen Wijzigen Lengte
String [ ] a; a = new String[10]; ……a[5]……
a[5] = ……;
…a.Length…
…is eigenlijk overbodig!
List a;
a = new List();
…a.Get(5)…
a.Set(5,…);
…a.Count…
array: oject dat een rij waarden bevat,met speciale notaties
List: array “ingepakt” in een klasse,met standaard notaties
Invoegen Achtervoegen
a.Insert(5,…);
List<String> a;
a = new List<String>();
a.Add(…);
uitgebreide
……a[5]……
a[5] = ……;
Voorbeeld: arrayclass Voorbeeld : Form{
}
Voorbeeld ( ){
}
void klik(object o, AE ae){
}
TextBox in; Button b;
void teken(object o, PEA pea){
}
in = new TextBox();b = new Button();b.Click += klik;this.Paint += teken;
String s = in.Text;alles[n] = s;n++;this.Invalidate();
String [] alles; int n;
alles = new String[100];n = 0;
for (int t=0; t<n; t++){ pea.Graphics.DrawString( alles[t], ......, y ); y+=20;}
int y = 50;
if (n<100){
}
List
List<String> alles;
alles = new List<String>( );
alles.Add(s);
alles[t]
alles.Count
;t++)
Varianten van List
ICollection IList
ISetinterface ICollection<Elem>{ void Add(Elem x); bool Remove(Elem x); bool Contains(Elem x); int Count { get; }; void Clear();}
interface IList<Elem> : ICollection<Elem>{ Elem this[int n] { get; set; } ; int IndexOf(Elem x); void Insert(int n, Elem x); void RemoveAt(int n);}
genummerd
zonder dubbele
Voorbeeld: Collectionclass Voorbeeld : Form{
}
Voorbeeld ( ){
}
void klik(object o, AE ae){
}
TextBox in; Button b;
void teken(object o, PEA pea){
}
in = new TextBox();b = new Button();b.Click += klik;this.Paint += teken;
String s = in.Text;alles[n] = s;n++;this.Invalidate();
alles = new String[100];n = 0;
for (int t=0; t<n; t++){ pea.Graphics.DrawString( alles[t], ......, y ); y+=20;}
int y = 50;
List<String> alles;
alles = new List <String>( );
alles.Add(s);
alles[t]
alles.Count
;t++)
IList<String> alles;
ICollection<String> alles;
LinkedList
Hoe doorloop je een Collection?
List
Collection
for (int t=0; t<alles.Count; t++) doeIetsMet( alles[t] );
foreach (String s in alles) doeIetsMet( s );
opdracht
for )( expr opdrachtexpr expr;;
Speciale syntax voorspecifieke situatie
met bepaalde klasse
type exprinnaam foreach ( ) opdracht
Implementaties van Collections
LinkedList
Queue
Stack
List
HashSet
SortedSet
SortedList
SortedDictionary
IList
ISet
IDictionary
ICollection
Implementaties van Collections
IEnumerable
IEnumerator
IComparator
LinkedList
Queue
Stack
List
HashSet
SortedSet
SortedList
SortedDictionary
IList
ISet
IDictionary
ICollection
Toepassing: schets-editor
SchetsWin
Tools
Akties
MenuMDI-Container
SchetsControl
Schets: Klasse-ontwerp
Control
Form
UserControl
Hoofdscherm
SchetsWin
SchetsControl
SchetsImage Bitmap
ButtonBase
Button
RadioButton
SchetsEditor
System.Windows.Forms
System.Drawing
Program
ContainerControl
ISchetsTool
TekstTool
LijnTool
PenTool
GumTool
RechthTool
VolRechthTool
TweepuntTool
StartpuntTool
Member-variabelenclass SchetsWin class
SchetsControlclass Schets
: Form : UserControl{
}
{
}
{
}
MenuStrip strip;SchetsControl sc;ISchetsTool huidig;
Schets schets;Color kleur;
Bitmap bm;
Schoonmaakwerk doenclass SchetsWin class
SchetsControlclass Schets
: Form : UserControl{
}
{
}
{
}
MenuStrip strip;SchetsControl sc;ISchetsTool huidig;
Schets schets;Color kleur;
Bitmap bm;
void teken(...PEA p){
}
schets.Teken (p.Graphics);
void Teken (Graphics g){
}
gr.DrawImage (bm, 0, 0);
void Schoon(...AE a){
}
schets.Schoon();
void Schoon( ){
}
this.Invalidate();
Graphics. FromImage(bm);
Graphics g =
g.FillRectangle (White,0,0,w,h);
SchetsWin(){
}
sc = new SchetsControl();this.maakAkties();
void maakAkties(){
}
Button b
b.Text = "Clear";b.Click +=
sc.Schoon;
= new Button();
Tool kiezenclass SchetsWin
: Form{
}
MenuStrip strip;SchetsControl sc;
ISchetsTool huidig;
void klikTool ( object obj, AE ae ){
}
huidig = obj . Tag(Button
)( )(ISchetsTool)
void maakTools({
}
ICollection<ISchTool> tools
)foreach (ISchetsTool tool in tools){
}
RadioButton r = new RadioButton();r.Text = tool.ToString();
r.Click += this.klikTool;r.Tag = tool;
Tool gebruikenclass SchetsWin
: Form{
}
MenuStrip strip;SchetsControl sc;
ISchetsTool huidig;
SchetsWin ( ){
}sc.MousePress +=
void klikTool ( object obj, AE ae ){
}
huidig = obj . Tag(Button
)( )(ISchetsTool)
sc = new SchetsControl ( );this.muis;
void muis ( object o, MEA mea){
}huidig.MuisVast(
interface ISchetsTool{
}
void MuisVast(
mea.Location)
Point p);
sc,
SchetsControl s,
Tool gebruikenclass SchetsWin
: Form{
}
MenuStrip strip;SchetsControl sc;
ISchetsTool huidig;
SchetsWin ( ){
sc.MousePress +=
void klikTool ( object obj, AE ae ){
}
huidig = obj . Tag(Button
)( )(ISchetsTool)
sc = new SchetsControl ( );( object o, MEA mea){
}
huidig.MuisVast( sc, mea.Location);
interface ISchetsTool{
}
void MuisVast(
Point p);
SchetsControl s,
}
=>anonieme methode
Schets-tools (1)
ISchetsTool
interface ISchetsTool {
}
void muisVast ( SchetsControl s, Point p) ;
void muisLos ( SchetsControl s, Point p) ;
void muisDrag ( Schetscontrol s, Point p ) ;
void Letter ( SchetsControl s, char k) ;
Schets-tools (2)
ISchetsToolStartpunt
Tool
abstract class StartpuntTool : ISchetsTool {
}
virtual void MuisVast ( SchetsControl s, Point p){ startpunt = p;}
Point startpunt ;
abstract void MuisDrag ( SchetsControl s, Point p) ;abstract void Letter ( SchetsControl s, Point p) ;
virtual void MuisLos ( SchetsControl s, Point p){ kwast = new SolidBrush( s.Penkleur );}
Brush kwast ;
Schets-tools (3)
ISchetsTool
TekstTool
StartpuntTool
class TekstTool : StartpuntTool {
}
override void Letter ( SchetsControl s, char c){
}
Graphics g = s . MaakBitmapGraphics();g . DrawString( c, kwast, ..., startpunt );startpunt.x += ……c….Width;
override void MuisDrag ( SchetsControl s, Point p) { }override void MuisLos ( SchetsControl s, Point p) { }
Schets-tools (4)
ISchetsTool
TekstTool
TweepuntTool
StartpuntTool
abstract class TweepuntTool : StartpuntTool {
}
override void MuisDrag ( SchetsControl s, Point p){
}
Graphics g = s . CreateGraphics();this . Bezig (g, startpunt, p);
abstract void Bezig (Graphics g, Point p1, Point p2) ;
override void MuisLos ( SchetsControl s, Point p){ Graphics g = s . MaakBitmapGraphics(); this . Compleet (g, startpunt, p);}
virtual void Compleet (Graphics g, Point p1, Point p2) { this . Bezig (g, p1, p2); }
Schets-tools (5)
Tool RechthoekTool
TekstTool
TweepuntTool
StartpuntTool
class RechthoekTool : TweepuntTool {
}
override void Bezig ( Graphics g, Point p1, Point p2 )g . DrawRectangle(kwast,
Punten2Rechthoek(p1,p2) );{
}static Rectangle Punten2Rechthoek(Point p1, Point p2){
}
return new Rectangle(new Point( Math.Min(p1.X,p2.X), Math.Min(p1.Y,p2.Y))new Size ( Math.Abs(p1.X-p2.X), Math.Abs(p1.Y-p2.Y)) );
Schets-tools (6)
Tool
TekstTool
TweepuntTool
StartpuntTool
class VolRechthoekTool : RechthoekTool {
}
override void Compleet ( Graphics g, Point p1, Point p2 ){
}
RechthoekTool
VolRechthTool
g . FillRectangle(kwast, Punten2Rechthoek(p1,p2) );
Schets-tools (7)
Tool
LijnToolTekstTool
TweepuntTool
StartpuntTool
class LijnTool : TweepuntTool {
}
override void Bezig ( Graphics g, Point p1, Point p2 ){
}
g . DrawLine( MaakPen(kwast,3), p1, p2 );
RechthoekTool
VolRechthTool
Schets-tools (8)
Tool
LijnTool PenToolTekstTool
TweepuntTool
StartpuntTool
class PenTool : LijnTool {
}
override void MuisDrag ( SchetsControl s, Point p ){
}
this . MuisLos (s, p);this . MuisVast (s, p);
RechthoekTool
VolRechthTool
Schets-tools (9)
Tool
LijnTool PenTool GumToolTekstTool
TweepuntTool
StartpuntTool
class GumTool : PenTool {
}
override void Bezig ( Graphics g, Point p1, Point p2 ){
}
RechthoekTool
VolRechthTool
g . DrawLine( MaakPen(Brushes.White,7), p1, p2 );