ebook img

Objektorienteret programmering i Java PDF

320 Pages·2007·3.063 MB·Danish
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Objektorienteret programmering i Java

Jacob Nordfalk J Objektorienteret programmering i ava 4. udgave FG811-3 - kolofon - OOPJava4.indd 1 21-11-2007 13:11:37 Objektorienteret programmering i Java Copyright: 2007© by Forlaget Globe A/S 4. udgave, 2. oplag Udgiver: Forlaget Globe A/S Skodsborgvej 305 2850 Nærum Tlf. 70 15 14 00 Fax 70 15 14 10 E-mail: [email protected] Internet: http://www.globe.dk Forfatter: Jacob Nordfalk Forlagsredaktion: Carsten Straaberg ([email protected]) Omslag: Carsten Straaberg DtP: Jacob Nordfalk Sats: Forlaget Globe A/S, Nærum Tryk: Eurographic Danmark, Taastrup Printed in Denmark 2009 ISBN: 978-87-7900-811-3 Forlaget Globe og forfatteren kan ikke holdes økonomisk ansvarlig for eventuelle fejl og mangler i bogen. www.globe.dk Bøg om fritid, hobby, IT og erhverv FG811-3 - kolofon - OOPJava4.indd 2 21-11-2007 13:11:37 Indholdsfortegnelse 0 Forord......................................................................................15 0.1 Bogens opbygning.......................................................................................15 0.1.1 Veje gennem bogen..............................................................................................................15 0.1.2 Kapitlernes opbygning..........................................................................................................17 0.2 Til underviseren............................................................................................17 0.3 Ændringer i fjerde udgave...........................................................................18 0.4 Tak.................................................................................................................18 1 Introduktion.............................................................................19 1.1 Programmering.............................................................................................20 1.1.1 Styresystemet.......................................................................................................................20 1.1.2 Hvorfor lære at programmere? ............................................................................................20 1.1.3 Et simpelt program................................................................................................................20 1.1.4 Hvordan lærer man at programmere....................................................................................21 1.2 Fordele og ulemper ved Java......................................................................21 1.2.1 Stærke sider.........................................................................................................................21 1.2.2 Stor opbakning......................................................................................................................22 1.2.3 Svagheder............................................................................................................................22 1.3 Programmeringsværktøjer til Java.............................................................23 1.3.1 Borland JBuilder...................................................................................................................23 1.3.2 Eclipse.org............................................................................................................................24 1.3.3 Oracle JDeveloper................................................................................................................25 1.3.4 NetBeans..............................................................................................................................26 1.3.5 BlueJ.....................................................................................................................................27 1.3.6 Andre værktøjer....................................................................................................................27 1.3.7 Sun JDK................................................................................................................................27 1.4 Hjælp og videre læsning..............................................................................28 1.4.1 Projekter med åben kildekode..............................................................................................28 2 Basal programmering............................................................29 2.1 Det første javaprogram................................................................................30 2.1.1 Kommentarer........................................................................................................................30 2.1.2 Klassedefinitionen.................................................................................................................31 2.1.3 Oversættelse og kørsel af programmet................................................................................31 HTTP://JAVABOG.DK - 3 2.2 Variabler........................................................................................................33 2.2.1 Heltal.....................................................................................................................................33 2.2.2 Sammensætte strenge med +..............................................................................................34 2.2.3 Beregningsudtryk..................................................................................................................34 2.2.4 Kommatal..............................................................................................................................35 2.2.5 Matematiske funktioner.........................................................................................................37 2.2.6 Kald af metoder....................................................................................................................38 2.2.7 Logiske variabler...................................................................................................................38 2.2.8 Opgaver................................................................................................................................39 2.3 Betinget udførelse........................................................................................39 2.3.1 Indlæsning fra tastaturet.......................................................................................................40 2.3.2 if-else....................................................................................................................................41 2.3.3 Opgaver................................................................................................................................41 2.4 Blokke............................................................................................................42 2.4.1 Indrykning.............................................................................................................................42 2.5 Løkker............................................................................................................43 2.5.1 while-løkken..........................................................................................................................43 2.5.2 for-løkken..............................................................................................................................45 2.5.3 Indlejrede løkker...................................................................................................................46 2.5.4 Uendelige løkker...................................................................................................................47 2.5.5 Opgaver................................................................................................................................47 2.6 Værditypekonvertering................................................................................48 2.6.1 Eksplicit typekonvertering.....................................................................................................48 2.6.2 Implicit typekonvertering.......................................................................................................49 2.6.3 Misforståelser omkring typekonvertering..............................................................................49 2.7 Fejl.................................................................................................................50 2.7.1 Indholdsmæssige (logiske) fejl.............................................................................................50 2.7.2 Sproglige fejl.........................................................................................................................50 2.7.3 Køretidsfejl............................................................................................................................51 2.8 Test dig selv..................................................................................................52 2.9 Resumé..........................................................................................................53 2.9.1 Gode råd om programmering...............................................................................................53 2.10 Opgaver.......................................................................................................54 2.10.1 Befordringsfradrag..............................................................................................................54 2.10.2 Kurveprogram.....................................................................................................................54 2.11 Appendiks...................................................................................................55 2.11.1 Navngivningsregler.............................................................................................................55 2.11.2 De simple typer...................................................................................................................55 2.11.3 Værditypekonvertering........................................................................................................55 2.11.4 Aritmetiske operatorer........................................................................................................56 2.11.5 Regning med logiske udtryk...............................................................................................57 2.11.6 Sammenligningsoperatorer.................................................................................................57 4 - OBJEKTORIENTERET PROGRAMMERING I JAVA 2.12 Avanceret....................................................................................................58 2.12.1 Grafiske indtastningsvinduer..............................................................................................58 2.12.2 Formatere tal og datoer......................................................................................................59 2.12.3 Primtal.................................................................................................................................60 2.12.4 Klassemetoder....................................................................................................................60 2.12.5 Kombination af logiske operatorer......................................................................................62 2.12.6 Alternativudtryk...................................................................................................................63 2.12.7 Brug af ++ og -- (optælling og nedtælling)..........................................................................63 2.12.8 Brug af += og -=..................................................................................................................63 2.12.9 do-while-løkken...................................................................................................................63 2.12.10 break.................................................................................................................................64 2.12.11 continue............................................................................................................................64 2.12.12 break og continue med navngivne løkker.........................................................................64 2.12.13 switch................................................................................................................................65 2.12.14 Komma i for-løkker...........................................................................................................65 3 Objekter...................................................................................67 3.1 Objekter og klasser......................................................................................68 3.2 Punkter (klassen Point)................................................................................69 3.2.1 Erklæring og oprettelse.........................................................................................................69 3.2.2 Objektvariabler......................................................................................................................70 3.2.3 Metodekald...........................................................................................................................70 3.2.4 Eksempel..............................................................................................................................71 3.2.5 Import af standardklasser.....................................................................................................72 3.3 Rektangler (klassen Rectangle)..................................................................72 3.3.1 Konstruktører........................................................................................................................72 3.3.2 Metoder.................................................................................................................................73 3.3.3 Metoders returtype................................................................................................................74 3.3.4 Metoders parametre.............................................................................................................75 3.4 Tekststrenge (klassen String).....................................................................76 3.4.1 Streng-objekter er uforanderlige...........................................................................................77 3.4.2 Man behøver ikke bruge new til String-objekter...................................................................78 3.4.3 Navnesammenfald for metoder............................................................................................78 3.4.4 At sætte strenge sammen med +..........................................................................................78 3.4.5 Sammenligning.....................................................................................................................79 3.4.6 Opgaver................................................................................................................................80 3.5 Lister (klassen ArrayList)............................................................................80 3.5.1 Gennemløb af lister..............................................................................................................82 3.5.2 Eksempel: Eventyrfortælling.................................................................................................82 3.5.3 Eksempel: Punkter i en liste.................................................................................................82 3.5.4 Historisk bemærkning: Lister før JDK 1.5.............................................................................83 3.5.5 Eksempel: Blanding af kort med ArrayList............................................................................84 3.6 Datoer (klassen Date)...................................................................................85 3.6.1 Opgaver................................................................................................................................86 HTTP://JAVABOG.DK - 5 3.7 Test dig selv..................................................................................................87 3.8 Resumé..........................................................................................................87 3.9 Appendiks.....................................................................................................88 3.9.1 Klassen Point........................................................................................................................88 3.9.2 Klassen Rectangle................................................................................................................89 3.9.3 Klassen String.......................................................................................................................90 3.9.4 Specialtegn i strenge............................................................................................................91 3.9.5 Klassen Date........................................................................................................................92 3.9.6 Klassen ArrayList..................................................................................................................93 3.10 Avanceret....................................................................................................94 3.10.1 Sætte strenge sammen (klassen StringBuilder).................................................................94 3.10.2 Standardmetoder til at arbejde med lister...........................................................................96 3.10.3 Lister af simple typer (autoboxing)......................................................................................96 3.10.4 Andre slags lister og mængder...........................................................................................96 3.10.5 Nøgleindekserede tabeller (klassen HashMap)..................................................................97 4 Definition af klasser...............................................................99 4.1 En Boks-klasse...........................................................................................100 4.1.1 Variabler.............................................................................................................................100 4.1.2 Brug af klassen...................................................................................................................101 4.1.3 Metodedefinition.................................................................................................................101 4.1.4 Flere objekter......................................................................................................................102 4.2 Indkapsling..................................................................................................103 4.3 Konstruktører..............................................................................................105 4.3.1 Standardkonstruktører........................................................................................................106 4.3.2 Opgaver..............................................................................................................................106 4.4 En Terning-klasse.......................................................................................107 4.4.1 Opgaver..............................................................................................................................108 4.5 Relationer mellem objekter........................................................................108 4.5.1 En Raflebæger-klasse........................................................................................................108 4.5.2 Opgaver..............................................................................................................................110 4.6 Nøgleordet this...........................................................................................111 4.7 Ekstra eksempler........................................................................................112 4.7.1 En n-sidet terning................................................................................................................112 4.7.2 Personer.............................................................................................................................114 4.7.3 Design af klasser................................................................................................................115 4.8 Test dig selv................................................................................................116 4.9 Resumé........................................................................................................116 4.9.1 Formen af en klasse...........................................................................................................117 4.9.2 Formen af en metode.........................................................................................................118 6 - OBJEKTORIENTERET PROGRAMMERING I JAVA 4.10 Opgaver.....................................................................................................119 4.10.1 BlueJ.................................................................................................................................119 4.10.2 Fejlfinding.........................................................................................................................120 4.11 Avanceret..................................................................................................122 4.11.1 Bankkonti..........................................................................................................................122 4.11.2 Opgaver............................................................................................................................122 5 Nedarvning............................................................................123 5.1 At udbygge eksisterende klasser.............................................................124 5.1.1 Eksempel: En falsk terning.................................................................................................124 5.1.2 At udbygge med flere metoder og variabler........................................................................126 5.1.3 Nøgleordet super................................................................................................................126 5.1.4 Opgaver..............................................................................................................................127 5.2 Polymorfe variabler....................................................................................128 5.2.1 Dispensation fra traditionel typesikkerhed..........................................................................128 5.2.2 Polymorfi.............................................................................................................................129 5.2.3 Eksempel på polymorfi: Brug af Raflebaeger.....................................................................129 5.2.4 Hvilken vej er en variabel polymorf ?..................................................................................130 5.2.5 Reference-typekonvertering...............................................................................................131 5.3 Eksempel: Et matador-spil........................................................................132 5.3.1 Polymorfi.............................................................................................................................138 5.3.2 Opgaver..............................................................................................................................138 5.4 Stamklassen Object...................................................................................139 5.4.1 Referencer til objekter.........................................................................................................140 5.5 Konstruktører i underklasser....................................................................141 5.5.1 Konsekvenser.....................................................................................................................142 5.6 Ekstra eksempler........................................................................................143 5.6.1 Matadorspillet version 2......................................................................................................143 5.6.2 Opgaver..............................................................................................................................145 5.7 Test dig selv................................................................................................146 5.8 Resumé........................................................................................................146 5.9 Avanceret....................................................................................................147 5.9.1 Initialisering uden for konstruktørerne................................................................................147 5.9.2 Kald af en konstruktør fra en anden konstruktør................................................................148 5.9.3 Metoder erklæret final.........................................................................................................148 5.9.4 Metoder erklæret protected................................................................................................148 5.9.5 Variabel-overskygning........................................................................................................148 6 Pakker....................................................................................149 6.1 At importere klassedefinitioner.................................................................150 6.2 Standardpakkerne......................................................................................150 6.2.1 Pakken java.lang................................................................................................................151 HTTP://JAVABOG.DK - 7 6.3 Pakkers placering på filsystemet..............................................................151 6.4 At definere egne pakker.............................................................................152 6.4.1 Eksempel............................................................................................................................152 6.5 Pakke klasser i jar-filer (Java-arkiver)......................................................152 6.5.1 Eksekverbare jar-filer..........................................................................................................153 6.6 Test dig selv................................................................................................153 6.7 Resumé........................................................................................................153 6.8 Opgaver.......................................................................................................153 6.9 Avanceret: public, protected og private...................................................154 6.9.1 Variabler og metoder..........................................................................................................154 6.9.2 Indkapsling med pakker......................................................................................................154 6.9.3 Klasser................................................................................................................................154 7 Lokale, objekt- og klassevariabler......................................155 7.1 Klassevariabler og -metoder.....................................................................156 7.1.1 Eksempler på klassevariabler.............................................................................................157 7.1.2 Eksempler på klassemetoder.............................................................................................157 7.2 Lokale variabler og parametre..................................................................158 7.2.1 Parametervariabler.............................................................................................................158 7.3 Test dig selv................................................................................................160 7.4 Resumé........................................................................................................160 7.4.1 Tilknytning...........................................................................................................................160 7.4.2 Adgang til variabler og metoder..........................................................................................160 7.4.3 Adgang fra metoder............................................................................................................160 7.5 Avanceret....................................................................................................161 7.5.1 Introduktion til rekursive algoritmer.....................................................................................161 7.5.2 Rekursion: Beregning af formel..........................................................................................162 7.5.3 Rekursion: Listning af filer..................................................................................................163 7.5.4 Rekursion: Tegning af fraktaler...........................................................................................164 8 Arrays....................................................................................165 8.1 Erklæring og brug......................................................................................166 8.1.1 Eksempel: Statistik.............................................................................................................167 8.1.2 Initialisere et array med startværdier..................................................................................167 8.1.3 Arrayet i main()-metoden....................................................................................................168 8.2 Gennemløb og manipulering af array.......................................................168 8.3 Array af objekter.........................................................................................169 8.3.1 Polymorfi.............................................................................................................................169 8.4 Arrays versus lister (ArrayList).................................................................169 8.5 Resumé........................................................................................................170 8.6 Opgaver.......................................................................................................170 8 - OBJEKTORIENTERET PROGRAMMERING I JAVA 8.7 Avanceret....................................................................................................171 8.7.1 Flerdimensionale arrays.....................................................................................................171 8.7.2 Konvertere mellem arrays og lister.....................................................................................172 9 Grafiske programmer...........................................................173 9.1 Klassen Graphics.......................................................................................175 9.1.1 Eksempel: Grafikdemo.......................................................................................................176 9.1.2 Eksempel: Kurvetegning.....................................................................................................177 9.2 Metoder du kan kalde.................................................................................178 9.3 Opgaver.......................................................................................................179 9.3.1 Opgave: Grafisk Matador-spil.............................................................................................179 9.4 Avanceret....................................................................................................181 9.4.1 Simple animationer.............................................................................................................181 9.4.2 Animationer med en separat tråd.......................................................................................182 9.4.3 Java2D - avanceret grafiktegning.......................................................................................182 9.4.4 Passiv versus aktiv visning.................................................................................................184 9.4.5 Fuldskærmstegning............................................................................................................185 9.4.6 Dobbeltbuffer......................................................................................................................186 10 Appletter..............................................................................187 10.1 HTML-dokumentet....................................................................................188 10.2 Javakoden.................................................................................................188 10.3 Metoder i appletter...................................................................................189 10.3.1 Metoder i appletter, som du kan kalde..............................................................................189 10.3.2 Metoder i appletter, som systemet kalder.........................................................................190 10.3.3 Eksempel..........................................................................................................................190 10.4 Resumé......................................................................................................191 10.5 Avanceret..................................................................................................192 10.5.1 Sikkerhedsbegrænsninger for appletter............................................................................192 10.5.2 Pakker og CODE/CODEBASE i HTML-koden..................................................................192 10.5.3 Begrænsninger i ældre netlæsere....................................................................................192 11 Grafiske standardkomponenter........................................193 11.1 Generering med et værktøj......................................................................194 11.1.1 Interaktive programmer.....................................................................................................196 11.1.2 Genvejstaster....................................................................................................................197 11.2 Overblik over komponenter.....................................................................198 11.2.1 JLabel...............................................................................................................................198 11.2.2 JButton..............................................................................................................................198 11.2.3 JCheckBox og JRadioButton............................................................................................198 11.2.4 JTextField.........................................................................................................................199 11.2.5 JTextArea.........................................................................................................................199 11.2.6 JComboBox......................................................................................................................199 11.2.7 JList..................................................................................................................................200 HTTP://JAVABOG.DK - 9 11.3 Eksempel...................................................................................................200 11.4 Overblik over containere.........................................................................202 11.4.1 JWindow...........................................................................................................................202 11.4.2 JDialog..............................................................................................................................202 11.4.3 JFrame..............................................................................................................................202 11.4.4 JPanel...............................................................................................................................202 11.4.5 JApplet..............................................................................................................................202 11.4.6 JTabbedPane...................................................................................................................203 11.5 Layout-managere......................................................................................203 11.5.1 Ingen styring (null-layout).................................................................................................203 11.5.2 FlowLayout.......................................................................................................................203 11.5.3 BorderLayout....................................................................................................................204 11.5.4 GridBagLayout..................................................................................................................205 11.6 Menuer.......................................................................................................206 11.7 Test dig selv..............................................................................................208 11.8 Resumé......................................................................................................208 11.9 Avanceret..................................................................................................209 11.9.1 HTML-kode i komponenter...............................................................................................209 11.9.2 Flertrådet komponentprogrammering...............................................................................210 11.9.3 Brug af komponenter vs. paintComponent().....................................................................210 12 Interfaces - grænseflader til objekter...............................211 12.1 Definere et interface.................................................................................212 12.2 Implementere et interface........................................................................212 12.2.1 Variabler af type Tegnbar.................................................................................................213 12.2.2 Eksempler med interfacet Tegnbar...................................................................................213 12.2.3 Visning af nogle Tegnbare objekter..................................................................................215 12.2.4 Polymorfi...........................................................................................................................216 12.3 Interfaces i standardbibliotekerne..........................................................216 12.3.1 Sortering med en Comparator..........................................................................................216 12.3.2 Flere tråde med Runnable................................................................................................217 12.3.3 Lytte til musen med MouseListener..................................................................................217 12.4 Test dig selv..............................................................................................218 12.5 Resumé......................................................................................................218 12.6 Opgaver.....................................................................................................218 12.7 Avanceret..................................................................................................219 12.7.1 Collections - ArrayList's familie.........................................................................................219 12.7.2 Implementere flere interfaces...........................................................................................219 12.7.3 At udbygge interfaces.......................................................................................................220 12.7.4 Multipel arv.......................................................................................................................220 12.7.5 Variabler i et interface.......................................................................................................220 12.7.6 Statisk import....................................................................................................................220 10 - OBJEKTORIENTERET PROGRAMMERING I JAVA

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.