Java 非静态块中的init方法
我正在扩展ScrollView,因为我使用了非静态块after构造函数来初始化一些变量 代码 我不想在每个构造函数中调用init(context)方法。这就是我使用非静态块的原因。你能建议一下这样做是否正确吗Java 非静态块中的init方法,java,android,constructor,scrollview,Java,Android,Constructor,Scrollview,我正在扩展ScrollView,因为我使用了非静态块after构造函数来初始化一些变量 代码 我不想在每个构造函数中调用init(context)方法。这就是我使用非静态块的原因。你能建议一下这样做是否正确吗 *我可以毫无错误地运行此代码。您不能使用静态上下文。如果您的问题是不想在每个构造函数中调用init,只需使用this而不是super(显式构造函数调用)。乙二醇 不能使用静态上下文。如果您的问题是不想在每个构造函数中调用init,只需使用this而不是super(显式构造函数调用)。乙二醇
*我可以毫无错误地运行此代码。您不能使用静态上下文。如果您的问题是不想在每个构造函数中调用init,只需使用
this
而不是super
(显式构造函数调用)。乙二醇
不能使用静态上下文。如果您的问题是不想在每个构造函数中调用init,只需使用
this
而不是super
(显式构造函数调用)。乙二醇
public ScrollViewExtended(Context context) {
super(context);
}
public ScrollViewExtended(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ScrollViewExtended(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public ScrollViewExtended(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
private void init(Context context) {
activity = (Activity) context;
userActivityLogDao = new UserActivityLogDao();
activity_name = activity.getClass().getSimpleName();
}
{
init(getContext());
}
public ScrollViewExtended(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ScrollViewExtended(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(this);
}