Java Android-扩展泛型类
我有两个从基本抽象类(C)继承的类(A和B)。这个父类(C)为它的两个子类(A&B)实现了一些公共函数,并且必须从不同的类继承。所以,我决定让它成为Java Android-扩展泛型类,java,android,generics,inheritance,Java,Android,Generics,Inheritance,我有两个从基本抽象类(C)继承的类(A和B)。这个父类(C)为它的两个子类(A&B)实现了一些公共函数,并且必须从不同的类继承。所以,我决定让它成为genericone。但我不知道这是否可能以及如何做到。请看下面我的代码: 父类: //THIS PARENT CLASS MUST BE GENERIC TO EXTEND DIFFERENT CLASSES //SUCH AS Preference and LinearLayout abstract class C<T> { pu
generic
one。但我不知道这是否可能以及如何做到。请看下面我的代码:
父类:
//THIS PARENT CLASS MUST BE GENERIC TO EXTEND DIFFERENT CLASSES
//SUCH AS Preference and LinearLayout
abstract class C<T> {
public C(Context context) {
super(context);
}
public C(Context context, AttributeSet attrs) {
super(context, attrs);
}
public C(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
int commonFunc1(WebView view, int mode){
//implementation
}
//lots of common functions
}
//Class A must be inherited from the base class C that is inherited from LinearLayout
public class A extends C<LinearLayout>{
public A(Context context) {
super(context);
}
public A(Context context, AttributeSet attrs) {
super(context, attrs);
}
public A(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
void exec(){
commonFunc1(myWebView, 1);
}
}
//Class B must be inherited from the base class C that is inherited from Preference
public class B extends C<Preference>{
public B(Context context) {
super(context);
}
public B(Context context, AttributeSet attrs) {
super(context, attrs);
}
public B(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
void exec(){
commonFunc1(myWebView, 2);
}
}
//此父类必须是泛型才能扩展不同的类
//例如偏好和线性布局
抽象类C{
公共C(上下文){
超级(上下文);
}
公共C(上下文,属性集属性){
超级(上下文,attrs);
}
公共C(上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
}
int commonFunc1(网络视图,int模式){
//实施
}
//很多常见的功能
}
首选项和LinearLayout具有相同的构造函数
A&B课程:
//THIS PARENT CLASS MUST BE GENERIC TO EXTEND DIFFERENT CLASSES
//SUCH AS Preference and LinearLayout
abstract class C<T> {
public C(Context context) {
super(context);
}
public C(Context context, AttributeSet attrs) {
super(context, attrs);
}
public C(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
int commonFunc1(WebView view, int mode){
//implementation
}
//lots of common functions
}
//Class A must be inherited from the base class C that is inherited from LinearLayout
public class A extends C<LinearLayout>{
public A(Context context) {
super(context);
}
public A(Context context, AttributeSet attrs) {
super(context, attrs);
}
public A(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
void exec(){
commonFunc1(myWebView, 1);
}
}
//Class B must be inherited from the base class C that is inherited from Preference
public class B extends C<Preference>{
public B(Context context) {
super(context);
}
public B(Context context, AttributeSet attrs) {
super(context, attrs);
}
public B(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
void exec(){
commonFunc1(myWebView, 2);
}
}
//类A必须继承自从LinearLayout继承的基类C
公共A类扩展到C类{
公共A(上下文){
超级(上下文);
}
公共A(上下文、属性集属性){
超级(上下文,attrs);
}
公共A(上下文上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
}
void exec(){
commonFunc1(myWebView,1);
}
}
//类B必须从从首选项继承的基类C继承
公共B级扩展到C级{
公共B(上下文){
超级(上下文);
}
公共B(上下文、属性集属性){
超级(上下文,attrs);
}
公共B(上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
}
void exec(){
commonFunc1(myWebView,2);
}
}
因此,我的目标是让A类继承自LinearLayout
,具有C类的功能。B类继承自Preference
,具有C类的相同功能
我知道接口
可以有默认
实现,但它需要Java 1.8及以上版本
,这对我来说并不合适
任何帮助都将不胜感激 不幸的是,这是可能实现的。类不能仅扩展泛型类型
T
。您可以使用组合,而不是尝试用一些泛型类型实现继承
您可以执行以下操作:
class A extends LinearLayout {
...
...
// If C is expensive to create:
private final C cObj;
public A(final C cobj){
this.cObj = cObj;
}
//If C needs to be created based on A then you can pass all the parameters needed for C as parameter for A's contructor
void exec(){
c.commonFunc1(myWebView, 2);
}
}
class B extends Preference {
...
private C c;
...
//Use common functions of class C where-ever needed.
}
如果需要将
类C
作为抽象类,以便更改某些特定输入,那么您仍然将类C
作为抽象类
,并在初始化过程中将其初始化为匿名类。您的措辞确实不清楚。您不能从不同的类继承。扩展泛型类时,可以指定泛型类型参数,也可以不指定。这并不意味着您从该类型参数类继承了任何内容。@GhostCat,可以分别从类A和B为类C指定祖先类吗?不,不可以。C扩展对象。故事结束了。看来你应该更深入地研究泛型。@GhostCat,这就是我问这个问题的原因。在我的情况下,我需要复制A类和B类的代码?或者有更好的解决方案吗?我想知道对于完全不相关的类,如LinearLayout
和Preference
有什么“通用函数”非常不方便,因为我必须将许多变量传递给类的函数C@Nolesh您仍然可以将这些参数传递给A&B的构造函数,以便可以初始化C。不管怎样,构造C都需要这样做。您也可以先初始化C,然后将C的对象传递给A&B