i defined a class TestoMessaggi and a subclass called Messaggio
//esercizio 3.3 del libro
import javax.swing.JOptionPane;
public class TestoMessaggio {
private String code;
private String testo;
public TestoMessaggio(String c, String t) {
code = c;
testo = t;
}
public static TestoMessaggio creaTestoMessaggio() {
String co = JOptionPane.showInputDialog(null,"inserisci codice");
String te = JOptionPane.showInputDialog(null,"inserisci testo");
TestoMessaggio t1 = new TestoMessaggio(co,te);
return t1;
}
public String getCode() {
return code;
}
public String getTesto() {
return testo;
}
}
Here is Messaggio.class
public class Messaggio extends TestoMessaggio {
private String mittente;
private String destinatario;
public Messaggio(String c, String t,String m, String d) {
super(c,t);
mittente = m;
destinatario = d;
}
public String getDestinatario() {
return destinatario;
}
public String getMittente() {
return mittente;
}
public void setDestinatario(String d) {
destinatario = d;
}
public static void stampaMessaggio(Messaggio m) {
System.out.println("code : "+m.getCode());
System.out.println("testo : "+m.getTesto());
System.out.println("destinatario : " +m.getDestinatario());
System.out.println("mittente : " +m.getMittente());
}
}
i created a program to test the two classes: here's the code
//esercizio 3.5 del libro
import javax.swing.JOptionPane;
public class Esempio3_5 {
public static String leggiNumero() {
String num = JOptionPane.showInputDialog(null,"inserisci numero");
return num;
}
public static void main(String[] args) {
String m = leggiNumero();
TestoMessaggio t1 = creaTestoMessaggio(); // non trova il metodo
String d = leggiNumero();
Messaggio mex = new Messaggio(null,null, m,d);
stampaMessaggio(mex); // nn trova il metodo
}
}
when i try to compile i get this error
Esempio3_5.java:16: error: cannot find symbol
TestoMessaggio t1 = creaTestoMessaggio(); // non trova il metodo
^
symbol: method creaTestoMessaggio()
location: class Esempio3_5
Esempio3_5.java:19: error: cannot find symbol
stampaMessaggio(mex); // nn trova il metodo
^
symbol: method stampaMessaggio(Messaggio) location: class Esempio3_5
all 3 files are in the same directory. Any suggestions? Thanks in advance
it is not getting the functions from the other classes. try to associate them using dot(.)opertaor like
TestoMessaggio t1 = TestoMessaggio.creaTestoMessaggio();
Your error says:
I don't see a
stampaMessagio()
method inEsempio3_5
, and neither does the compiler. When you call a method without a class name or instance before it, it calls the method in the current class/object.I'm presuming you want to call the static
stampaMessagio()
that's defined inMessagio
, so you'd need to actually do that:You need to access the static methods on the class name:
The code that you used:
is equivalent to:
since, you are using it in
static
context. Now, clearly you don't have that method inEsempio3_5
class, so it fails.Similarly, change the other line to:
But IMO, you should override
toString
method inMessaggio
instead of providing astatic stampaMessaggio()
method.