23.07.2013 Views

transparenterne - Objektorienteret programmering i Java

transparenterne - Objektorienteret programmering i Java

transparenterne - Objektorienteret programmering i Java

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Lokale, objekt− og klassevariable<br />

public class Boks4<br />

{ private double længde; // objektvariabel<br />

private double bredde; // objektvariabel<br />

private double højde; // objektvariabel<br />

private static int antalBokse; // klassevariabel<br />

public Boks4(double l, double b, double h)<br />

{<br />

// l, b og h er lokale variabler<br />

længde = l;<br />

bredde = b;<br />

højde = h;<br />

antalBokse = antalBokse + 1;<br />

}<br />

public static int læsAntalBokse() // klassemetode<br />

{ return antalBokse;<br />

}<br />

public double volumen()<br />

{<br />

// vol er en lokal variabel<br />

double vol;<br />

vol = længde*bredde*højde;<br />

return vol;<br />

}<br />

}<br />

public class BenytBoks4<br />

{ public static void main(String args[])<br />

{ System.out.println("Antal bokse: "+ Boks4.læsAntalBokse());<br />

Boks4 boksen;<br />

boksen = new Boks4(2,5,10);<br />

System.out.println("Antal bokse: "+ Boks4.læsAntalBokse());<br />

Boks4 enAndenBoks, enTredjeBoks;<br />

enAndenBoks = new Boks4(5,5,10);<br />

enTredjeBoks = new Boks4(7,5,10);<br />

Boks<br />

________________________________<br />

antalBokse :int<br />

læ ngde :double<br />

bredde :double<br />

¡<br />

h jde :double<br />

________________________________<br />

+Boks(l,b,h)<br />

+læ sAntalBokse(): int<br />

+volumen() :double<br />

System.out.println("Antal bokse: "+ Boks4.læsAntalBokse());<br />

}<br />

}<br />

Antal bokse: 0<br />

Antal bokse: 1<br />

Antal bokse: 3

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!