gpt4 book ai didi

java构造方法重载与多重匹配

转载 作者:行者123 更新时间:2023-12-02 04:23:48 25 4
gpt4 key购买 nike

引用下面的代码:

public class ExpandableTextView extends TextView {

public ExpandableTextView(Context context) {
this(context, null, null);
}

public ExpandableTextView(Context context, AttributeSet attrs) {
this(context, attrs, null);
}

public ExpandableTextView(Context context, AttributeSet attrs, Runnable runnable) {
super(context, attrs);

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ExpandableTextView);
this.trimLength = typedArray.getInt(R.styleable.ExpandableTextView_trimLength, DEFAULT_TRIM_LENGTH);
typedArray.recycle();

setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
trim = !trim;
setText();
requestFocusFromTouch();
}
});
}

public ExpandableTextView(Context context, AttributeSet attrs, Activity activity) {
super(context, attrs);

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ExpandableTextView);
this.trimLength = typedArray.getInt(R.styleable.ExpandableTextView_trimLength, DEFAULT_TRIM_LENGTH);
typedArray.recycle();

setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
trim = !trim;
setText();
requestFocusFromTouch();
}
});
}
}

像这样的方法(context, null, null);可以引用另外两个构造函数方法,有什么方法可以指定它引用哪一个而不是更改签名或“null”?谢谢

最佳答案

当然,只需将 null 转换为签名类型即可!

    new ExpandableTextView(context, (AttributeSet)null, (Runnable)null)

关于java构造方法重载与多重匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32446329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com