Λειτουργία Getclass στην Java

Συζητήσεις για θέματα που σχετίζονται με software.
Post Reply
vrasmospm
Buffer underflow exception
Buffer underflow exception
Posts: 2
Joined: Sun May 29, 2011 1:01 pm

Λειτουργία Getclass στην Java

Post by vrasmospm » Sun May 29, 2011 1:11 pm

Γνωρίζει κάποιος ακριβώς την λειτουργία της getclass στην Java;;;
Συγκεκριμένα θέλω να εκτυπώσω όλα τα περιεχόμενα μιας από τις 5 κλάσεις που είναι μέσα σε μία λίστα. Γνωρίζει κανείς πώς μπορώ να το κάνω;;
Ευχαριστώ πολύ.
User avatar
necrofear_13
Wow! Terabyte level
Wow! Terabyte level
Posts: 2141
Joined: Sun Nov 18, 2007 5:32 am
Academic status: Alumnus/a
Gender:
Location: ΗΡΑΚΛΕΙΟ ΑΤΤΙΚΗΣ

Re: Λειτουργία Getclass στην Java

Post by necrofear_13 » Sun May 29, 2011 1:15 pm

Καλύτερα δοκίμασε με instanceof αντί με getClass(). Google it a bit και θα βρεις άκρη εύκολα.
No matter what kind of sadness envelops the darkness of our despair, if you put one foot in front of the other, at some point you will reach a place where you can see the light.

In heaven or earth, no matter to where we progress, no matter where we walk, that becomes our road.
As long as we keep walking the battle will continue.
And that is exactly why this place will always be a paradise
User avatar
Loner
Venus Former Team Member
Posts: 4004
Joined: Fri Oct 26, 2007 11:08 pm
Academic status: N>4
Gender:
Location: Στη ρωγμή του χρόνου

Re: Λειτουργία Getclass στην Java

Post by Loner » Sun May 29, 2011 6:19 pm

Με μια πρόχειρη αναζήτηση και αν κατάλαβα καλά τι ζητάς, βρήκα αυτό. Εν συντομία:

Code: Select all

AwesomeClass className= new AwesomeClass();
Field[] fields = className.getClass().getDeclaredFields();
for ( Field field : fields  ) {
     System.out.print("Variable Name: "+field.getName()+", value: "+field.get(className));
}     
AwesomeClass είναι το όνομα της τάξης σου.
Το field.get(className) δουλεύει μόνο αν είναι public οι μεταβλητές στην AwesomeClass σου :smt016

Πιο απλό είναι να φτιάξεις μία toString μέσα στην class σου,δεν είναι καθόλου δύσκολο,δες εδώ. :)


Για την getDeclaredFields()
Spoiler: εμφάνιση/απόκρυψη
[quote]getDeclaredFields public Field[] getDeclaredFields() throws SecurityException Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object. This includes public, protected, default (package) access, and private fields, but excludes inherited fields. The elements in the array returned are not sorted and are not in any particular order. This method returns an array of length 0 if the class or interface declares no fields, or if this Class object represents a primitive type, an array class, or void.[/quote] http://download.oracle.com/javase/1,5.0/docs/api/java/lang/Class.html#getDeclaredFields()
You either die a Spongebob or live long enough to see yourself become the Squidward.
vrasmospm
Buffer underflow exception
Buffer underflow exception
Posts: 2
Joined: Sun May 29, 2011 1:01 pm

Re: Λειτουργία Getclass στην Java

Post by vrasmospm » Sun May 29, 2011 6:34 pm

Σόρρυ αν δεν μπορώ να καταλάβω ακριβώς κάποια πράγματα.
Λοιπόν σε μία λίστα μπορούμε να αποθηκεύσουμε δεδομένα τύπου Item. Δηλαδή πχ κατάστημα υπολογιστών, μέσα στο κατάστημα υπάρχουν lap top και desktop. Στα laptop υπάρχουν τα 2πύρηνα και τα τετραπύρηνα, στα desktop τα 4πύρηνα και τα 8 πύρηνα.
Πώς θα μπορέσω εγώ από τη λίστα η οποία τα περιέχει όλα να εκτυπώσω μόνο τα laptop ή μόνο τα 8πύρηνα(που αναγκαστικά είναι desktop);;
Ελπίζω να έγινα κατανοητός.
User avatar
Zifnab
Venus Former Team Member
Posts: 7581
Joined: Tue Nov 15, 2005 2:42 am
Academic status: MSc
Gender:
Location: Connecticut
Contact:

Re: Λειτουργία Getclass στην Java

Post by Zifnab » Sun May 29, 2011 7:27 pm

vrasmospm wrote:Σόρρυ αν δεν μπορώ να καταλάβω ακριβώς κάποια πράγματα.
Λοιπόν σε μία λίστα μπορούμε να αποθηκεύσουμε δεδομένα τύπου Item. Δηλαδή πχ κατάστημα υπολογιστών, μέσα στο κατάστημα υπάρχουν lap top και desktop. Στα laptop υπάρχουν τα 2πύρηνα και τα τετραπύρηνα, στα desktop τα 4πύρηνα και τα 8 πύρηνα.
Πώς θα μπορέσω εγώ από τη λίστα η οποία τα περιέχει όλα να εκτυπώσω μόνο τα laptop ή μόνο τα 8πύρηνα(που αναγκαστικά είναι desktop);;
Ελπίζω να έγινα κατανοητός.

Λοιπόν

Έστω ότι έχεις, με βάση την περιγραφή σου (και κάπως διορθωμένα καθώς δεν θα κάνεις νέα κλαση για κάθε αριθμό πυρήνων):

Code: Select all

class Katastima {
   List<MyItem> mylist= new List<MyItem>();

}
Και έστω

Code: Select all

class MyItem { }

class Laptop extends MyItem() {
  public int numofcores;
  public void toString() {
     return "Eimai ena Item";
  }
}

class Desktop extends MyItem() {
   public int numofcores;
}
Θα κάνεις

Code: Select all

for(item : mylist) {
   //έλεγχος item
}
Για έλεγχο του item βάζεις κάτι σαν τα παρακάτω:

Για να βρεις τα Laptop και όλες τις κλάσσεις που τα κάνουν extend

Code: Select all

if (item instanceof Laptop)  { System.out.println(item); }
Αν θές να βρεις τα αντικείμενα που είναι κλάσσης Laptop

Code: Select all

if (item.getClass()=="Laptop.class") {  System.out.println(item); }
Αν θες πχ Desktop και 8πύρηνα

Code: Select all

if (item instanceof Desktop && item.numofcores==8) {  System.out.println(item); }
Οι κώδικες μπορεί να περιέχουν συντακτικά λάθη, γιατί έχω να γράψω java κανένα χρόνο :-p
User avatar
Loner
Venus Former Team Member
Posts: 4004
Joined: Fri Oct 26, 2007 11:08 pm
Academic status: N>4
Gender:
Location: Στη ρωγμή του χρόνου

Re: Λειτουργία Getclass στην Java

Post by Loner » Sun May 29, 2011 7:43 pm

Ωραίος Zifnab,ετοίμαζα κάτι παρόμοιο αλλά με πρόλαβες :smt016 :smt023 :smt023
You either die a Spongebob or live long enough to see yourself become the Squidward.
User avatar
netharis
Venus Former Team Member
Posts: 1845
Joined: Sun Jul 15, 2007 2:37 pm
Academic status: Alumnus/a
Gender:
Location: Seattle, Washington
Contact:

Re: Λειτουργία Getclass στην Java

Post by netharis » Sun May 29, 2011 9:00 pm

Zifnab wrote:

Code: Select all

foreach(item : mylist) {
   //έλεγχος item
}
Ι'm pretty sure "foreach" is not a valid keyword :-)
Image
Resistance is futile. Join the foss-side now!
"UNIX is very simple, it just needs a genius to understand its simplicity." - Dennis Ritchie
"The things that I want, by Max Payne. A smoke. A whiskey. For the sun to shine. I want to sleep, to forget. To change the past. Unlimited ammo and a license to kill. But right then, more than anything, I wanted her."
User avatar
Zifnab
Venus Former Team Member
Posts: 7581
Joined: Tue Nov 15, 2005 2:42 am
Academic status: MSc
Gender:
Location: Connecticut
Contact:

Re: Λειτουργία Getclass στην Java

Post by Zifnab » Sun May 29, 2011 9:57 pm

netharis wrote:
Zifnab wrote:

Code: Select all

foreach(item : mylist) {
   //έλεγχος item
}
Ι'm pretty sure "foreach" is not a valid keyword :-)
PHP speaks in me :lol: :lol:
fixed it ;)
Post Reply

Return to “Software”