问题
Yo! I have a task with subclasses, where I gotta use the printible version of output, and I added the method toString(), however it seems that the method doesn't really apply to some part of the code.I post all three classes for this task: Class 1
public class Prostokat {
int szerokosc;
int wysokosc;
public Punkt origin;
public Prostokat () {
origin = new Punkt(0,0);
szerokosc = 0;
wysokosc = 0;
}
public Prostokat (Punkt p) {
origin = p;
}
public Prostokat (Punkt p, int a){
origin = p;
szerokosc = a;
wysokosc = a;
}
public Prostokat (Punkt p, int szerokosc, int wysokosc) {
origin = p;
this.szerokosc = szerokosc;
this.wysokosc = wysokosc;
}
public Prostokat (int x, int y, int szerokosc, int wysokosc) {
origin = new Punkt(x,y);
this.szerokosc = szerokosc;
this.wysokosc = wysokosc;
}
public int getSzerokosc() {
return szerokosc;
}
public void setSzerokosc(int szerokosc) {
this.szerokosc = szerokosc;
}
public int getWysokosc() {
return wysokosc;
}
public void setWysokosc(int wysokosc) {
this.wysokosc = wysokosc;
}
public Punkt getOrigin() {
return origin;
}
public void setOrigin(Punkt origin) {
this.origin = origin;
}
public String toString(Prostokat c) {
String res = "punkt: " + "(" + origin + ")" + "szerkosc: " +
szerokosc + "wysokosc: " + wysokosc;
return res;
}
public int obwod(Prostokat c) {
return 2*szerokosc + 2*wysokosc;
}
public int pole(Prostokat c) {
return szerokosc*wysokosc;
}
public void setOrigin(int i, int j) {
this.origin = origin;
}
public void setPunkt(Punkt p) {
this.origin = origin;
}
}
Then class 2 for Punkt
, where I also used this method:
package prostokatzadanie2;
public class Punkt {
public int j;
public int k;
public Punkt(int j, int k) {
this.j =j;
this.k = k;
}
public Punkt (int m) {
this.j =m;
this.k = m;
}
public String toString(Punkt p) {
String pim = j + "," + k;
return pim;
}
}
And then the main class, from where output should work:
package prostokatzadanie2;
public class zadanie2 {
public static void main(String[] args) {
Punkt p1 =new Punkt (1,5);
Punkt p2 = new Punkt (2);
Punkt p3 = new Punkt (18,6);
Prostokat pr1 = new Prostokat();
Prostokat pr2 = new Prostokat(p1,15);
Prostokat pr3 = new Prostokat(p2,10,6);
Prostokat pr4 = new Prostokat(0,75,10,4);
System.out.println("pr1: " + pr1.toString(pr1));
System.out.println("pr2: " + pr2.toString(pr2));
System.out.println("pr3: " + pr3.toString(pr3));
System.out.println("pr4: " + pr4.toString(pr4));
System.out.println("==================================================");
pr1.setWysokosc(26);
pr1.setOrigin(5,96);
pr2.setPunkt(p3);
System.out.println("pr1: " + pr1.toString(pr1));
System.out.println("pr2: " + pr2.toString(pr2));
System.out.println("pr3: " + pr3.toString(pr3));
int pr2Obwod = pr2.obwod(pr2);
System.out.println("Obwod pr2: " + pr2Obwod);
int pr3Pole = pr3.pole(pr3);
System.out.println("Pole pr3: " + pr3Pole);
}
}
So output is quite unreadable, which confuses me, where I might have forgotten to put the toString method correctly. This is the output:
pr1: punkt: (prostokatzadanie2.Punkt@139a55)szerkosc: 0wysokosc: 0
pr2: punkt: (prostokatzadanie2.Punkt@1db9742)szerkosc: 15wysokosc: 15
pr3: punkt: (prostokatzadanie2.Punkt@106d69c)szerkosc: 10wysokosc: 6
pr4: punkt: (prostokatzadanie2.Punkt@52e922)szerkosc: 10wysokosc: 4
==================================================
pr1: punkt: (prostokatzadanie2.Punkt@139a55)szerkosc: 0wysokosc: 26
pr2: punkt: (prostokatzadanie2.Punkt@1db9742)szerkosc: 15wysokosc: 15
pr3: punkt: (prostokatzadanie2.Punkt@106d69c)szerkosc: 10wysokosc: 6
Obwod pr2: 60
Pole pr3: 60
Thanks for your help. :)
回答1:
You overrode toString
incorrectly in both classes. The method to override in Object takes no parameters.
You don't use the parameter anyway, so just remove the parameter in both classes.
public String toString()
If you add the @Override
annotation to methods that are intended to override or implement a method from a superclass or superinterface, then the compiler will generate an error for you on failure to override, giving you information you need to easily see that the method didn't actually override what you intended.
回答2:
Use tab Source -> Generate toString()... in eclipse and no problem!
The same advice about Generating Constructor using Fields, Getters and Setters them locates in the same tab on panel.
来源:https://stackoverflow.com/questions/33792078/object-to-tostring-method-in-java-with-subclasses