user1532208
user1532208

Reputation: 305

Why do some constructors in this class use this(context) instead of super(context)?

Why do two of the three constructors in this class use this(context) instead of super(context)?

The class is part of a larger project available at https://code.google.com/p/android-touchexample/source/browse/branches/cupcake/src/com/example/android/touchexample/TouchExampleView.java

package com.example.android.touchexample;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class TouchExampleView extends View {

private Drawable mIcon;
private float mPosX;
private float mPosY;

private VersionedGestureDetector mDetector;
private float mScaleFactor = 1.f;

public TouchExampleView(Context context) {
    this(context, null, 0);
}

public TouchExampleView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public TouchExampleView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    mIcon = context.getResources().getDrawable(R.drawable.icon);
    mIcon.setBounds(0, 0, mIcon.getIntrinsicWidth(), mIcon.getIntrinsicHeight());

    mDetector = VersionedGestureDetector.newInstance(context, new GestureCallback());
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    mDetector.onTouchEvent(ev);
    return true;
}

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    canvas.save();
    canvas.translate(mPosX, mPosY);
    canvas.scale(mScaleFactor, mScaleFactor);
    mIcon.draw(canvas);
    canvas.restore();
}

private class GestureCallback implements VersionedGestureDetector.OnGestureListener {
    public void onDrag(float dx, float dy) {
        mPosX += dx;
        mPosY += dy;
        invalidate();
    }

    public void onScale(float scaleFactor) {
        mScaleFactor *= scaleFactor;

        // Don't let the object get too small or too large.
        mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));

        invalidate();
    }
}

}

Upvotes: 1

Views: 120

Answers (1)

rgettman
rgettman

Reputation: 178303

In a constructor, this() does something different than super(). Calling this() defers to an overloaded constructor in the same class. Calling super() invokes a superclass constructor.

Here, the first two TouchExampleView constructors call this to defer to the third constructor by passing default value(s). The third constructor calls super to invoke the superclass constructor (plus do a few other things).

The Java Language Specification, Section 8.8.7.1 describes these calls.

Explicit constructor invocation statements can be divided into two kinds:

Alternate constructor invocations begin with the keyword this (possibly prefaced with explicit type arguments). They are used to invoke an alternate constructor of the same class.

Superclass constructor invocations begin with either the keyword super (possibly prefaced with explicit type arguments) or a Primary expression. They are used to invoke a constructor of the direct superclass.

Upvotes: 3

Related Questions