2015-10-20 10 views
5

der diese Klasse Struktur Lassen Sie haben:Generics Ausgabe: clone() versuchen, schwächere Privilegien Zugriff zuweisen

public interface TypeIdentifiable {} 

public interface TypeCloneable extends Cloneable { 
    public Object clone() throws CloneNotSupportedException; 
} 

public class Foo implements TypeCloneable, TypeIdentifiable { 

    @Override 
    public Object clone() throws CloneNotSupportedException { 
     // ... 
     return null; 
    } 
} 

public abstract class AbstractClass<T extends TypeCloneable & TypeIdentifiable> { 

    public void foo(T element) throws Exception { 
     TypeCloneable cloned = (TypeCloneable) element.clone(); 
     System.out.println(cloned); 
    } 
} 

ich dieses Übersetzungsfehler haben (obwohl die IDE, IntelliJ in meinem Fall nicht in der Lage ist, den Fehler zu zeigen, während

Codierung)
Error:(4, 37) java: clone() in java.lang.Object cannot implement clone() in foo.TypeCloneable attempting to assign weaker access privileges; was public 

ich weiß, dass der Compiler clone() Methode von Object statt TypeCloneable, zu nennen versucht, aber ich verstehe nicht, warum. Ich habe es auch versucht, Casting TypeCloneable (ich legte den Compiler würde wissen, welche clone() Methode Aufruf in diesem Fall, aber das gleiche Problem).

public void foo(T element) throws Exception { 
     TypeCloneable typeCloneable = (TypeCloneable) element; 
     TypeCloneable cloned = (TypeCloneable) typeCloneable.clone(); 
    } 

Ich bin ein bisschen verwirrt ... Kann ich hier etwas tun Klon zu zwingen, callling() von TypeCloneable?

Danke für die hellp

+1

Wenn ich die JLS richtig zu verstehen, sollte dies kompilieren . Ich denke, es ist ein Fehler. http://stackoverflow.com/questions/33023332/why-doesnt-this-java-program-compile –

+1

Also ja ... es scheint ein Fehler zu sein. Ich habe diese Frage nicht gefunden. Vielen Dank! – troig

Antwort

2

Dies funktioniert für mich, (ich bin zu raten, ist ein Problem mit dem Typ & Typ Oberegrenze Syntax):

interface TypeIdentifiable {} 

interface TypeCloneable extends Cloneable { 
    public Object clone() throws CloneNotSupportedException; 
} 

class Foo implements TypeCloneable, TypeIdentifiable { 

    @Override 
    public Object clone() throws CloneNotSupportedException { 
     // ... 
     return null; 
    } 
} 

interface TypeCloneableAndIndetifiable extends TypeCloneable, TypeIdentifiable { 

} 
abstract class AbstractClass<T extends TypeCloneableAndIndetifiable> { 

    public void foo(T element) throws Exception { 
     TypeCloneable cloned = (TypeCloneable) element.clone(); 
     System.out.println(cloned); 
    } 
} 
+0

Danke für die Beantwortung @WillShackleford. Ja, wenn ich beide Schnittstellen zu einem zusammenfasse, wie du sagst, funktioniert es. Das Problem ist, dass sowohl TypeCloneable als auch TypeIdentifier Legacy-Code sind. Denkst du, das könnte ein Bug mit der & Type-Oberbound-Syntax sein? – troig

+0

Ich bin mir nicht sicher. Ich hätte gedacht, es würde funktionieren, so wie du es benutzt hast, bevor ich diese Frage gesehen habe. – WillShackleford

+0

Wie @Paul Boddington in einem Kommentar zeigt, scheint es ein Fehler zu sein. Vielleicht könnten Sie Ihre Antwort bearbeiten, indem Sie dies hinzufügen. Wie auch immer, ich akzeptiere Ihre Antwort auf Ihren Vorschlag, zwei Interfaces zu einem zu machen. Vielen Dank – troig