2016-07-20 16 views
1

Der folgende Code stammt von LinkedList.java in 1.8 Version. Ich kann nicht verstehen, warum this() heißt? Der Konstruktor LinkedList() scheint nichts zu tun?Warum rufen einige Konstruktoren mit Parameter noch dieses() auf? Es scheint nichts zu tun?

/** 
* Constructs an empty list. 
*/ 
public LinkedList() { 
} 

/** 
* Constructs a list containing the elements of the specified 
* collection, in the order they are returned by the collection's 
* iterator. 
* 
* @param c the collection whose elements are to be placed into this list 
* @throws NullPointerException if the specified collection is null 
*/ 
public LinkedList(Collection<? extends E> c) { 
    this(); 
    addAll(c); 
} 
+0

das ruft einfach den Standardkonstruktor – Stultuske

+0

Wo instanziieren Sie die Klasse? –

+0

Nur Spekulation, aber ich könnte mir vorstellen, es ist eine Bestimmung für zukünftige Versionen. Vielleicht wird der Default-Ctor irgendwann "etwas tun". Dann haben Sie bereits den parametrierten ctor mit festgelegt. – Fildor

Antwort

0

Sie haben Recht. Es tut nichts. Der leere Konstruktor ruft standardmäßig super() auf. Ohne this() ruft der zweite Konstruktor standardmäßig auch super() auf. Meine Vermutung ist, dass, sobald in LinkedList() initialisiert wurde, und durch Aufruf von this() diese Initialisierung auch in LinkedList(Collection<? extends E> c) verwendet wird. Und jetzt ist es ein nutzloser und harmloser Rest.