Witam
Napisałem mini programik cwiczac sobie jave:
Pierwsza klasa:
public class Próby
{
private int a;
private int b;
public Próby()
{
this(0,0);
}
public Próby(int a, int b)
{
this.a = a;
this.b = b;
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
public int obliczDl()
{
return a+b;
}
public String toString()
{
return "Suma długosci to: " + obliczDl();
}
}
Druga klasa:
public class PróbyC extends Próby
{
private int c;
public PróbyC()
{
this(0, 0, 0);
}
public PróbyC(int a, int b, int c)
{
super(a,b);
this.c = c;
}
public int obliczDl()
{
return ((super.getA()) + (super.getB()) + c);
}
// public String toString()
// {
// return "Suma długosci to: " + obliczDl();
// }
}
I chciałem spytać o dwie rzeczy:
- czy zapis: " return ((super.getA()) + (super.getB()) + c); " jest poprawny? chodzi mi tutaj o pole "c", musze zrobic dla niego setter i użyć?
No i czy wgl potrzebne są settery i gettery z klasy Próby? moze moge użyc "super.a" zamiast "super.getA()" i to samo z B?
2)Dlaczego mimo, że toString w klasie PróbyC jest zakomentowane, jak w klasie do testowania wywołam System.out.println(obiekt_klasy_ProbaC); to wyswietla sie metoda toString? jest ona dziedziczona w jakis sposob po klasie Próby?
pozdrawiam i prosze o wyrozumiałosc ;)