public void setPrice(double newPrice) { price = newPrice; } }
class PriceCalculations { public static void main(String[] args) { final double amount = 2.5; final double kiloPrice1 = 7.30; final double kiloPrice2 = 7.90; Merchandise aMerchandise = new Merchandise("Brie", 123, kiloPrice1); double price1 = aMerchandise.getPriceWithoutVAT(amount); double price2 = aMerchandise.getPriceWithVAT(amount); System.out.println("The price per kilo without VAT: " + kiloPrice1); System.out.println("The price for " + amount + " kilos is " + price1 + " without VAT"); System.out.println("The price for " + amount + " kilos is " + price2 + " with VAT");
aMerchandise.setPrice(kiloPrice2); System.out.println("New price per kilo without VAT: " + kiloPrice2); System.out.println("The price for " + amount + " kilos is " + aMerchandise.getPriceWithoutVAT(amount) + " without VAT"); System.out.println("The price for " + amount + " kilos is " + aMerchandise.getPriceWithVAT(amount) + " with VAT"); } }
/* Example Run: The price per kilo without VAT: 7.3 The price for 2.5 kilos is 18.25 without VAT The price for 2.5 kilos is 21.9 with VAT New price per kilo without VAT: 7.9 The price for 2.5 kilos is 19.75 without VAT The price for 2.5 kilos is 23.7 with VAT */
Manuelle og semi-automatiske strategier for identitetsstyring virker - lige indtil nogen beder om dokumentation. For at undgå denne fare har DKTV taget kontrol over sin identitets- og adgangsstrategi.
Det kan du udenmærkt gøre pt. Lav en fil, som hedder Merchandise.java - og put source-coden i den... og lave en fil, som hedder PriceCalculations.java og put PriceCalculations klassekoden deri ...
Der er intet til hinder for det pt :-) (og det er bedre at have hver klasse i hver deres fil
lad os gennemgå udtrykket ord for ord.... PriceCalculations ny = new PriceCalculations();
Udtrykket består af to dele, som er opdelt af et lighedstegn... Dette betyder at den venstre udtryk antager værdien af det højre udtryk : PriceCalculations ny bliver lig med new PriceCalculations();
Det venstre udtryk PriceCalculations ny betyder at vi opretter en ny variabel, af typen "PriceCalculations", som hedder "ny"
Det højre udtryk new PriceCalculations(); betyder at der bliver oprettet (og instatieret) et nyt (new) objekt af typen "PriceCalculations" og at der ikke sendes nogle argumenter afsted til objektets konstruktør () [der står ikke noget mellem paranteserne...]
Derfor læses udtrykket PriceCalculations ny = new PriceCalculations(); således :
Der oprettes en ny variabel, af typen "PriceCalculations", som hedder "ny" - og denne sættes til at pege på et nyt instantieret objekt af typen "PriceCalculations".
Hvis du ikke har tænkt dig at bruge "ny" til noget, så er det helt lovligt at skrive public static void main(String[] args) { new PriceCalculations(); }
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.