עזרה בג'אוה

haunter

New member
עזרה בג'אוה

מה השגיאות האלה אומרות? מידע כללי: המחלקה כלב יורשת מיונק שיורשת מחיה. הקונסטרקטור בסדר. השיטה details() מחזירה מחרוזת. השיטה talk() מדפיסה משהו. מישהו יכול בבקשה לומר ממה נבעו השגיאות שכתובות שם? תודה!
 

Zack DA

New member
טוב, פרסם את הקוד, זה נראה כמו

שגיאת סינטקס בסיסית.
 

haunter

New member
איזה קומפיילר? אממ לא יודע...../images/Emo122.gif

לפרסם איזה קוד? של המחלקות? כי הבעיה היא רק בTest! הנה הן בכל מקרה:
public class Animal { protected String name; protected int age; protected int numOfLegs; protected int energy; public Animal(String name, int age, int numOfLegs, int energy) { this.name=name; this.age=age; this.numOfLegs=numOfLegs; this.energy=energy; } public Animal() { this.name="Ish"; this.age=0; this.numOfLegs=2; this.energy=100; } public void setName(String name){ this.name=name; } public void setAge(int age){ this.age=age; } public String getName(){ return this.name; } public int getAge(){ return this.age; } public int getNumOfLegs(){ return this.numOfLegs; } public void eat(int amount){ this.energy+=amount; } public void exercise(int amount){ if (amount>this.energy) System.out.print("You wanna kill him?!"); else this.energy-=amount; } public void talk() { System.out.println("Bla."); } public String details() { return ("Name: "+this.name+", Age: "+this.age+", Legs: "+this.numOfLegs+", Energy: "+this.energy); } } public class Mammal extends Animal { protected int milk; public Mammal(String name, int age, int numOfLegs, int energy, int milk){ super(name, age, numOfLegs, energy); this.milk=milk; } public Mammal() { super(); this.milk=0; } public void drinkMilk(int amount) { this.milk+=amount; } public void giveMilk(int amount) { if (amount>this.milk) System.out.print("Not enough milk!"); else this.milk-=amount; } public int getMilk() { return this.milk; } public void talk() { System.out.println("Prrr!"); } public String details() { return ("Name: "+this.name+", Age: "+this.age+", Legs: "+this.numOfLegs+", Energy: "+this.energy+", Milk: "+this.milk); } } public class Dog extends Mammal { protected String breed; public Dog(String name, int age, int energy, int milk, String breed) { super(name, age, 4, energy, milk); this.breed=breed; } public Dog() { super(); this.breed="Mutt"; } public String getBreed() { return this.breed; } public void talk() { System.out.println("Wuff!"); } public String details() { return ("Name: "+this.name+", Age: "+this.age+", Legs: "+this.numOfLegs+", Energy: "+this.energy+", Milk: "+this.milk+", Breed: "+this.breed); } }​
מישהו מוצא את הבעיה
 

Zack DA

New member
../images/Emo13.gif נו באמת.......

אתה כותב קוד בתוך ה- test שלך, והוא אפילו לא בתוך מתודה....... ואגב - תפסיק להשתמש ב- this כל הזמן, זה ממש מעצבן ולא קריא.
 

haunter

New member
LOL ../images/Emo67.gif

כל כך עסקתי במחלקות עצמן ששכחתי מזה! תודה! וצודק בקשר לthis, אבל זה משהו ללימודים, וככה המורה מעדיפה o_O
 

haunter

New member
טוב ככה התרגלתי...

זה באמת די מיותר, אעשה את זה פחות בעתיד, פשוט ככה אני רגיל...
 

vinney

Well-known member
זה לא רק מעצבן

אתה שם פרמטרים לפונקציה עם אותו שם כמו מתודה בתוך המחלקה, ולכן אתה חייב להשתמש ב this. זה תכנות לא יפה ופתח לשגיאות, תן שמות שונים לפרמטרים למתודות ולמשתנים בתוך המחלקה.
 
למעלה