|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Member
Iscritto dal: Oct 2004
Città: Bologna
Messaggi: 50
|
[JAVA] Istanziare classi statiche?
Salve a tutti,
ho un pezzo di codice di esempio che non riesco a comprendere perchè fa cose che non credevo possibili con Java. Il fatto è questo: ho una classe statica BindingManager che ha un metodo statico create() che ritorna un new BindingManager(). Già qui è strano perchè così capisco che sta istanziando una classe statica e qualcosa non mi torna... ma soprattutto il new ha una forma che non avevo mai visto: new BindingManager() { /*... metodi ...*/ }; La parte di codice oscura è quella tra i commenti // ?!?, il resto l'ho postato solo per contesto. Qualcuno saprebbe dirmi cosa significa o almeno indicarmi un riferimento per cercare sul Java Language Specification? Codice:
public static class BindingManager {
public static BindingManager create() {
return new BindingManager() { // ?!?
public void exitScope() {
throw new IllegalStateException("exitScope from top level");
}
public boolean contains(String identifier) {
return map.containsKey(identifier);
}
public Literal getValue(String identifier) {
return (Literal) map.get(identifier);
}
}; // ?!?
}
private BindingManager enclosingScope;
protected Map map = new HashMap();
private BindingManager() {
}
private BindingManager(BindingManager enclosingScope, Map map) {
this.enclosingScope = enclosingScope;
this.map = map;
}
public void enterScope() {
this.enclosingScope = new BindingManager(enclosingScope, map);
this.map = new HashMap();
}
public void exitScope() {
this.enclosingScope = enclosingScope.enclosingScope;
this.map = enclosingScope.map;
}
public boolean contains(String identifier) {
return map.containsKey(identifier) ? true : enclosingScope.contains(identifier);
}
public void bind(String identifier, Literal value) {
map.put(identifier, value);
}
public Literal getValue(String identifier) {
Literal value = (Literal) map.get(identifier);
return (value != null) ? value : enclosingScope.getValue(identifier);
}
}
__________________
And the salad is frightful! I have an important message to deliver to all the cute people all over the world. If you're out there and you're cute, maybe you're beautiful. I just want to tell you something: there's more of us ugly mother-fuckers than you are, hey-y, so watch out. |
|
|
|
|
|
#2 | |
|
Senior Member
Iscritto dal: Apr 2003
Città: Genova
Messaggi: 4739
|
Quote:
una funzione membro static è in effetti un membro della classe in sè piuttosto che un membro di un oggetto istanza di quella classe, quindi può essere invocata anche se non sono stati già creati oggetti di quella classe, e uno delle applicazioni più utili è appunto la loro (o sua, quando l' esecuzione del programma prevede un solo oggetto - ad es nel pattern singleton) creazione una funzione static però, quando la classe non è ancora stata istanziata, può manipolare variabili membro e chiamare altre funzioni membro, solo se anche questi sono dichiarati static... tutto, se non ricordo male... devo rivedere in diamonds se come e quando si usano le classi statiche...
__________________
Jappilas is a character created by a friend for his own comic - I feel honored he allowed me to bear his name Saber's true name belongs to myth - a Heroic Soul out of legends, fighting in our time to fullfill her only wish Let her image remind of her story, and of the emotions that flew from my heart when i assisted to her Fate
Ultima modifica di jappilas : 18-06-2006 alle 00:04. |
|
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Feb 2003
Città: GE
Messaggi: 397
|
new BindingManager() { /*... metodi ...*/ };
server semplicemente ad istanziare una classe anonima interna che ridefinisce i /* metodi */. suc ome funzioni una classe statica mi dovrei documentare meglio.
__________________
La supposizione e' la madre di tutte le ca**ate! |
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Nov 2004
Città: Tra Verona e Mantova
Messaggi: 4553
|
La forma che ti appare strana è la creazione di un'istanza di una classe internal locale anonima. E' la contrazione di:
Codice:
public static BindingManager create() {
class Anomina extends BindingManager() {
public void exitScope() {
throw new IllegalStateException("exitScope from top level");
}
public boolean contains(String identifier) {
return map.containsKey(identifier);
}
public Literal getValue(String identifier) {
return (Literal) map.get(identifier);
}
};
return new Anonima();
}
Quanto al resto, BindingManager è un Tipo annidato. I tipi annidati esistono in un contesto statico e il contesto statico è privo di un "oggetto di riferimento" (che in un contesto non statico è rappresentato da "this"). Mancando la definizione implicita dell'oggetto che possiede i membri è chiaro che non sia possibile accedere ad alcun membro a meno che non si specifichi quale sia l'oggetto che li possieda, preponendo il relativo riferimento. Circa il significato complessivo di quel codice, è una riduzione del pattern Factory: c'è il momento della produzione, espresso dal metodo (personalmente penso che i metodi statici non siano metodi ma non posso dirlo |
|
|
|
|
|
#5 | |
|
Member
Iscritto dal: Oct 2004
Città: Bologna
Messaggi: 50
|
Quote:
__________________
And the salad is frightful! I have an important message to deliver to all the cute people all over the world. If you're out there and you're cute, maybe you're beautiful. I just want to tell you something: there's more of us ugly mother-fuckers than you are, hey-y, so watch out. |
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 04:53.











devo rivedere in diamonds se come e quando si usano le classi statiche...








