Odpowiedź jest ustawiona na Nie. Zmienna referencyjna cFrame wywoła konstruktor inny niż arg Test(), a nie domyślny konstruktor. Podczas gdy konstruktor Test() odnajdzie i wywoła nieargumentowy konstruktor klasy JFrame (Parent) i tak dalej, Każda klasa musi mieć przynajmniej jeden konstruktor. Jeśli nie jest jawnie zadeklarowany, kompilator java udostępnia niepasametryzowany konstruktor, tj. Konstruktor domyślny. Ten domyślny konstruktor wywołuje nieparametryzowany konstruktor klasy macierzystej. Inicjuje zmienne klasy do ich wartości domyślnych.
Np
Class Base {}
Class Derived extends Base {} // Default constructor of class "Derived" will automatically calls non-arg constructor of class "Base" and intialzes value to the variables
Kiedy konstruktor nie Arg zdefiniowany jedynie przez programistę. Może również inicjować zmienne. Jeszcze jeden punkt do dodania to to, że super() wywołanie jest automatycznie dodawane przez kompilator java, jeśli nie znajdzie super() w klasie pochodnej.
Np
Class Base {
int y;
public Base() {
}
public int getY() {
return y;
}
}
public class Derived extends Base {
private int x;
public Derived() { //super() will be automatically used
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
}
Derived d = new Derived();
System.out.println("Base x value => " + d.getX());
System.out.println("Base y value => " + d.getY());
Result:
Base x value => 0 // Default value to its primitive datatype(In this case: int)
Base y value => 0
no nie wywołać konstruktor domyślny teraz nie ma domyślnego konstruktora –
Proszę odnieść się również do [Java] konstruktor domyślny (http://stackoverflow.com/questions/4488716/java -default-konstruktor) –
@CodeGeek dzięki –