【问题标题】:Inheritance from class with change to it parent class at the new class从类继承并更改为新类的父类
【发布时间】:2015-08-26 21:08:47
【问题描述】:

我有一个扩展 TextView 的类,它基本上添加了一些羽毛来显示不同颜色的文本、动画等...

现在我正在使用其他不使用 Android TextView 但使用与 TextView 完全不相关的 Label 视图的自定义视图。

我想要做的是将所有逻辑和方法复制到一个新类中,该类将扩展 Label 并仅更改一些内容:颜色不是 int 类型而是 Color 类型等等。 setText(text) 的工作原理相同。

我是否必须真正复制粘贴所有课程并更改一些内容,或者我可以以某种方式使用一种逻辑并对新课程进行一些更改。当它扩展 Label 时,setText(text) 将在 Label 上执行相同的操作(Label 类也有该方法)

public class TextDisplayTextView extends TextView {

public TextDisplayTextView(Context context) {
        super(context);
        init();
    }

    public TextDisplayTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

private void setText (String text) {

// do some effects
setText(txt);
}

private void setTextColor (int color) {

// do some color effects
setTextColor(color);
}

.
.
.

}

新课程 - TextDisplayLabel

public class TextDisplayLabelView extends Label {

public TextDisplayLabelView(Label.Design design ) {
        super(design);
        init();
    }

private void setText (String text) {

// do some effects
setText(txt);
}

private void setTextColor (Color color) {

// do some color effects
setLabelColor(color);
}

.
.
.

}

EDITED:我想在两种情况下使用它

在使用TextView 的类中:

TextDisplayTextView textV = (TextDisplayTextView) mLayout.findViewById(R.id.textDisplay);
    mText.setText()

在课堂上使用Label

Label.Design designStyle = new Label.Design designStyle();
TextDisplayLabelView textL = new TextDisplayTextView (designStyle);
textL.setText();

【问题讨论】:

  • 也许使用抽象类和控制反转?您能否更具体地了解您要查找的内容?客户端代码如何使用每个类?
  • 我正在使用这些类创建实例,并使用在那里实现的方法,例如 setColorWithA()showTextAnimation()... 就像这样:mTextView.setColorWithA()mLabelCustomView.setColorWithA()
  • 所以基本上,你有一个方法 setColorWithA() 根据它是 textView 还是 labelCustomView 设置特定的颜色?请提供一些您正在执行此操作的代码。什么最适合您的用例?抽象将取决于您如何使用这些类。正如 Sanjay Manohar 所注意到的,Java 中不存在多重继承,但是有一些策略可以解决此类问题,具体取决于您的需要。
  • @FrancisToth 我添加了代码来显示我应该使用它,基本上在使用 TextView 的对象中使用第一个选项,而使用 Label 的对象使用第二个选项

标签: java android oop inheritance


【解决方案1】:

Java 没有多重继承。这是一个有争议的观点。 这将是实现您所建议的标准方法。

Why is there no multiple inheritance in Java, but implementing multiple interfaces is allowed?

但是有一些模式可以绕过它。

一种方法是使用辅助类。该类将包含“共同使用的代码”的实际实现。

另一种方法是对通用方面使用静态方法。

但是,您必须在各个接口实现中重复对该类的调用。


编辑

鉴于您添加的信息,我有一些可能性

public class TextDisplayLabelView extends Label {
  public void setText(String text){
    MyTextHelper.setTextHandler(this,text);
    super.setText(text);
  }
}
public class MyTextHelper {
   public static void setTextHandler(View item, String text){
     // do stuff that works on any type of item
   }
}

public class TextDisplayLabelView extends Label implements IMyTextHelper {
  public void setText(String text){
    MyTextHelper.setTextHandler(this,text);
    super.setText(text);
  }
}
public interface IMyTextHandler{
  static class MyTextHelper{
    public static voidTextLabel setTextHandler( ... ){ ... }
  }
}

或者在 Java 8 中,您可以直接在接口中使用 static 方法。

旁白:

请注意,这些实际上是多重继承的替代品。例如在 python 中,你会像这样继承:

class TextHelper extends Object:   methods to work on text
class TextDisplayLabelView extends Label,TextHelper
class TextDisplayTextView  extends TextView, TextHelper

后两个类将继承处理文本的方法,以及完全成熟的View 组件。

另一种可能性是使用代理对象。但这并没有实际上覆盖实际文本对象的方法,因此可能无法满足您的需要。

class GenericTextObject implements {
  public View proxy;
  public void setText(String text){
    if (proxy instanceof TextView){
       ((TextView)proxy).setText(text)
    }else if(proxy instanceof Label){
       ((Label)proxy).setText(text);
    }

    doExtraThings();
  }
}

【讨论】:

  • 即使可以选择使用多重继承,我也不希望有一个类具有来自TextViewLabel 的方法。你能展示一些如何用静态方法、接口实现它的代码吗?
猜你喜欢
  • 2014-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-24
  • 2020-06-06
  • 1970-01-01
  • 1970-01-01
  • 2016-06-05
相关资源
最近更新 更多