|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Nov 2006
Città: Pianezza(TO)
Messaggi: 1250
|
[JAVA] PROBLEMA PACKAGE PER IMPORTARE UNA CLASSE DALL'ESTERNO DEL PACKAGE
Ho un problema non riesco a importare una classe creata da me(chiaramente è pubblica la classe) dall' esterno del package in cui mi trovo.
esempio specifico: ho creato la cartella "c07" dove risiede il file(Music3.java) che dovrebbe ricevere la classe importata, in questa cartella vi è un altra sottodirectory "musi" dove all'interno si trova la classe pubblica da importare(Note.java). potrebba essere un problema di classpath(il compilatore funziona perfettamente)? codice Music3.java: Codice:
package c07.music3;
import c07.musi.Note;
import java.util.*;
class Instrument {
public void play(Note n){
System.out.println("Instrument.play() " +n);
}// fine metodo play
String what() {
return "Instrument";
}
void adjust() {}
}// fine classe Instrument
class Wind extends Instrument {
void play(Note n){
System.out.println("Wind.play() " +n);
}
String what() {return "Wind"; }
void adjust() {}
}// fine classe Wind
class Percussion extends Instrument {
void play(Note n) {
System.out.println("Percussion.play() " + n);
}
String what() { return "Percussion"; }
void adjust() {}
}// fine classe Percussion
class Stringed extends Instruments {
void play(Note n){
System.out.println("Stringed.play() " + n);
}
String what() { return "Stringed"; }
void adjust() {}
}// fine classe Stringed
class Brass extends Wind {
void play(Note n) {
System.out.println("Brass.play()" + n);
}// fine metodo play
void adjust(){
System.out.println("Brass.adjust()");
}// fine metodo adjust
}// fine classe Brass
class Woodwind extends Wind {
void play(Note n){
System.out.println("Woodwind.play() " + n);
}
String what(){ return "Woodwind"; }
}// fine classe Woodwind
public class Music3 {
// non si preoccupa del tipo, quindi i nuovi tipi
// aggiunti al sistema funzionano corrattamente:
public static void tune(Instrument i){
//..
i.play(Note.MIDDLE_C);
}
public static void tuneAll(Instrument[] e){
for(int i=0; i < e.length;i++)
tune(e[i]);
}
public static void main(String[] args){
//Upcasting durante l'aggiunta all'array
Instrument[] orchestra = {
new Wind(),
new Percussion(),
new Stringed(),
new Brass(),
new Woodwind()
};
tuneAll(orchestra);
System.out.println(new String[] {
"Wind.play() Middle C",
"Percussion.play() Middle C",
"Stringed.play() Middle C",
"Brass.play() Middle C",
"Woodwind.play() Middle C"
});
} // fine main
} // fine classe Music3
codice Note.java: Codice:
package c07.musi;
public class Note {
private String noteName;
private Note(String noteName){
this.noteName = noteName;
}
public String toString() {return noteName; }
public static final Note
MIDDLE_C = new Note("Middle C"),
C_SHARP = new Note("C Sharp"),
B_FLAT = new Note("B Flat");
//Ecc.
}// fine classe Note
grazie
__________________
il mio pc ASUS Z97-PRO - INTEL CORE I7-4790 3.6 ghz - CORSAIR HX620W - G. SKILL F3-2400C11Q-32GXM(XMP) 32GB(4X8GB) DS 11-13-13-31 1.65v - 2TB Toshiba DT01ACA200 - 3TB Toshiba P300 HDWD130UZSVA - 4TB Toshiba Canvio Basics HDTB440EK3CA - PIONEER BDR2209 - THERMALTAKE ARMOR MX Silver VH8000SWA - NETGEAR DGN3500 - LOGITECH G51 trattative sul mercatino:http://cristian3371.altervista.org/Acquisti.html acquisti e vendite su ebayhttp://myworld.ebay.it/cristian3371 Ultima modifica di cristian3371 : 28-09-2008 alle 12:46. |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Sep 2005
Città: Torino
Messaggi: 606
|
Modifica il tuo post e metti il codice tra i tag "code", in modo da tenere una corretta formattazione.
Che succede?ti da errore a compile-time o a runtime? qual'è l'errore? usi un IDE o da linea di comando?
__________________
"Se proprio dovete piratare un prodotto, preferiamo che sia il nostro piuttosto che quello di qualcun altro." [Jeff Raikes] "Pirating software? Choose Microsoft!" |
|
|
|
|
|
#3 | |
|
Senior Member
Iscritto dal: Nov 2006
Città: Pianezza(TO)
Messaggi: 1250
|
Quote:
ciao
__________________
il mio pc ASUS Z97-PRO - INTEL CORE I7-4790 3.6 ghz - CORSAIR HX620W - G. SKILL F3-2400C11Q-32GXM(XMP) 32GB(4X8GB) DS 11-13-13-31 1.65v - 2TB Toshiba DT01ACA200 - 3TB Toshiba P300 HDWD130UZSVA - 4TB Toshiba Canvio Basics HDTB440EK3CA - PIONEER BDR2209 - THERMALTAKE ARMOR MX Silver VH8000SWA - NETGEAR DGN3500 - LOGITECH G51 trattative sul mercatino:http://cristian3371.altervista.org/Acquisti.html acquisti e vendite su ebayhttp://myworld.ebay.it/cristian3371 |
|
|
|
|
|
|
#4 | |
|
Member
Iscritto dal: Jul 2005
Città: Potenza
Messaggi: 126
|
Quote:
In pratica dovresti lanciare il comando javac dalla cartella c07 o da quella superiore: Es. javac c07/sottocartella/NomeClasse.java
__________________
Un'interfaccia per domarli, un package per trovarli tutti, un riferimento per ghermirli e nel framework incatenarli." Il Signore degli Oggetti
|
|
|
|
|
|
|
#5 | |
|
Senior Member
Iscritto dal: Nov 2006
Città: Pianezza(TO)
Messaggi: 1250
|
Quote:
__________________
il mio pc ASUS Z97-PRO - INTEL CORE I7-4790 3.6 ghz - CORSAIR HX620W - G. SKILL F3-2400C11Q-32GXM(XMP) 32GB(4X8GB) DS 11-13-13-31 1.65v - 2TB Toshiba DT01ACA200 - 3TB Toshiba P300 HDWD130UZSVA - 4TB Toshiba Canvio Basics HDTB440EK3CA - PIONEER BDR2209 - THERMALTAKE ARMOR MX Silver VH8000SWA - NETGEAR DGN3500 - LOGITECH G51 trattative sul mercatino:http://cristian3371.altervista.org/Acquisti.html acquisti e vendite su ebayhttp://myworld.ebay.it/cristian3371 |
|
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Nov 2006
Città: Pianezza(TO)
Messaggi: 1250
|
dato che mi sono rotto ho messo tutto in un unico file e funziona tutto(come mi aspettavo) ovviamente togliendo l'import che mi dava dei problemi, però è il fatto che non continuo a capire come mai non mi importava quella classe pubblica dall'esterno del package in cui mi trovavo
__________________
il mio pc ASUS Z97-PRO - INTEL CORE I7-4790 3.6 ghz - CORSAIR HX620W - G. SKILL F3-2400C11Q-32GXM(XMP) 32GB(4X8GB) DS 11-13-13-31 1.65v - 2TB Toshiba DT01ACA200 - 3TB Toshiba P300 HDWD130UZSVA - 4TB Toshiba Canvio Basics HDTB440EK3CA - PIONEER BDR2209 - THERMALTAKE ARMOR MX Silver VH8000SWA - NETGEAR DGN3500 - LOGITECH G51 trattative sul mercatino:http://cristian3371.altervista.org/Acquisti.html acquisti e vendite su ebayhttp://myworld.ebay.it/cristian3371 |
|
|
|
|
|
#7 | |
|
Member
Iscritto dal: Jul 2005
Città: Potenza
Messaggi: 126
|
Quote:
la definizione del metodo nelle sottoclassi deve essere esattamente la stessa di quella della superclasse. In pratica, se nella classe Instrument hai un metodo public void play(Note n) { ... } nelle sottoclassi devi avere la stessa definizione // stesso tipo e numero di argomenti, il nome può essere diverso public void play(Note x) { ... } Nel tuo codice le sottoclassi hanno un metodo void play(Note n) che deve essere pubblico. Compilando con javac dalla cartella superiore a c07, con le opportune modifiche al codice, funziona tutto.
__________________
Un'interfaccia per domarli, un package per trovarli tutti, un riferimento per ghermirli e nel framework incatenarli." Il Signore degli Oggetti
|
|
|
|
|
|
|
#8 |
|
Member
Iscritto dal: Jul 2005
Città: Potenza
Messaggi: 126
|
Per chiarezza posto le classi corrette:
Codice:
package c07.music3;
import c07.musi.Note;
import java.util.*;
class Instrument {
public void play(Note n){
System.out.println("Instrument.play() " +n);
}// fine metodo play
String what() {
return "Instrument";
}
void adjust() {}
}// fine classe Instrument
class Wind extends Instrument {
public void play(Note n){
System.out.println("Wind.play() " +n);
}
String what() {return "Wind"; }
void adjust() {}
}// fine classe Wind
class Percussion extends Instrument {
public void play(Note n) {
System.out.println("Percussion.play() " + n);
}
String what() { return "Percussion"; }
void adjust() {}
}// fine classe Percussion
class Stringed extends Instrument {
public void play(Note n){
System.out.println("Stringed.play() " + n);
}
String what() { return "Stringed"; }
void adjust() {}
}// fine classe Stringed
class Brass extends Wind {
public void play(Note n) {
System.out.println("Brass.play()" + n);
}// fine metodo play
void adjust(){
System.out.println("Brass.adjust()");
}// fine metodo adjust
}// fine classe Brass
class Woodwind extends Wind {
public void play(Note n){
System.out.println("Woodwind.play() " + n);
}
String what(){ return "Woodwind"; }
}// fine classe Woodwind
public class Music3 {
// non si preoccupa del tipo, quindi i nuovi tipi
// aggiunti al sistema funzionano corrattamente:
public static void tune(Instrument i){
//..
i.play(Note.MIDDLE_C);
}
public static void tuneAll(Instrument[] e){
for(int i=0; i < e.length;i++)
tune(e[i]);
}
public static void main(String[] args){
//Upcasting durante l'aggiunta all'array
Instrument[] orchestra = {
new Wind(),
new Percussion(),
new Stringed(),
new Brass(),
new Woodwind()
};
tuneAll(orchestra);
System.out.println(new String[] {
"Wind.play() Middle C",
"Percussion.play() Middle C",
"Stringed.play() Middle C",
"Brass.play() Middle C",
"Woodwind.play() Middle C"
});
} // fine main
} // fine classe Music3
Codice:
package c07.musi;
public class Note {
private String noteName;
private Note(String noteName){
this.noteName = noteName;
}
public String toString() {return noteName; }
public static final Note
MIDDLE_C = new Note("Middle C"),
C_SHARP = new Note("C Sharp"),
B_FLAT = new Note("B Flat");
//Ecc.
}// fine classe Note
Codice:
javac c07/music3/Music3.java Alla fine il compilatore ti crea comunque tanti file .class diversi in base alla classi che trova, che risiedano nello stesso file o meno. Spero di esserti stato utile
__________________
Un'interfaccia per domarli, un package per trovarli tutti, un riferimento per ghermirli e nel framework incatenarli." Il Signore degli Oggetti
|
|
|
|
|
|
#9 | |
|
Senior Member
Iscritto dal: Nov 2006
Città: Pianezza(TO)
Messaggi: 1250
|
Quote:
__________________
il mio pc ASUS Z97-PRO - INTEL CORE I7-4790 3.6 ghz - CORSAIR HX620W - G. SKILL F3-2400C11Q-32GXM(XMP) 32GB(4X8GB) DS 11-13-13-31 1.65v - 2TB Toshiba DT01ACA200 - 3TB Toshiba P300 HDWD130UZSVA - 4TB Toshiba Canvio Basics HDTB440EK3CA - PIONEER BDR2209 - THERMALTAKE ARMOR MX Silver VH8000SWA - NETGEAR DGN3500 - LOGITECH G51 trattative sul mercatino:http://cristian3371.altervista.org/Acquisti.html acquisti e vendite su ebayhttp://myworld.ebay.it/cristian3371 |
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 17:40.




















