java - 从类继承并在新类中更改其父类

标签 java android oop inheritance

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

现在我正在使用其他自定义 View ,这些 View 不使用 Android TextView 但使用与 TextView 根本不相关的 Label View 。

我想要做的是将所有逻辑和方法复制到一个新类中,该类将扩展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);
}

.
.
.

}

已编辑:我想在两种情况下使用它

在使用 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();

最佳答案

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

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

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

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

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

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

<小时/>

编辑

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

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 中,您可以直接在接口(interface)中使用静态方法。

旁白:

请注意,这些实际上是多重继承的替代品。例如,在 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();
  }
}

关于java - 从类继承并在新类中更改其父类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32236728/

相关文章:

java - 找不到适合 show(ActionBar,String) 方法的方法 DialogFragment.show(FragmentTransaction,String) 不适用

java - 无法在 Android VideoView 中播放 h264 流

java - 如何在java中保持连接

java - 如何在 ServerName 和 Url 中使用 DNS 设置 Glassfish 连接池

java - 编辑程序以使用 Java 中用户输入的 do while/while 循环

Android - WebView 语言在 Android 7.0 及更高版本上突然更改

java - Kotlin 泛型转换为更具体的子类

javascript - 将值从输入传递到返回对象的构造函数

c++ - 工资单项目中的 fstream 问题

java - 使用 ArrayDeque 实现缓存