从类

时间:2017-04-17 05:16:36

标签: java android

我有Activity两个的不同类。

我有一个 CanvasLayout 类,需要在某些条件下更改活动的对象。

我该怎么做?

CurrentlyCountingActivity

public class CurrentlyCountingActivity extends AppCompatActivity {

    CanvasLayout canvasLayout;

    //I want to call one of the carCommands and 
    //pathCoordinates functions from canvasLayout
    CarCommands carCommands;
    PathCoordinates pathCoordinates;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        canvasLayout = new CanvasLayout(this);
        setContentView(canvasLayout);
        carCommands = new CarCommands();

        //because I don't want the car to be moving initially
        carCommands.setAbsoluteSpeed(0, 0);
    }

    //Other functions dealing with Path coordinates
}

CanvasLayout类

public class CanvasLayout extends SurfaceView implements Runnable {

    ... initializations here

    @Override
    public void run() {

        prepBrush();

        while(canDraw) {
            ..other irrelevant code

            motionPoint();

            ...more code


            if((pointX == INITIAL_X) && (pointY == INITIAL_Y)) {
                canDraw = false;
                //I would like to have something like 
                //carCommands.setAbsoluteSpeed(0, 0); here
            }
        }
    }


   private void motionPoint() {
       //I want to call pathCoordinates of Activity in CanvasLayout
       pathCoordinates.trackPathCoordinates();
       pointX = pathCoordinates.getX();
       pointY = pathCoordinates.getY();

   }

}

2 个答案:

答案 0 :(得分:1)

使用Interface

<强> SurfaceView

private OnCanvasListener onCanvasListener;
public class CanvasLayout extends SurfaceView implements Runnable {

  public interface OnCanvasListener{

    public void doSomething();
   }

   public CanvasLayout(Context this){
onCanvasListener = (OnCanvasListener)this;
   }


 @Override
    public void run() {

        prepBrush();

        while(canDraw) {
            ..other irrelevant code

            motionPoint();

            ...more code


            if((pointX == INITIAL_X) && (pointY == INITIAL_Y)) {
                canDraw = false;
                //I would like to have something like 
                //carCommands.setAbsoluteSpeed(0, 0); here
                 onCanvasListener.doSomething(); // invoke interface method

            }
        }
    }

}

在活动中实施界面

public class CurrentlyCountingActivity extends AppCompatActivity implements OnCanvasListener  {

    CanvasLayout canvasLayout;

    //I want to call one of the carCommands and 
    //pathCoordinates functions from canvasLayout
    CarCommands carCommands;
    PathCoordinates pathCoordinates;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        canvasLayout = new CanvasLayout(this);
        setContentView(canvasLayout);
        carCommands = new CarCommands();

        //because I don't want the car to be moving initially
        carCommands.setAbsoluteSpeed(0, 0);
    }

    //Other functions dealing with Path coordinates

  @Override
public void doSomething(){

// implement your logic here.
}

}

对不起任何错字。希望这会有所帮助。

答案 1 :(得分:0)

有几种方法可以访问CarCommands对象:

  1. 在CanvasLayout中创建一个带有CarCommands对象的构造函数。

  2. 在CanvasLayout中创建一个带有CarCommands对象的setter方法。

  3. 在CanvasLayout中创建CarCommands对象。

  4. 向activity类添加允许访问CarCommands对象的方法,并向CanvasLayout添加一个接受CurrentlyCountingActivity的构造函数。

  5. 我相信还有其他选择。您选择哪一个在很大程度上取决于代码设计中的其他因素。我向#3学习。活动真的需要CarCommands吗?

    我建议你继续学习面向对象的原则。只有经验才能帮助您判断此类设计决策的权衡。