简体   繁体   English

泛型问题:clone()尝试分配较弱的访问权限

[英]Generics issue: clone() attempting to assign weaker access privileges

Let's have this class structure: 让我们有这个类结构:

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);
   }
}

I have this compilation error (although the IDE, Intellij in my case, is unable to show the error while coding) 我有这个编译错误(虽然我的情况下IDE,Intellij,编码时无法显示错误)

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

I know that the compiler is trying to call clone() method from Object instead of TypeCloneable , but I don't understand why. 我知道编译器试图从Object而不是TypeCloneable调用clone()方法,但我不明白为什么。 I also tried it casting to TypeCloneable (I suposed the compiler would know which clone() method call in this case, but the same problem). 我也尝试过将其转换为TypeCloneable (我认为编译器会知道在这种情况下调用哪个clone()方法,但同样的问题)。

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

I'm a bit confused...Can I do something here to force callling clone() from TypeCloneable? 我有点困惑......我可以在这里做些什么来强制从TypeCloneable调用clone()吗?

Thanks for the hellp 谢谢你的帮助

This works for me, (I am guessing it is a problem with the Type & Type upperbound syntax): 这适用于我,(我猜测它是Type&Type上限语法的问题):

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);
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android AsyncTask错误,尝试分配较弱的访问权限。 - Android AsyncTask error, attempting to assign weaker access privileges. 无法覆盖 Fragment 中的 onResume() 尝试分配较弱的访问权限; 是公开的 - Cannot override onResume() in Fragment attempting to assign weaker access privileges; was public 试图分配较弱的访问权限; 是isCancelled的公开错误 - attempting to assign weaker access privileges; was public error for isCancelled Android Studio:房间数据库实施时出现“尝试分配较弱的访问权限”错误 - Android Studio: "attempting to assign weaker access privileges" error on Room Database implementation java试图分配较弱的访问权限错误 - java attempting to assign weaker access privilege error 为什么java不允许为子类中的静态方法分配较弱的访问权限? - Why doesn't java allow to assign weaker access privileges to static methods in a child class? Java:“试图分配较弱的访问特权错误”(使用1.8编译JDK 1.6 Source) - Java: “attempting to assign weaker access privilege error” (Compiling JDK 1.6 Source with 1.8) 分配对从超类覆盖的方法的较弱访问 - Assign weaker access to method overriding from superclass 为什么我们不能在子类中分配较弱的权限 - why can't we assign weaker privilege in subclass 在泛型集合上进行克隆 - Making clone on a collection of generics
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM