28. oktober 2001 - 11:21Der er
5 kommentarer og 2 løsninger
lægge tal sammen...
Jeg har dette eksempel på javakode bestående af to klasser.
Det virker fint da det er et færdigt eksempel, men hvordan laver jeg en metode som kan lægge 6 30, og 80 samen, og udskrive det færdige resultat?
class CurioStore1 {
/* Curio Store No. 1 by J M Bishop April 2000 * ----------------- * Illustrates the basic form of an object oriented program */
// Declare three object variables Curio mugs, tshirts, carvings;
// The constructor for the program is // where the initial work gets done CurioStore1 () { // Create three objects with different initial values mugs = new Curio(\"Traditional mugs\", 6, \"beaded in Ndebele style\"); tshirts = new Curio(\"T-shirts\", 30, \"sizes M to XL\"); carvings = new Curio(\"Masks\", 80, \"carved in wood\");
// Print out a header System.out.println(\"The Polelo Curio Store sells\\n\");
// Print the values contained in each of the objects mugs.write(); tshirts.write(); carvings.write(); }
// All programs must have a main method public static void main (String [ ] args) { // Start the program running from its constructor new CurioStore1 (); } }
class Curio {
// Declare variables related to a curio String name; int price; String description;
// The constructor copies the initial values // into the object\'s variables Curio (String n, int p, String d) { name = n; price = p; description = d; }
// a method to output the values of the object\'s variables void write () { System.out.println(name + \" \" + description + \" for G\" + price); } }
ok.. Den skal tage de faktiske tal: 6 30 80 og lægge sammen. Og hvis man ændrede dem i CorioStore1 klassen skal der således printes et andet resultat ud. Kan det ikke gøres via en metode i corio klassen, som så bliver \"kaldt\" i coriostore1 klassen. En metode med return måske?
/* Curio Store No. 1 by J M Bishop April 2000 * ----------------- * Illustrates the basic form of an object oriented program */
// Declare three object variables Curio mugs, tshirts, carvings;
// The constructor for the program is // where the initial work gets done CurioStore1 () { // Create three objects with different initial values mugs = new Curio(\"Traditional mugs\", 6, \"beaded in Ndebele style\"); tshirts = new Curio(\"T-shirts\", 30, \"sizes M to XL\"); carvings = new Curio(\"Masks\", 80, \"carved in wood\");
// Print out a header System.out.println(\"The Polelo Curio Store sells\\n\");
// Print the values contained in each of the objects mugs.write(); tshirts.write(); carvings.write(); int[] priser = { mugs.price, tshirts.price, carvings.price }; //NY Linie summa( priser ); // NY linie }
static int summa( int[] args ) { // NY FUNKTION int sum = 0; for ( int i = args.length-1; i>=0; i--) sum += args[i]; System.out.println( sum ); return sum; }
// All programs must have a main method public static void main (String [ ] args) { // Start the program running from its constructor new CurioStore1 (); } }
class Curio {
// Declare variables related to a curio String name; int price; String description;
// The constructor copies the initial values // into the object\'s variables Curio (String n, int p, String d) { name = n; price = p; description = d; }
// a method to output the values of the object\'s variables void write () { System.out.println(name + \" \" + description + \" for G\" + price); } }
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.