英文:
Assigning a variable of type class to a different class that extends the declared variable type
问题
这样我已经声明了:
Class<ThemeControls> thisClass = ThemeControls.class;
我想通过一个继承了 ThemeControls
的单独类来访问这个类内部的方法和变量。这个类是:
public class BaseControls extends ThemeControls { ... }
当使用以下代码行时会出现错误:
thisClass = BaseControls.class;
错误信息:
不兼容的类型
因为 BaseContols
继承了 ThemeControls
,所以它可以被声明为相同的类型吗?
英文:
So I have declared this:
Class<ThemeControls> thisClass = ThemeControls.class;
I would like to access the methods and variables within this class through a separate class that extends ThemeControls
. This class:
public class BaseControls extends ThemeControls { ... }
An error this thrown when using this line of code:
thisClass = BaseControls.class;
Error:
Incompatible types
Since BaseContols extends ThemeControls, it can be declared as the same type?
答案1
得分: 3
如Class<BaseControls>
不是准确的Class<ThemeControls>
。您需要使用Class<? extends ThemeControls>
。
所以:
Class<? extends ThemeControls> thisClass = BaseControls.class;
英文:
As Class<BaseControls>
is not exact Class<ThemeControls>
. You need to use Class<? extends ThemeControls>
.
So:
Class<? extends ThemeControls> thisClass = BaseControls.class;;
答案2
得分: 1
这对你来说必须有效:
Class<? extends ThemeControls> thisClass =...
英文:
This must work for you:
Class<? extends ThemeControls> thisClass =...
专注分享java语言的经验与见解,让所有开发者获益!
评论